繁体   English   中英

如何在多个类中使用 JTextField

[英]How to use a JTextField in more than one class

我有 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);
}

}`

正如您在 30 分钟前的上一个类似问题中所讨论的那样:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM