Java 如何在多个类中使用JTextField

Java 如何在多个类中使用JTextField,java,swing,class,jtextfield,tic-tac-toe,Java,Swing,Class,Jtextfield,Tic Tac Toe,我有三门课是关于抽搐的。 类1:这是主类,它按住按钮等 第二类:游戏本身计算分数等 类别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 Ima

我有三门课是关于抽搐的。 类1:这是主类,它按住按钮等 第二类:游戏本身计算分数等 类别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.");
}

}`

正如您在30分钟前的类似问题中所讨论的:

使用一个JFrame。另一个窗口应该是JDialog,尽管这里是非模态JDialog。 使用getter方法允许一个类从另一个类获取分数字符串。 由于我们当时没有足够的信息,这里没有讨论的棘手部分是知道何时将信息传递给Score JDialog。在这里,您的主GUI应该包含一个分数JDialog实例,为这个JDialog提供一个setter方法,当实际分数发生变化时,该方法允许主GUI设置分数的分数JTextField。
`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);
}