【问题标题】:Method not calling方法不调用
【发布时间】:2014-05-10 23:01:29
【问题描述】:

在第一类文件中创建了 GUI

public class TicTac extends JFrame {
TicTacEvent tictac = new TicTacEvent(this);
JPanel row1 = new JPanel();
JButton[][] boxes = new JButton[4][4];
JButton play = new JButton("Play");
JButton restart = new JButton("Restart");
JTextField blank1 = new JTextField();
JTextField blank2 = new JTextField();
JOptionPane win = new JOptionPane("Winner");
ImageIcon back = new ImageIcon("cardback.jpg");

public TicTac() {
    super ("Tic Tac Toe");
    setSize (800,650);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    FlowLayout layout = new FlowLayout();
    setLayout(layout);
    int name = 0;
    String newname;

    GridLayout layout1 = new GridLayout(5, 4, 10, 10);
    row1.setLayout(layout1);
    for (int x=0; x<=3; x++){
        for (int y=0; y<=3; y++){
            name = name + 1;
            newname = Integer.toString(name);
            boxes[x][y] = new JButton(newname);
            boxes[x][y].setIcon(back);
            row1.add(boxes[x][y]);
        }
    }
    row1.add(blank1);
    row1.add(play);
    row1.add(blank2);
    row1.add(restart);
    add (row1);

    play.addActionListener(tictac);
    for (int x=0; x<=3; x++){
        for (int y=0; y<=3; y++){
            boxes[x][y].addActionListener(tictac);
        }
    }


    setVisible(true);
}

我想做的是创建一个重启按钮,但是我的方法没有调用

   Thread playing;
   Thread restarting;
   public void actionPerformed(ActionEvent event){

   String command = event.getActionCommand();

   if (command.equals("Play")) {
    startPlaying();  
       }
          if (command.equals("Restart")){
   restart();

我的 Restart 方法根本没有调用,我只在方法中尝试了随机 setText ,但它仍然不会这样做,我不知道为什么,没有错误。稍后我将要解决的另一个问题是修复我的重启方法,我不确定它是否会起作用,是否可以只调用公共方法“TicTac”,在不同的类中重新创建 GUI?

我找了这么久还是找不到办法?

    public void restart();
     TicTac restartok = new TicTac();
    restartok.row1.add(restartok.blank1);
    restartok.row1.add(restartok.play);
    restartok.row1.add(restartok.blank2);
    restartok.row1.add(restartok.restart);
    restartok.add (restartok.row1);

    restartok.play.addActionListener(restartok.tictac);
    for (int x=0; x<=3; x++){
        for (int y=0; y<=3; y++){
            restartok.boxes[x][y].addActionListener(restartok.tictac);
        }
    }

}

【问题讨论】:

    标签: java class methods


    【解决方案1】:

    一个明显的例子是命令既不是“播放”也不是“重启”。

    您可以编写此代码以快速调试正在发生的事情:

    if (command.equals("Play")) {
        startPlaying();  
    } else if (command.equals("Restart")) {
        restart();
    } else {
        throw new IllegalArgumentException("Unknown command: " command);
    }
    

    【讨论】:

    • 两者都没有抛出 IllegalArguementException,所以我假设这是我的重新启动方法没有执行,仍然一无所知。但是感谢您向我展示了调试代码段。
    【解决方案2】:

    你在哪里定义restart?您有一个void restart();,但没有定义该方法。如果我正确阅读了您的代码,您想更改

    public void restart();
        TicTac restartok = new TicTac();
        restartok.row1.add(restartok.blank1);
        restartok.row1.add(restartok.play);
        restartok.row1.add(restartok.blank2);
        restartok.row1.add(restartok.restart);
        restartok.add (restartok.row1);
    
        restartok.play.addActionListener(restartok.tictac);
        for (int x=0; x<=3; x++){
            for (int y=0; y<=3; y++){
                restartok.boxes[x][y].addActionListener(restartok.tictac);
            }
        }
    }
    

    public void restart() {
        TicTac restartok = new TicTac();
        restartok.row1.add(restartok.blank1);
        restartok.row1.add(restartok.play);
        restartok.row1.add(restartok.blank2);
        restartok.row1.add(restartok.restart);
        restartok.add (restartok.row1);
    
        restartok.play.addActionListener(restartok.tictac);
        for (int x=0; x<=3; x++){
            for (int y=0; y<=3; y++){
                restartok.boxes[x][y].addActionListener(restartok.tictac);
            }
        }
    }
    

    【讨论】:

    • 也试过了,现在它正在调用,所以我的问题很可能在我的方法实际代码中,我假设。
    猜你喜欢
    • 2019-11-20
    • 2014-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-12
    • 2013-12-13
    • 2012-03-08
    相关资源
    最近更新 更多