【问题标题】:How to use a JTextField in more than one class如何在多个类中使用 JTextField
【发布时间】:2014-04-24 21:52:26
【问题描述】:

我有 3 个井字游戏课程。 1类:这是主类,它持有按钮等 Class 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);
}

}`

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-25
      • 1970-01-01
      • 2016-10-13
      • 2014-01-07
      • 2016-12-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多