方法没有打电话

时间:2014-05-10 23:01:30

标签: java class methods

在第一类文件中创建了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();

我的重启方法根本就没有调用,我只尝试了单独的方法中的随机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);
        }
    }

}

2 个答案:

答案 0 :(得分:0)

一个明显的例子是该命令既不是&#34; Play&#34;也没有&#34;重新启动&#34;。

您可以对此进行编码以快速调试正在发生的事情:

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

答案 1 :(得分:0)

您在哪里定义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);
        }
    }
}