如何将Jbutton中的文本转换为更改后更新的变量?

时间:2014-04-20 02:32:27

标签: java jpanel jbutton jtextarea jtextpane

我的202课程的最后编程任务有一些问题。

首先,我的任务是在Java中创建一个名为Togiz Kumalak的游戏。如你所见,我远未完成。

到目前为止,我的两个问题是我的按钮和文字显示不可见,而且我不知道如何将我的按钮内的数字(它们代表种子)变成可以改变的变量数字0-10。谢谢你的帮助。

我为这个烂摊子道歉,我已经添加了因ADD而无法正常运行的东西。

编辑:我弄清楚为什么我的面板不可见,这是因为我看到第四个面板没有添加内容。第二个问题仍然存在。

Edit2:更新了标题和代码。

import java.awt.*;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Window;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JLabel;

public class Togiz_Kumalak {

    private static void createAndShowUI() {
        CupGui gui = new CupGui();
        TkMenu menu = new TkMenu(gui);
        JFrame frame = new JFrame("Togiz Kumalak");
        frame.getContentPane().add(gui.getMainPanel());

        frame.setJMenuBar(menu.getJMenuBar());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        frame.setFocusable(true);
    }

    public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                createAndShowUI();
            }
        });
    }

    private Togiz_Kumalak() {
    }
}

class CupGui {

    private   String[][] Cup1_Texts = {
        {"10", "10", "10", "10","10", "10", "10"},};
        private  String[][] Cup2_Texts = {
        {"10", "10", "10", "10","10", "10", "10"},};

    private static final int GAP = 5;
    private static final Font BTN_FONT = new Font(Font.DIALOG, Font.BOLD, 20);
    private JPanel firstPanel = new JPanel();
    private JPanel secondPanel = new JPanel();
    private JPanel thirdPanel = new JPanel();
    private JPanel fourthPanel = new JPanel();
    private JPanel textPanel = new JPanel();
    private JPanel mainPanel = new JPanel();
    private JPanel cupPane1;
    private JPanel cupPane2;
    private JTextField display = new JTextField();
    private JLabel label = new JLabel("test");

    CupGui() {
        display.setFont(BTN_FONT);
        cupPane1 = createBtnPanel(Cup1_Texts, "Player 1");
        cupPane2 = createBtnPanel(Cup2_Texts, "Player 2");
        mainPanel.setLayout(new BorderLayout());
        mainPanel.setBorder(BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP));
        firstPanel.add(cupPane1, BorderLayout.CENTER);

        secondPanel.add(cupPane2, BorderLayout.CENTER);
        thirdPanel.add(firstPanel, BorderLayout.LINE_START);
        thirdPanel.add(secondPanel, BorderLayout.LINE_END);
        thirdPanel.add(fourthPanel, BorderLayout.PAGE_START);
        fourthPanel.add(display, BorderLayout.CENTER);


        mainPanel.add(thirdPanel);





        cupPane1.setVisible(true);
        cupPane2.setVisible(true);
        firstPanel.setVisible(true);
        secondPanel.setVisible(true);
        thirdPanel.setVisible(true);
        fourthPanel.setVisible(true);

    }

    public void cupPanelSetVisible(boolean visible) {
        cupPane1.setVisible(visible);
        Window win = SwingUtilities.getWindowAncestor(mainPanel);
        win.pack();
    }

    public JPanel getMainPanel() {
        return mainPanel;
    }
    public JPanel getFirstPanel(){
      return firstPanel;
    }
    public JPanel getSecondPanel(){
      return secondPanel;
    }

    private JPanel createBtnPanel(String[][] texts, String title) {
        JPanel btnPanel = new JPanel();
        int rows = texts.length;
        int cols = texts[0].length;
        btnPanel.setLayout(new GridLayout(rows, cols, GAP, GAP));
        for (int row = 0; row < texts.length; row++) {
            for (int col = 0; col < texts[row].length; col++) {
                JButton btn = new JButton(texts[row][col]);
                btn.setFont(BTN_FONT);
                btnPanel.add(btn);
            }
        }
        btnPanel.setBorder(BorderFactory.createTitledBorder(title));
        return btnPanel;
    }
}

class TkMenu {

    private static final String NEW_GAME =  "New Game";
    private static final String LOAD_GAME = "Load Game";
    private static final String SAVE_GAME = "Save Game";

    private CupGui gui;
    private JMenuBar menuBar = new JMenuBar();
    private JMenuItem newGame;
    private JMenuItem loadGame;
    private JMenuItem saveGame;

    TkMenu(CupGui gui) {
        this.gui = gui;
        newGame = new JMenuItem(NEW_GAME, KeyEvent.VK_T);
        loadGame = new JMenuItem(LOAD_GAME, KeyEvent.VK_S);
        saveGame = new JMenuItem(SAVE_GAME, KeyEvent.VK_S);



        newGame.setEnabled(true);
        JMenu viewMenu = new JMenu("File");
        viewMenu.setMnemonic(KeyEvent.VK_V);
        viewMenu.add(newGame);
        viewMenu.add(loadGame);
        viewMenu.add(saveGame);

        menuBar.add(viewMenu);

    }

    public JMenuBar getJMenuBar() {
        return menuBar;
    }


        }

1 个答案:

答案 0 :(得分:0)

尝试将ActionListener添加到按钮,然后将文本设置为您选择的变量。您还可以获取按钮的当前文本,执行计算,然后再次设置按钮的文本。例如:

 btn.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    // TODO Auto-generated method stub
                    int x;
                    x = Integer.parseInt(btn.getText());
                    x--;
                    btn.setText(Integer.toString(x));
                }
            });