java - 在 Swing UI 线程中放置另一种方法的位置
问题描述
我正在使用 Swing 用 Java 制作 2 人井字游戏。UI 按预期工作,但我无法让它检查获胜者。
我做了三个类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("");
}
}
这是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 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
}
这是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 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
}
解决方案
代码构造看起来不错,不明白为什么方法 isWon - 没有被调用。在调试中单步执行您的代码 - 以了解更多信息。
匿名动作侦听器将对类中定义的静态方法具有可见性。
可以从 isWon() 方法中调用获胜者显示框架,如果您使用对话框而不是 JFrame 来发出警报 - 您将从用户那里获得终止程序的反馈。
推荐阅读
- python - 如何修复 'TypeError: unhashable type: 'list' 错误?
- java - 如何在 Java 中使用 POST/GET http 方法发送/获取数据 - REST API
- angular - 使用响应式表单和 ChangeDetectionStrategy.OnPush 显示验证消息
- firebase - 如何在 Web CMS 中隐藏文档字段,但在 android 和 iOS 中不隐藏
- javascript - Electron 中的 renderer.js 有什么特别之处吗?
- javascript - 将数组值转换为对象,根据索引位置添加键
- ruby - 为什么用于在本地选择文件的相同命令在 CircleCI 中不起作用?
- graphql - 基于字段特定值的 GraphQL 查询
- python - 是否可以使用 Python 绘制 R glmer 模型预测?
- docker - couchbase docker 端口 8091 有 301 重定向