Java 在Swing UI线程中放置另一个方法的位置
我正在使用Swing在Java中制作一个2人Tic-Tac-Toe游戏。用户界面工作正常,但我无法让它检查是否有赢家 我制作了三个类Java 在Swing UI线程中放置另一个方法的位置,java,swing,user-interface,Java,Swing,User Interface,我正在使用Swing在Java中制作一个2人Tic-Tac-Toe游戏。用户界面工作正常,但我无法让它检查是否有赢家 我制作了三个类Main,gui,和winnerDisplay。gui类负责创建3x3网格和按钮等。winnerDisplay类用于显示祝贺获胜者的提示。我假设第一个玩家选择“X”符号。此外,3x3网格以行主方式从0到8进行编号。(此处“网格”一词用于一般意义,而不是GridLayout/2D矩阵意义) 我想在每个玩家的每一回合后检查是否有赢家(我将在以后对其进行优化,因为至少三步
Main
,gui
,和winnerDisplay
。gui
类负责创建3x3网格和按钮等。winnerDisplay
类用于显示祝贺获胜者的提示。我假设第一个玩家选择“X”符号。此外,3x3网格以行主方式从0到8进行编号。(此处“网格”一词用于一般意义,而不是GridLayout/2D矩阵意义)
我想在每个玩家的每一回合后检查是否有赢家(我将在以后对其进行优化,因为至少三步之后才能有赢家)。如果有赢家或平局,我希望另一个窗口弹出并显示赢家的名字
现在的问题是:无论我在哪里放置isWon()
方法,它都不会停止游戏的主UI线程,也不会在每次移动后检查它。我知道UI运行在不同的线程上,但如何实现我的目标
这是Main
类:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package tictactoeDivyanshuVarma;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
/**
*
* @author divyanshuvarma
*/
public class Main {
/**
* @param args the command line arguments
*/
static boolean playerOneTurn = true;
static boolean isFilled0 = false, isFilled1 = false, isFilled2 = false;
static boolean isFilled3 = false, isFilled4 = false, isFilled5 = false;
static boolean isFilled6 = false, isFilled7 = false, isFilled8 = false;
//static JButton button;
static boolean gameWon = false;
public static void main(String[] args) {
// TODO code application logic here
gui board = new gui();
winnerDisplay winnerDisplayWindow = new winnerDisplay();
setBoardProperties(board);
initializeBoard(board);
initializeGame(board);
/* if (isWon(board)) {
winnerDisplayWindow.setVisible(true);
winnerDisplayWindow.setTitle("Congrats Winner !");
winnerDisplayWindow.setResizable(false);
winnerDisplayWindow.setAlwaysOnTop(true);
JLabel winnerLabel = new JLabel("Winner is ");
winnerDisplayWindow.setWinnerLabel(winnerLabel);
}*/
System.out.println("\nThread count: "+Thread.activeCount());
}
public static void setBoardProperties(gui board) {
board.setVisible(true);
board.setTitle("Tic-Tac-Toe (2-player)");
board.setResizable(false);
}
public static void initializeGame(gui board) {
JButton button0, button1, button2, button3, button4, button5, button6, button7, button8;
button0 = board.getButton0();
button0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!isFilled0) {
if (playerOneTurn) {
button0.setText("X");
} else {
button0.setText("O");
}
isFilled0 = true;
// I HAVE TRIED PLACING isWon() HERE
if (playerOneTurn) {
playerOneTurn = false;
} else {
playerOneTurn = true;
}
}
}
});
button1 = board.getButton1();
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!isFilled1) {
if (playerOneTurn) {
button1.setText("X");
} else {
button1.setText("O");
}
isFilled1 = true;
// I HAVE TRIED PLACING isWon() HERE
if (playerOneTurn) {
playerOneTurn = false;
} else {
playerOneTurn = true;
}
}
}
});
button2 = board.getButton2();
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!isFilled2) {
if (playerOneTurn) {
button2.setText("X");
} else {
button2.setText("O");
}
isFilled2 = true;
// I HAVE TRIED PLACING isWon() HERE
if (playerOneTurn) {
playerOneTurn = false;
} else {
playerOneTurn = true;
}
}
}
});
button3 = board.getButton3();
button3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!isFilled3) {
if (playerOneTurn) {
button3.setText("X");
} else {
button3.setText("O");
}
isFilled3 = true;
// I HAVE TRIED PLACING isWon() HERE
if (playerOneTurn) {
playerOneTurn = false;
} else {
playerOneTurn = true;
}
}
}
});
button4 = board.getButton4();
button4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!isFilled4) {
if (playerOneTurn) {
button4.setText("X");
} else {
button4.setText("O");
}
isFilled4 = true;
// I HAVE TRIED PLACING isWon() HERE
if (playerOneTurn) {
playerOneTurn = false;
} else {
playerOneTurn = true;
}
}
}
});
button5 = board.getButton5();
button5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!isFilled5) {
if (playerOneTurn) {
button5.setText("X");
} else {
button5.setText("O");
}
isFilled5 = true;
// I HAVE TRIED PLACING isWon() HERE
if (playerOneTurn) {
playerOneTurn = false;
} else {
playerOneTurn = true;
}
}
}
});
button6 = board.getButton6();
button6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!isFilled6) {
if (playerOneTurn) {
button6.setText("X");
} else {
button6.setText("O");
}
isFilled6 = true;
// I HAVE TRIED PLACING isWon() HERE
if (playerOneTurn) {
playerOneTurn = false;
} else {
playerOneTurn = true;
}
}
}
});
button7 = board.getButton7();
button7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!isFilled7) {
if (playerOneTurn) {
button7.setText("X");
} else {
button7.setText("O");
}
isFilled7 = true;
// I HAVE TRIED PLACING isWon() HERE
if (playerOneTurn) {
playerOneTurn = false;
} else {
playerOneTurn = true;
}
}
}
});
button8 = board.getButton8();
button8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!isFilled8) {
if (playerOneTurn) {
button8.setText("X");
} else {
button8.setText("O");
}
isFilled8 = true;
// I HAVE TRIED PLACING isWon() HERE
if (playerOneTurn) {
playerOneTurn = false;
} else {
playerOneTurn = true;
}
}
}
});// I HAVE TRIED PLACING isWon() HERE TOO
}
public static boolean isWon(gui board) {
String symbol0, symbol1, symbol2, symbol3, symbol4, symbol5, symbol6, symbol7, symbol8;
symbol0 = board.getButton0().getText();
symbol1 = board.getButton1().getText();
symbol2 = board.getButton2().getText();
symbol3 = board.getButton3().getText();
symbol4 = board.getButton4().getText();
symbol5 = board.getButton5().getText();
symbol6 = board.getButton6().getText();
symbol7 = board.getButton7().getText();
symbol8 = board.getButton8().getText();
if (symbol0.equals(symbol4) && symbol4.equals(symbol8)) //main diagonal
{
gameWon = true;
}
if (symbol2.equals(symbol4) && symbol4.equals(symbol6)) //other diagonal
{
gameWon = true;
}
if (symbol0.equals(symbol1) && symbol1.equals(symbol2)) //first row
{
gameWon = true;
}
if (symbol3.equals(symbol4) && symbol4.equals(symbol5)) //second row
{
gameWon = true;
}
if (symbol6.equals(symbol7) && symbol7.equals(symbol8)) //third row
{
gameWon = true;
}
if (symbol0.equals(symbol3) && symbol3.equals(symbol6)) //first column
{
gameWon = true;
}
if (symbol1.equals(symbol4) && symbol4.equals(symbol7)) //second column
{
gameWon = true;
}
if (symbol2.equals(symbol5) && symbol5.equals(symbol8)) //third column
{
gameWon = true;
}
if (gameWon) {
return true;
} else {
return false;
}
}
public static void initializeBoard(gui board) {
JButton button;
button = board.getButton0();
button.setText("");
button = board.getButton1();
button.setText("");
button = board.getButton2();
button.setText("");
button = board.getButton3();
button.setText("");
button = board.getButton4();
button.setText("");
button = board.getButton5();
button.setText("");
button = board.getButton6();
button.setText("");
button = board.getButton7();
button.setText("");
button = board.getButton8();
button.setText("");
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package tictactoeDivyanshuVarma;
import javax.swing.JButton;
/**
*
* @author divyanshuvarma
*/
public class gui extends javax.swing.JFrame {
/**
* Creates new form gui
*/
public gui() {
initComponents();
}
public void setButton0Text(String symbol) {
this.button0.setText(symbol);
}
public void setButton1Text(String symbol) {
this.button1.setText(symbol);
}
public void setButton2Text(String symbol) {
this.button2.setText(symbol);
}
public void setButton3Text(String symbol) {
this.button3.setText(symbol);
}
public void setButton4Text(String symbol) {
this.button4.setText(symbol);
}
public void setButton5Text(String symbol) {
this.button5.setText(symbol);
}
public void setButton6Text(String symbol) {
this.button6.setText(symbol);
}
public void setButton7Text(String symbol) {
this.button7.setText(symbol);
}
public void setButton8Text(String symbol) {
this.button8.setText(symbol);
}
public JButton getButton0() {
return button0;
}
public JButton getButton1() {
return button1;
}
public JButton getButton2() {
return button2;
}
public JButton getButton3() {
return button3;
}
public JButton getButton4() {
return button4;
}
public JButton getButton5() {
return button5;
}
public JButton getButton6() {
return button6;
}
public JButton getButton7() {
return button7;
}
public JButton getButton8() {
return button8;
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
button1 = new javax.swing.JButton();
button3 = new javax.swing.JButton();
button5 = new javax.swing.JButton();
button2 = new javax.swing.JButton();
button6 = new javax.swing.JButton();
button8 = new javax.swing.JButton();
button4 = new javax.swing.JButton();
button0 = new javax.swing.JButton();
button7 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
button1.setText("jButton1");
button1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
button1ActionPerformed(evt);
}
});
button3.setText("jButton1");
button5.setText("jButton1");
button2.setText("jButton1");
button6.setText("jButton1");
button8.setText("jButton1");
button4.setText("jButton1");
button7.setText("jButton1");
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(button0, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGap(72, 72, 72)
.addComponent(button1))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(button3)
.addGap(72, 72, 72)
.addComponent(button4))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(button6)
.addGap(72, 72, 72)
.addComponent(button7)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 79, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(button8, javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(button5, javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(button2, javax.swing.GroupLayout.Alignment.TRAILING))
.addContainerGap())
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(button0, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(button1, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(button2, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(18, 18, Short.MAX_VALUE)
.addComponent(button8, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(32, 32, 32)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(button3, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(button4, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(button5, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 55, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(button6, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(button7, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addContainerGap())
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
);
pack();
}// </editor-fold>
private void button1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(gui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(gui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(gui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(gui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new gui().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton button0;
private javax.swing.JButton button1;
private javax.swing.JButton button2;
private javax.swing.JButton button3;
private javax.swing.JButton button4;
private javax.swing.JButton button5;
private javax.swing.JButton button6;
private javax.swing.JButton button7;
private javax.swing.JButton button8;
private javax.swing.JPanel jPanel1;
// End of variables declaration
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package tictactoeDivyanshuVarma;
import javax.swing.JLabel;
/**
*
* @author divyanshuvarma
*/
public class winnerDisplay extends javax.swing.JFrame {
/**
* Creates new form winnerDisplay
*/
public winnerDisplay() {
initComponents();
}
public JLabel getWinnerLabel() {
return winnerLabel;
}
public void setWinnerLabel(JLabel winnerLabel) {
this.winnerLabel = winnerLabel;
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
winnerLabel = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
winnerLabel.setText("jLabel1");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(77, 77, 77)
.addComponent(winnerLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 243, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(80, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(82, Short.MAX_VALUE)
.addComponent(winnerLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(94, 94, 94))
);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(winnerDisplay.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(winnerDisplay.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(winnerDisplay.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(winnerDisplay.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new winnerDisplay().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel winnerLabel;
// End of variables declaration
}
这是gui
类:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package tictactoeDivyanshuVarma;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
/**
*
* @author divyanshuvarma
*/
public class Main {
/**
* @param args the command line arguments
*/
static boolean playerOneTurn = true;
static boolean isFilled0 = false, isFilled1 = false, isFilled2 = false;
static boolean isFilled3 = false, isFilled4 = false, isFilled5 = false;
static boolean isFilled6 = false, isFilled7 = false, isFilled8 = false;
//static JButton button;
static boolean gameWon = false;
public static void main(String[] args) {
// TODO code application logic here
gui board = new gui();
winnerDisplay winnerDisplayWindow = new winnerDisplay();
setBoardProperties(board);
initializeBoard(board);
initializeGame(board);
/* if (isWon(board)) {
winnerDisplayWindow.setVisible(true);
winnerDisplayWindow.setTitle("Congrats Winner !");
winnerDisplayWindow.setResizable(false);
winnerDisplayWindow.setAlwaysOnTop(true);
JLabel winnerLabel = new JLabel("Winner is ");
winnerDisplayWindow.setWinnerLabel(winnerLabel);
}*/
System.out.println("\nThread count: "+Thread.activeCount());
}
public static void setBoardProperties(gui board) {
board.setVisible(true);
board.setTitle("Tic-Tac-Toe (2-player)");
board.setResizable(false);
}
public static void initializeGame(gui board) {
JButton button0, button1, button2, button3, button4, button5, button6, button7, button8;
button0 = board.getButton0();
button0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!isFilled0) {
if (playerOneTurn) {
button0.setText("X");
} else {
button0.setText("O");
}
isFilled0 = true;
// I HAVE TRIED PLACING isWon() HERE
if (playerOneTurn) {
playerOneTurn = false;
} else {
playerOneTurn = true;
}
}
}
});
button1 = board.getButton1();
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!isFilled1) {
if (playerOneTurn) {
button1.setText("X");
} else {
button1.setText("O");
}
isFilled1 = true;
// I HAVE TRIED PLACING isWon() HERE
if (playerOneTurn) {
playerOneTurn = false;
} else {
playerOneTurn = true;
}
}
}
});
button2 = board.getButton2();
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!isFilled2) {
if (playerOneTurn) {
button2.setText("X");
} else {
button2.setText("O");
}
isFilled2 = true;
// I HAVE TRIED PLACING isWon() HERE
if (playerOneTurn) {
playerOneTurn = false;
} else {
playerOneTurn = true;
}
}
}
});
button3 = board.getButton3();
button3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!isFilled3) {
if (playerOneTurn) {
button3.setText("X");
} else {
button3.setText("O");
}
isFilled3 = true;
// I HAVE TRIED PLACING isWon() HERE
if (playerOneTurn) {
playerOneTurn = false;
} else {
playerOneTurn = true;
}
}
}
});
button4 = board.getButton4();
button4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!isFilled4) {
if (playerOneTurn) {
button4.setText("X");
} else {
button4.setText("O");
}
isFilled4 = true;
// I HAVE TRIED PLACING isWon() HERE
if (playerOneTurn) {
playerOneTurn = false;
} else {
playerOneTurn = true;
}
}
}
});
button5 = board.getButton5();
button5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!isFilled5) {
if (playerOneTurn) {
button5.setText("X");
} else {
button5.setText("O");
}
isFilled5 = true;
// I HAVE TRIED PLACING isWon() HERE
if (playerOneTurn) {
playerOneTurn = false;
} else {
playerOneTurn = true;
}
}
}
});
button6 = board.getButton6();
button6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!isFilled6) {
if (playerOneTurn) {
button6.setText("X");
} else {
button6.setText("O");
}
isFilled6 = true;
// I HAVE TRIED PLACING isWon() HERE
if (playerOneTurn) {
playerOneTurn = false;
} else {
playerOneTurn = true;
}
}
}
});
button7 = board.getButton7();
button7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!isFilled7) {
if (playerOneTurn) {
button7.setText("X");
} else {
button7.setText("O");
}
isFilled7 = true;
// I HAVE TRIED PLACING isWon() HERE
if (playerOneTurn) {
playerOneTurn = false;
} else {
playerOneTurn = true;
}
}
}
});
button8 = board.getButton8();
button8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!isFilled8) {
if (playerOneTurn) {
button8.setText("X");
} else {
button8.setText("O");
}
isFilled8 = true;
// I HAVE TRIED PLACING isWon() HERE
if (playerOneTurn) {
playerOneTurn = false;
} else {
playerOneTurn = true;
}
}
}
});// I HAVE TRIED PLACING isWon() HERE TOO
}
public static boolean isWon(gui board) {
String symbol0, symbol1, symbol2, symbol3, symbol4, symbol5, symbol6, symbol7, symbol8;
symbol0 = board.getButton0().getText();
symbol1 = board.getButton1().getText();
symbol2 = board.getButton2().getText();
symbol3 = board.getButton3().getText();
symbol4 = board.getButton4().getText();
symbol5 = board.getButton5().getText();
symbol6 = board.getButton6().getText();
symbol7 = board.getButton7().getText();
symbol8 = board.getButton8().getText();
if (symbol0.equals(symbol4) && symbol4.equals(symbol8)) //main diagonal
{
gameWon = true;
}
if (symbol2.equals(symbol4) && symbol4.equals(symbol6)) //other diagonal
{
gameWon = true;
}
if (symbol0.equals(symbol1) && symbol1.equals(symbol2)) //first row
{
gameWon = true;
}
if (symbol3.equals(symbol4) && symbol4.equals(symbol5)) //second row
{
gameWon = true;
}
if (symbol6.equals(symbol7) && symbol7.equals(symbol8)) //third row
{
gameWon = true;
}
if (symbol0.equals(symbol3) && symbol3.equals(symbol6)) //first column
{
gameWon = true;
}
if (symbol1.equals(symbol4) && symbol4.equals(symbol7)) //second column
{
gameWon = true;
}
if (symbol2.equals(symbol5) && symbol5.equals(symbol8)) //third column
{
gameWon = true;
}
if (gameWon) {
return true;
} else {
return false;
}
}
public static void initializeBoard(gui board) {
JButton button;
button = board.getButton0();
button.setText("");
button = board.getButton1();
button.setText("");
button = board.getButton2();
button.setText("");
button = board.getButton3();
button.setText("");
button = board.getButton4();
button.setText("");
button = board.getButton5();
button.setText("");
button = board.getButton6();
button.setText("");
button = board.getButton7();
button.setText("");
button = board.getButton8();
button.setText("");
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package tictactoeDivyanshuVarma;
import javax.swing.JButton;
/**
*
* @author divyanshuvarma
*/
public class gui extends javax.swing.JFrame {
/**
* Creates new form gui
*/
public gui() {
initComponents();
}
public void setButton0Text(String symbol) {
this.button0.setText(symbol);
}
public void setButton1Text(String symbol) {
this.button1.setText(symbol);
}
public void setButton2Text(String symbol) {
this.button2.setText(symbol);
}
public void setButton3Text(String symbol) {
this.button3.setText(symbol);
}
public void setButton4Text(String symbol) {
this.button4.setText(symbol);
}
public void setButton5Text(String symbol) {
this.button5.setText(symbol);
}
public void setButton6Text(String symbol) {
this.button6.setText(symbol);
}
public void setButton7Text(String symbol) {
this.button7.setText(symbol);
}
public void setButton8Text(String symbol) {
this.button8.setText(symbol);
}
public JButton getButton0() {
return button0;
}
public JButton getButton1() {
return button1;
}
public JButton getButton2() {
return button2;
}
public JButton getButton3() {
return button3;
}
public JButton getButton4() {
return button4;
}
public JButton getButton5() {
return button5;
}
public JButton getButton6() {
return button6;
}
public JButton getButton7() {
return button7;
}
public JButton getButton8() {
return button8;
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
button1 = new javax.swing.JButton();
button3 = new javax.swing.JButton();
button5 = new javax.swing.JButton();
button2 = new javax.swing.JButton();
button6 = new javax.swing.JButton();
button8 = new javax.swing.JButton();
button4 = new javax.swing.JButton();
button0 = new javax.swing.JButton();
button7 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
button1.setText("jButton1");
button1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
button1ActionPerformed(evt);
}
});
button3.setText("jButton1");
button5.setText("jButton1");
button2.setText("jButton1");
button6.setText("jButton1");
button8.setText("jButton1");
button4.setText("jButton1");
button7.setText("jButton1");
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(button0, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGap(72, 72, 72)
.addComponent(button1))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(button3)
.addGap(72, 72, 72)
.addComponent(button4))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(button6)
.addGap(72, 72, 72)
.addComponent(button7)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 79, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(button8, javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(button5, javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(button2, javax.swing.GroupLayout.Alignment.TRAILING))
.addContainerGap())
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(button0, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(button1, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(button2, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(18, 18, Short.MAX_VALUE)
.addComponent(button8, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(32, 32, 32)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(button3, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(button4, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(button5, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 55, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(button6, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(button7, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addContainerGap())
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
);
pack();
}// </editor-fold>
private void button1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(gui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(gui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(gui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(gui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new gui().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton button0;
private javax.swing.JButton button1;
private javax.swing.JButton button2;
private javax.swing.JButton button3;
private javax.swing.JButton button4;
private javax.swing.JButton button5;
private javax.swing.JButton button6;
private javax.swing.JButton button7;
private javax.swing.JButton button8;
private javax.swing.JPanel jPanel1;
// End of variables declaration
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package tictactoeDivyanshuVarma;
import javax.swing.JLabel;
/**
*
* @author divyanshuvarma
*/
public class winnerDisplay extends javax.swing.JFrame {
/**
* Creates new form winnerDisplay
*/
public winnerDisplay() {
initComponents();
}
public JLabel getWinnerLabel() {
return winnerLabel;
}
public void setWinnerLabel(JLabel winnerLabel) {
this.winnerLabel = winnerLabel;
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
winnerLabel = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
winnerLabel.setText("jLabel1");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(77, 77, 77)
.addComponent(winnerLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 243, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(80, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(82, Short.MAX_VALUE)
.addComponent(winnerLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(94, 94, 94))
);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(winnerDisplay.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(winnerDisplay.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(winnerDisplay.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(winnerDisplay.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new winnerDisplay().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel winnerLabel;
// End of variables declaration
}
代码结构看起来不错,不明白为什么方法ISWIN-没有被调用。在调试中逐步完成代码-以了解更多信息 匿名操作侦听器将对类中定义的静态方法具有可见性
winnerDisplay框架可以从isWon()方法中调用,如果您使用对话框而不是JFrame来发出警报,您将从用户那里得到终止程序的反馈。以下是我的最终代码: (根据用户建议的更改
vbn
)
类gui
与以前一样
更新的main
类:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package tictactoeDivyanshuVarma;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
/**
*
* @author divyanshuvarma
*/
public class Main {
/**
* @param args the command line arguments
*/
static boolean playerOneTurn = true;
static boolean isFilled0 = false, isFilled1 = false, isFilled2 = false;
static boolean isFilled3 = false, isFilled4 = false, isFilled5 = false;
static boolean isFilled6 = false, isFilled7 = false, isFilled8 = false;
static boolean gameWon = false;
static gui board;
static winnerDisplay winnerDisplayWindow;
static int turnNumber = 0;
public static void main(String[] args) {
// TODO code application logic here
board = new gui();
winnerDisplayWindow = new winnerDisplay();
setBoardProperties(board);
initializeBoard(board);
initializeGame(board);
}
public static void setBoardProperties(gui board) {
board.setVisible(true);
board.setTitle("Tic-Tac-Toe (2-player)");
board.setResizable(false);
}
public static void initializeGame(gui board) {
JButton button0, button1, button2, button3, button4, button5, button6, button7, button8;
button0 = board.getButton0();
button0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!isFilled0) {
if (playerOneTurn) {
button0.setText("X");
} else {
button0.setText("O");
}
isFilled0 = true;
isWon(board);
if (playerOneTurn) {
playerOneTurn = false;
} else {
playerOneTurn = true;
}
}
}
});
button1 = board.getButton1();
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!isFilled1) {
if (playerOneTurn) {
button1.setText("X");
} else {
button1.setText("O");
}
turnNumber++;
isFilled1 = true;
isWon(board);
if (playerOneTurn) {
playerOneTurn = false;
} else {
playerOneTurn = true;
}
}
}
});
button2 = board.getButton2();
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!isFilled2) {
if (playerOneTurn) {
button2.setText("X");
} else {
button2.setText("O");
}
turnNumber++;
isFilled2 = true;
isWon(board);
if (playerOneTurn) {
playerOneTurn = false;
} else {
playerOneTurn = true;
}
}
}
});
button3 = board.getButton3();
button3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!isFilled3) {
if (playerOneTurn) {
button3.setText("X");
} else {
button3.setText("O");
}
turnNumber++;
isFilled3 = true;
isWon(board);
if (playerOneTurn) {
playerOneTurn = false;
} else {
playerOneTurn = true;
}
}
}
});
button4 = board.getButton4();
button4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!isFilled4) {
if (playerOneTurn) {
button4.setText("X");
} else {
button4.setText("O");
}
turnNumber++;
isFilled4 = true;
isWon(board);
if (playerOneTurn) {
playerOneTurn = false;
} else {
playerOneTurn = true;
}
}
}
});
button5 = board.getButton5();
button5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!isFilled5) {
if (playerOneTurn) {
button5.setText("X");
} else {
button5.setText("O");
}
turnNumber++;
isFilled5 = true;
isWon(board);
if (playerOneTurn) {
playerOneTurn = false;
} else {
playerOneTurn = true;
}
}
}
});
button6 = board.getButton6();
button6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!isFilled6) {
if (playerOneTurn) {
button6.setText("X");
} else {
button6.setText("O");
}
turnNumber++;
isFilled6 = true;
isWon(board);
if (playerOneTurn) {
playerOneTurn = false;
} else {
playerOneTurn = true;
}
}
}
});
button7 = board.getButton7();
button7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!isFilled7) {
if (playerOneTurn) {
button7.setText("X");
} else {
button7.setText("O");
}
turnNumber++;
isFilled7 = true;
isWon(board);
if (playerOneTurn) {
playerOneTurn = false;
} else {
playerOneTurn = true;
}
}
}
});
button8 = board.getButton8();
button8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!isFilled8) {
if (playerOneTurn) {
button8.setText("X");
} else {
button8.setText("O");
}
turnNumber++;
isFilled8 = true;
isWon(board);
if (playerOneTurn) {
playerOneTurn = false;
} else {
playerOneTurn = true;
}
}
}
});
}
public static void isWon(gui board) {
String symbol0, symbol1, symbol2, symbol3, symbol4, symbol5, symbol6, symbol7, symbol8;
symbol0 = board.getButton0().getText();
symbol1 = board.getButton1().getText();
symbol2 = board.getButton2().getText();
symbol3 = board.getButton3().getText();
symbol4 = board.getButton4().getText();
symbol5 = board.getButton5().getText();
symbol6 = board.getButton6().getText();
symbol7 = board.getButton7().getText();
symbol8 = board.getButton8().getText();
if (turnNumber >= 3) {
if (symbol0.equals(symbol4) && symbol4.equals(symbol8) && !symbol8.equals("")) //main diagonal
{
gameWon = true;
}
if (symbol2.equals(symbol4) && symbol4.equals(symbol6) && !symbol6.equals("")) //other diagonal
{
gameWon = true;
}
if (symbol0.equals(symbol1) && symbol1.equals(symbol2) && !symbol2.equals("")) //first row
{
gameWon = true;
}
if (symbol3.equals(symbol4) && symbol4.equals(symbol5) && !symbol5.equals("")) //second row
{
gameWon = true;
}
if (symbol6.equals(symbol7) && symbol7.equals(symbol8) && !symbol8.equals("")) //third row
{
gameWon = true;
}
if (symbol0.equals(symbol3) && symbol3.equals(symbol6) && !symbol6.equals("")) //first column
{
gameWon = true;
}
if (symbol1.equals(symbol4) && symbol4.equals(symbol7) && !symbol7.equals("")) //second column
{
gameWon = true;
}
if (symbol2.equals(symbol5) && symbol5.equals(symbol8) && !symbol8.equals("")) //third column
{
gameWon = true;
}
if (gameWon) {
//return true;
winnerDisplayWindow.setVisible(true);
winnerDisplayWindow.setTitle("Congrats Winner !");
winnerDisplayWindow.setResizable(false);
winnerDisplayWindow.setAlwaysOnTop(true);
if (playerOneTurn) {
winnerDisplayWindow.setWinnerLabelText("winner is X");
} else {
winnerDisplayWindow.setWinnerLabelText("winner is O");
}
}
}
if (turnNumber == 8 && gameWon == false) {
winnerDisplayWindow.setVisible(true);
winnerDisplayWindow.setTitle("Congrats Both !");
winnerDisplayWindow.setResizable(false);
winnerDisplayWindow.setAlwaysOnTop(true);
winnerDisplayWindow.setWinnerLabelText("It is a tie");
}
}
public static void initializeBoard(gui board) {
JButton button;
button = board.getButton0();
button.setText("");
button = board.getButton1();
button.setText("");
button = board.getButton2();
button.setText("");
button = board.getButton3();
button.setText("");
button = board.getButton4();
button.setText("");
button = board.getButton5();
button.setText("");
button = board.getButton6();
button.setText("");
button = board.getButton7();
button.setText("");
button = board.getButton8();
button.setText("");
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package tictactoeDivyanshuVarma;
import javax.swing.JLabel;
/**
*
* @author divyanshuvarma
*/
public class winnerDisplay extends javax.swing.JFrame {
/**
* Creates new form winnerDisplay
*/
public winnerDisplay() {
initComponents();
}
public JLabel getWinnerLabel() {
return winnerLabel;
}
public void setWinnerLabelText(String winnerLabelText) {
this.winnerLabel.setText(winnerLabelText);
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
winnerLabel = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
winnerLabel.setText("jLabel1");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(77, 77, 77)
.addComponent(winnerLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 243, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(80, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(82, Short.MAX_VALUE)
.addComponent(winnerLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(94, 94, 94))
);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(winnerDisplay.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(winnerDisplay.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(winnerDisplay.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(winnerDisplay.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new winnerDisplay().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel winnerLabel;
// End of variables declaration
}
以下是更新的winnerDisplay
类:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package tictactoeDivyanshuVarma;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
/**
*
* @author divyanshuvarma
*/
public class Main {
/**
* @param args the command line arguments
*/
static boolean playerOneTurn = true;
static boolean isFilled0 = false, isFilled1 = false, isFilled2 = false;
static boolean isFilled3 = false, isFilled4 = false, isFilled5 = false;
static boolean isFilled6 = false, isFilled7 = false, isFilled8 = false;
static boolean gameWon = false;
static gui board;
static winnerDisplay winnerDisplayWindow;
static int turnNumber = 0;
public static void main(String[] args) {
// TODO code application logic here
board = new gui();
winnerDisplayWindow = new winnerDisplay();
setBoardProperties(board);
initializeBoard(board);
initializeGame(board);
}
public static void setBoardProperties(gui board) {
board.setVisible(true);
board.setTitle("Tic-Tac-Toe (2-player)");
board.setResizable(false);
}
public static void initializeGame(gui board) {
JButton button0, button1, button2, button3, button4, button5, button6, button7, button8;
button0 = board.getButton0();
button0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!isFilled0) {
if (playerOneTurn) {
button0.setText("X");
} else {
button0.setText("O");
}
isFilled0 = true;
isWon(board);
if (playerOneTurn) {
playerOneTurn = false;
} else {
playerOneTurn = true;
}
}
}
});
button1 = board.getButton1();
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!isFilled1) {
if (playerOneTurn) {
button1.setText("X");
} else {
button1.setText("O");
}
turnNumber++;
isFilled1 = true;
isWon(board);
if (playerOneTurn) {
playerOneTurn = false;
} else {
playerOneTurn = true;
}
}
}
});
button2 = board.getButton2();
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!isFilled2) {
if (playerOneTurn) {
button2.setText("X");
} else {
button2.setText("O");
}
turnNumber++;
isFilled2 = true;
isWon(board);
if (playerOneTurn) {
playerOneTurn = false;
} else {
playerOneTurn = true;
}
}
}
});
button3 = board.getButton3();
button3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!isFilled3) {
if (playerOneTurn) {
button3.setText("X");
} else {
button3.setText("O");
}
turnNumber++;
isFilled3 = true;
isWon(board);
if (playerOneTurn) {
playerOneTurn = false;
} else {
playerOneTurn = true;
}
}
}
});
button4 = board.getButton4();
button4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!isFilled4) {
if (playerOneTurn) {
button4.setText("X");
} else {
button4.setText("O");
}
turnNumber++;
isFilled4 = true;
isWon(board);
if (playerOneTurn) {
playerOneTurn = false;
} else {
playerOneTurn = true;
}
}
}
});
button5 = board.getButton5();
button5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!isFilled5) {
if (playerOneTurn) {
button5.setText("X");
} else {
button5.setText("O");
}
turnNumber++;
isFilled5 = true;
isWon(board);
if (playerOneTurn) {
playerOneTurn = false;
} else {
playerOneTurn = true;
}
}
}
});
button6 = board.getButton6();
button6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!isFilled6) {
if (playerOneTurn) {
button6.setText("X");
} else {
button6.setText("O");
}
turnNumber++;
isFilled6 = true;
isWon(board);
if (playerOneTurn) {
playerOneTurn = false;
} else {
playerOneTurn = true;
}
}
}
});
button7 = board.getButton7();
button7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!isFilled7) {
if (playerOneTurn) {
button7.setText("X");
} else {
button7.setText("O");
}
turnNumber++;
isFilled7 = true;
isWon(board);
if (playerOneTurn) {
playerOneTurn = false;
} else {
playerOneTurn = true;
}
}
}
});
button8 = board.getButton8();
button8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!isFilled8) {
if (playerOneTurn) {
button8.setText("X");
} else {
button8.setText("O");
}
turnNumber++;
isFilled8 = true;
isWon(board);
if (playerOneTurn) {
playerOneTurn = false;
} else {
playerOneTurn = true;
}
}
}
});
}
public static void isWon(gui board) {
String symbol0, symbol1, symbol2, symbol3, symbol4, symbol5, symbol6, symbol7, symbol8;
symbol0 = board.getButton0().getText();
symbol1 = board.getButton1().getText();
symbol2 = board.getButton2().getText();
symbol3 = board.getButton3().getText();
symbol4 = board.getButton4().getText();
symbol5 = board.getButton5().getText();
symbol6 = board.getButton6().getText();
symbol7 = board.getButton7().getText();
symbol8 = board.getButton8().getText();
if (turnNumber >= 3) {
if (symbol0.equals(symbol4) && symbol4.equals(symbol8) && !symbol8.equals("")) //main diagonal
{
gameWon = true;
}
if (symbol2.equals(symbol4) && symbol4.equals(symbol6) && !symbol6.equals("")) //other diagonal
{
gameWon = true;
}
if (symbol0.equals(symbol1) && symbol1.equals(symbol2) && !symbol2.equals("")) //first row
{
gameWon = true;
}
if (symbol3.equals(symbol4) && symbol4.equals(symbol5) && !symbol5.equals("")) //second row
{
gameWon = true;
}
if (symbol6.equals(symbol7) && symbol7.equals(symbol8) && !symbol8.equals("")) //third row
{
gameWon = true;
}
if (symbol0.equals(symbol3) && symbol3.equals(symbol6) && !symbol6.equals("")) //first column
{
gameWon = true;
}
if (symbol1.equals(symbol4) && symbol4.equals(symbol7) && !symbol7.equals("")) //second column
{
gameWon = true;
}
if (symbol2.equals(symbol5) && symbol5.equals(symbol8) && !symbol8.equals("")) //third column
{
gameWon = true;
}
if (gameWon) {
//return true;
winnerDisplayWindow.setVisible(true);
winnerDisplayWindow.setTitle("Congrats Winner !");
winnerDisplayWindow.setResizable(false);
winnerDisplayWindow.setAlwaysOnTop(true);
if (playerOneTurn) {
winnerDisplayWindow.setWinnerLabelText("winner is X");
} else {
winnerDisplayWindow.setWinnerLabelText("winner is O");
}
}
}
if (turnNumber == 8 && gameWon == false) {
winnerDisplayWindow.setVisible(true);
winnerDisplayWindow.setTitle("Congrats Both !");
winnerDisplayWindow.setResizable(false);
winnerDisplayWindow.setAlwaysOnTop(true);
winnerDisplayWindow.setWinnerLabelText("It is a tie");
}
}
public static void initializeBoard(gui board) {
JButton button;
button = board.getButton0();
button.setText("");
button = board.getButton1();
button.setText("");
button = board.getButton2();
button.setText("");
button = board.getButton3();
button.setText("");
button = board.getButton4();
button.setText("");
button = board.getButton5();
button.setText("");
button = board.getButton6();
button.setText("");
button = board.getButton7();
button.setText("");
button = board.getButton8();
button.setText("");
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package tictactoeDivyanshuVarma;
import javax.swing.JLabel;
/**
*
* @author divyanshuvarma
*/
public class winnerDisplay extends javax.swing.JFrame {
/**
* Creates new form winnerDisplay
*/
public winnerDisplay() {
initComponents();
}
public JLabel getWinnerLabel() {
return winnerLabel;
}
public void setWinnerLabelText(String winnerLabelText) {
this.winnerLabel.setText(winnerLabelText);
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
winnerLabel = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
winnerLabel.setText("jLabel1");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(77, 77, 77)
.addComponent(winnerLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 243, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(80, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(82, Short.MAX_VALUE)
.addComponent(winnerLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(94, 94, 94))
);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(winnerDisplay.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(winnerDisplay.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(winnerDisplay.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(winnerDisplay.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new winnerDisplay().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel winnerLabel;
// End of variables declaration
}
/*
*要更改此许可证标题,请在“项目属性”中选择“许可证标题”。
*要更改此模板文件,请选择工具|模板
*然后在编辑器中打开模板。
*/
Tictactoedivansuvarma包装;
导入javax.swing.JLabel;
/**
*
*@author Divhuvarma
*/
公共类winnerDisplay扩展了javax.swing.JFrame{
/**
*创建新窗体winnerDisplay
*/
公共winnerDisplay(){
初始化组件();
}
公共JLabel getWinnerLabel(){
返回winnerLabel;
}
public void setWinnerLabelText(字符串winnerLabelText){
this.winnerLabel.setText(winnerLabelText);
}
/**
*从构造函数中调用此方法来初始化表单。
*警告:不要修改此代码。此方法的内容始终为
*由表单编辑器重新生成。
*/
@抑制警告(“未选中”)
//
私有组件(){
winnerLabel=newjavax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
winnerLabel.setText(“jLabel1”);
javax.swing.GroupLayout=newjavax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(布局);
layout.setHorizontalGroup(
createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(77,77,77)
.addComponent(winnerLabel,javax.swing.GroupLayout.PREFERRED_SIZE,243,javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(80,简称最大值))
);
layout.setVerticalGroup(
createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,layout.createSequentialGroup()
.addContainerGap(82,简称最大值)
.addComponent(winnerLabel,javax.swing.GroupLayout.PREFERRED_SIZE,124,javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(94,94,94))
);
包装();
}//
/**
*@param指定命令行参数
*/
公共静态void main(字符串参数[]){
/*设置Nimbus的外观和感觉*/
//
/*如果Nimbus(在JavaSE6中引入)不可用,请使用默认的外观。
*详情请参阅http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
试一试{
for(javax.swing.UIManager.LookAndFeelInfo:javax.swing.UIManager.getInstalledLookAndFeels()){
if(“Nimbus”.equals(info.getName())){
setLookAndFeel(info.getClassName());
打破
}
}
}捕获(ClassNotFoundException ex){
getLogger(winnerDisplay.class.getName()).log(java.util.logging.Level.severy,null,ex);
}catch(实例化异常){
getLogger(winnerDisplay.class.getName()).log(java.util.logging.Level.severy,null,ex);
}捕获(非法访问例外){
getLogger(winnerDisplay.class.getName()).log(java.util.logging.Level.severy,null,ex);
}catch(javax.swing.UnsupportedLookAndFeelException ex){
getLogger(winnerDisplay.class.getName()).log(java.util.logging.Level.severy,null,ex);
}
//
/*创建并显示表单*/
invokeLater(new Runnable()){
公开募捐{
新建winnerDisplay().setVisible(true);
}
});
}
//变量声明-不修改
私有javax.swing.JLabel winnerLabel;
//变量结束声明
}
如果我将isWon()放置在