簡體   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