如何在多个类中使用JTextField

时间:2014-04-24 21:52:27

标签: java swing class jtextfield tic-tac-toe

我有3个Tic Tac Toe课程。      第1类:这是主类,它按住按钮等      第2类:游戏本身计算分数等      第3类:显示第2类的分数

我试图从第2课得到分数并在第3课中显示

`Class 2`
`public class TicTacEvent implements ItemListener, ActionListener, Runnable {
TicTac gui;
Thread playing;
ImageIcon a = new ImageIcon("x.jpg");
ImageIcon b = new ImageIcon("o.jpg");
int clicks = 0;
int win = 0;
int winX = 0;
int winO = 0;
int tie = 0;
int[][] check = new int[3][3];

public TicTacEvent (TicTac in){
    gui = in;
    for (int row=0; row<=2; row++){
       for (int col=0; col<=2; col++){
           check[row][col]=0;
       }
   }
}

public void actionPerformed (ActionEvent event) {
   String command = event.getActionCommand();

   if (command.equals("Play")) {
       startPlaying();
   }
   if (command.equals("1")) {
       b1();
   }
   if (command.equals("2")) {
       b2();
   }
   if (command.equals("3")) {
       b3();
   }
   if (command.equals("4")) {
       b4();
   }
   if (command.equals("5")) {
       b5();
   }
   if (command.equals("6")) {
       b6();
   }
   if (command.equals("7")) {
       b7();
   }
   if (command.equals("8")) {
       b8();
   }
   if (command.equals("9")) {
       b9();
   }
}

void b1() {
    clicks = clicks + 1;
    if ((clicks%2)==1){
        gui.boxes[0][0].setIcon(a);
        check[0][0] = 1;
    } else {
        gui.boxes[0][0].setIcon(b);
        check[0][0] = 2;
    }
    winner();

}
void b2() {
    clicks = clicks + 1;
    if ((clicks%2)==1){
        gui.boxes[0][1].setIcon(a);
        check[0][1] = 1;
    } else {
        gui.boxes[0][1].setIcon(b);
        check[0][1] = 2;
    }
    winner();
}
void b3() {
    clicks = clicks + 1;
    if ((clicks%2)==1){
        gui.boxes[0][2].setIcon(a);
        check[0][2] = 1;
    } else {
        gui.boxes[0][2].setIcon(b);
        check[0][2] = 2;
    }
    winner();
}
void b4() {
    clicks = clicks + 1;
    if ((clicks%2)==1){
        gui.boxes[1][0].setIcon(a);
        check[1][0] = 1;
    } else {
        gui.boxes[1][0].setIcon(b);
        check[1][0] = 2;
    }
    winner();
}
void b5() {
    clicks = clicks + 1;
    if ((clicks%2)==1){
        gui.boxes[1][1].setIcon(a);
        check[1][1] = 1;
    } else {
        gui.boxes[1][1].setIcon(b);
        check[1][1] = 2;
    }
    winner();
}
void b6() {
    clicks = clicks + 1;
    if ((clicks%2)==1){
        gui.boxes[1][2].setIcon(a);
        check[1][2] = 1;
    } else {
        gui.boxes[1][2].setIcon(b);
        check[1][2] = 2;
    }
    winner();
}
void b7() {
    clicks = clicks + 1;
    if ((clicks%2)==1){
        gui.boxes[2][0].setIcon(a);
        check[2][0] = 1;
    } else {
        gui.boxes[2][0].setIcon(b);
        check[2][0] = 2;
    }
    winner();
}
void b8() {
    clicks = clicks + 1;
    if ((clicks%2)==1){
        gui.boxes[2][1].setIcon(a);
        check[2][1] = 1;
    } else {
        gui.boxes[2][1].setIcon(b);
        check[2][1] = 2;
    }
    winner();
}
void b9() {
    clicks = clicks + 1;
    if ((clicks%2)==1){
        gui.boxes[2][2].setIcon(a);
        check[2][2] = 1;
    } else {
        gui.boxes[2][2].setIcon(b);
        check[2][2] = 2;
    }
    winner();
}

void winner() {
    /** Check rows for winner */

    for (int x=0; x<=2; x++){
        if ((check[x][0]==check[x][1])&&(check[x][0]==check[x][2])) {
            if (check[x][0]==1) {
                JOptionPane.showMessageDialog(null, "X is the winner");
                winX = 1;
                System.out.println(Integer.toString(winX));
            } else if (check[x][0]==2){
                JOptionPane.showMessageDialog(null, "O is the winner");
                winO = 1;
            }
        }
    }

    /** Check columns for winner */
    for (int x=0; x<=2; x++){
        if ((check[0][x]==check[1][x])&&(check[0][x]==check[2][x])) {
            if (check[0][x]==1) {
                JOptionPane.showMessageDialog(null, "X is the winner");
                winX = 1;

            } else if (check[0][x]==2) {
                JOptionPane.showMessageDialog(null, "O is the winner");
                winO = 1;
            }
        }
    }

    /** Check diagonals for winner */
    if (((check[0][0]==check[1][1])&&(check[0][0]==check[2][2]))||
            ((check[2][0]==check[1][1])&&(check[1][1]==check[0][2]))){
        if (check[1][1]==1) {
            JOptionPane.showMessageDialog(null, "X is the winner");
            winX = 1;
        } else if (check[1][1]==2) {
            JOptionPane.showMessageDialog(null, "O is the winner");
            winO = 1;
        }

    }

    /** Checks if the game is a tie */
    if ((clicks==9) && (win==0)) {
        JOptionPane.showMessageDialog(null, "The game is a tie");
    }
}


void startPlaying() {
    playing = new Thread(this);
    playing.start();
    gui.play.setEnabled(false);
}

public void itemStateChanged(ItemEvent e) {
    throw new UnsupportedOperationException("Not supported yet.");
}

public void run() {
    throw new UnsupportedOperationException("Not supported yet.");
}

}

`Class 3`

`public class Score extends JFrame{
private JTextField xScore;
private JTextField oScore;
private JTextField tieScore;
private JButton reset;

public Score(){
    JFrame frame = new JFrame("Score");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(300, 300);
    FlowLayout score = new FlowLayout();
    frame.setLayout(score);
    xScore = new JTextField("X Wins: ");
    oScore = new JTextField("O Wins: ");
    tieScore = new JTextField("Ties: ");
    reset = new JButton("Reset");

    xScore.setBounds(0, 0, 100, 20);
    oScore.setBounds(0, 30, 100, 20);
    tieScore.setBounds(0, 60, 100, 20);

    frame.add(xScore);
    frame.add(oScore);
    frame.add(tieScore);
    frame.add(reset);

    frame.setBackground(Color.BLACK);
    frame.pack();
    frame.setVisible(true);
}

}`

1 个答案:

答案 0 :(得分:1)

正如之前30分钟前的类似问题所述:

  • 使用一个JFrame。另一个窗口应该是JDialog,虽然这里是一个非模态的JDialog。
  • 使用getter方法允许一个类从另一个类获取得分String。
  • 由于我们当时没有足够的信息,所以没有讨论的棘手部分是知道何时将信息传递给分数JDialog。在这里你的主GUI应该包含Score JDialog的一个实例,给这个JDialog一个setter方法,允许主GUI在实际分数改变时设置得分JTextField。