我正在编写一款井字游戏,该游戏在玩家(用户单击按钮)和计算机玩家之间轮流进行。
我有一个名为 playersTurn()
的方法,该方法需要等到单击 JButton
后该方法结束并且计算机播放器开始播放。我读过有关使用线程和 wait - notification
方法的内容,但我是 Java 新手,不知道如何实现它。
我想知道是否有更简单的方法来克服这个问题,或者这是否是唯一的方法,如果是的话,有人可以指导我一个好的教程吗?
谢谢
最佳答案
正如@HFOE所说(对他+1),你不想将Thread
与notify和wait一起使用,这真的让事情变得过于复杂
在我看来,你的游戏逻辑是有偏差的。
开始游戏之前先制定一些框架:
- 9
JButton
(数组将保存按钮) JPanel
使用GridLayout(3,3)
创建井字游戏网格
现在重要的部分来了:
- 玩家开始游戏,然后计算机开始运行,依此类推
考虑到上述内容,请参阅我的示例,我制作的基础知识位于玩家离开后用于每个按钮的 ActionListener
内(已单击按钮并设置玩家符号),我们调用该方法让cpu播放:
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
public class Test {
//declare variables for player and cpu symbol
private String playerSymbol = "X";
private String cpuSymbol = "O";
//used for cpu to select random block
private Random r = new Random();
//create arraylist to hold buttons
ArrayList<JButton> blocks = new ArrayList<>();
//this is the action listner that will be added to each block and will allow player the first turn then cpu goes
private ActionListener al = new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
//players turn
playerTurn(ae);
//check for win after player gone
checkForWin();
//cpu turn
cpuTurn();
//check for a win after cpu goes
checkForWin();
}
private void cpuTurn() {
System.out.println("CPU goes");
while (true) {
int blockNumber = r.nextInt(9);
System.out.println(blockNumber + "");
String tmp = blocks.get(blockNumber).getText();
if (tmp.equals("")) {
blocks.get(blockNumber).setText(cpuSymbol);
break;
}
}
}
private void checkForWin() {
System.out.println("Checking for a win...");
}
private void playerTurn(ActionEvent ae) {
System.out.println("Player goes");
JButton block = (JButton) ae.getSource();
block.setText(playerSymbol);
}
};
public Test() {
initComponents();
}
private void initComponents() {
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel board = new JPanel(new GridLayout(3, 3));//create gridlayput to hold buttons
//create blocks/Jbuttons to hold X and Y
createBlocks(blocks);
//add buttons/blocks to the board
fillBoard(blocks, board);
//add board to JFrame content pane
frame.add(board);
frame.pack();
frame.setVisible(true);
}
private void fillBoard(ArrayList<JButton> blocks, JPanel board) {
for (JButton block : blocks) {
board.add(block);
}
}
private void createBlocks(ArrayList<JButton> blocks) {
for (int i = 0; i < 9; i++) {
//create new button with a size of 50,50
JButton block = new JButton() {
@Override
public Dimension getPreferredSize() {
return new Dimension(50, 50);
}
};
block.addActionListener(al);//add the actionlistner to the button
blocks.add(block);
}
}
public static void main(String[] args) {
//set L&F and create UI on EDT
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
try {//set L&F
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (Exception e) {
// If Nimbus is not available, you can set the GUI to another look and feel.
}
//create UI
new Test();
}
});
}
}
关于java - 等到单击 JButton 后再结束方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13212226/