gpt4 book ai didi

java - 如何阻止 ActionListener 停止所有其他代码?

转载 作者:行者123 更新时间:2023-12-04 17:59:54 25 4
gpt4 key购买 nike

我问了this昨天的问题,并试图实现我收到的最佳答案。因此,根据昨天的代码,我尝试将 synchronized 添加到我的方法中,并使用 wait()notifyAll()。我一直在查找大量示例并阅读文档,但我绝对没有做对。

基本上,一旦发生 touchEvent,我的 ButtonListener 就会停止执行所有其他代码,并且只执行包含在 ButtonListener 中的代码。 这只发生在我的一台电脑上,我的笔记本电脑按照我预期的方式运行代码,我的桌面卡在 ButtonListener 中。这是我的学校作业几周前上交,最初得到了 70%,但我向我的老师解释说它可以在某些计算机上运行,​​他在他的办公室桌面上运行它,而 viola,它运行良好,我得到了 100%。当然,我想弄清楚问题出在哪里,所以这就是我仍在努力解决这个问题的原因。

以下是有问题的代码片段:

    public synchronized void playOneTurn(int player)
throws InterruptedException {
waiting = true;
while (waiting) {
try {
System.out.println("playOneTurn before wait.");
wait();
} catch (InterruptedException e) {
// e.printStackTrace();
}
}
System.out.println("playOneTurn AFTER wait.");
}

我这里的方法 playOneTurn 是在第一个触摸事件之前运行的最后一段代码。以前,通过查看我在顶部链接的问题可以看出,我使用了一个简单的 while 循环等待我的 ButtonListener 翻转一个 boolean 值,waiting 。以上是我尝试使用synchronized

    class ButtonListener implements ActionListener {
@Override
public synchronized void actionPerformed(ActionEvent e) {
System.out.println("Entering Action Performed");
for (i = 0; i < size; i++) {
for (j = 0; j < size; j++) {
if (e.getSource() == cells[i][j]) {
if (model[i][j] == 'e') {
cells[i][j].setText("");
currentSpot[0] = i;
currentSpot[1] = j;
if (count % 2 == 0) {
cells[i][j].setBackground(Color.GREEN);
cells[i][j].setIcon(X_MARK);
model[i][j] = 'x';
count++;
waiting = false;
System.out.println("Boolean hit");
notifyAll();
} else {
cells[i][j].setBackground(Color.CYAN);
cells[i][j].setIcon(O_MARK);
model[i][j] = 'o';
count++;
waiting = false;
System.out.println("Boolean hit");
notifyAll();
}

} else {
System.out
.println("Hey, you can't move there silly!");
}
}
}
}
}
}

这是我的 ButtonListener,我的程序在其中停止执行任何其他操作。请无视我随处可见的随机 println,我只是越来越生气并试图找出这个东西在做什么。

我还必须将这个 try/catch block 添加到我的 Controller 类中:

        while (!game.haveWinner() && !game.isFull()) {
player = (player + 1) % 2;
try {
game.playOneTurn(player);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("While loop looped");
}

我一直在尝试我能想到的所有方法来正确实现 synchronized,但显然我做错了什么。

Here is a Dropbox link to the entire program if it would help.

最佳答案

这个:

while (!game.haveWinner() && !game.isFull()) {

将占用 Swing 事件线程并卡住您的整个应用程序....不要这样做。

您的代码看起来像是在尝试将线性控制台程序改写成 Swing GUI,但这永远行不通,因为它们的程序流程和逻辑完全不同。解决方案是将您的逻辑更改为更加事件驱动。

您可能在某处有一个游戏循环,也许使用了 Swing Timer ...因此检查循环的每次迭代是否为赢家或是否已满。

你需要让所有的等待,所有的同步,所有的通知都离开你的程序。而是按下按钮来改变它的状态。


编辑
我在玩你的代码,想出了这样的东西。请注意,同步不是我的强项,所以对此持保留态度,但我的主要目标是确保仅在 Swing 事件线程上调用 Swing GUI 创建代码和状态更改代码,以及任何其他代码,特别是需要与其他线程同步的代码,不会在 Swing 事件线程上调用。

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;
import javax.swing.*;

public class TicTacToeApp {
public static void main(String[] args) {
TicTacToeGame game;
int size, need, player = 1;
String[] names = new String[2];
Scanner kbd = new Scanner(System.in);

// TODO: uncomment in running code
// System.out.print("Enter Player 1's name: ");
// names[0] = kbd.nextLine();
// System.out.print("Enter Player 2's name: ");
// names[1] = kbd.nextLine();
//
// System.out.print("Enter the TIC-TAC-TOE grid size: ");
// size = kbd.nextInt();
// System.out.print("Enter how many in a row you need to win: ");
// need = kbd.nextInt();
// System.out.println();

// TODO: For test purposes only. Delete in running code
size = 3;
need = 3;
names[0] = "Foo";
names[1] = "Bar";

game = new TicTacToeGame(size, need, names);

while (!game.haveWinner() && !game.isFull()) {
player = (player + 1) % 2;
try {
game.playOneTurn(player);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("While loop looped");
}

if (game.haveWinner())
System.out.println(names[player] + " is the winner!");
else
System.out.println("It's a TIE!");

System.out.println("\nBye!");

}
}

@SuppressWarnings("serial")
class TicTacToeGame extends JPanel {
private static final Object LOCK = new Object();
private volatile int player = 0;
private int size;
private int need;
private String[] names;
private JLabel nameLabel = new JLabel();
// private JButton testButton = new JButton();
private JButton[][] buttonGrid;
private volatile boolean waiting = false;

public TicTacToeGame(int size, int need, String[] names) {
this.size = size;
this.need = need;
this.names = names;

nameLabel.setText(names[0]);

JPanel topPanel = new JPanel();
topPanel.add(new JLabel("Player:"));
topPanel.add(nameLabel);

buttonGrid = new JButton[size][size];
ButtonListener actionListener = new ButtonListener(this);
JPanel middlePanel = new JPanel(new GridLayout(size, size));
for (int row = 0; row < size; row++) {
for (int col = 0; col < size; col++) {
JButton button = new JButton(" ");
middlePanel.add(button);
buttonGrid[row][col] = button;
button.addActionListener(actionListener);
}
}

setLayout(new BorderLayout());
add(topPanel, BorderLayout.NORTH);
add(middlePanel, BorderLayout.CENTER);

// run GUI on Swing event thread
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(TicTacToeGame.this);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public int getPlayer() {
return player;
}

public synchronized void playOneTurn(final int player)
throws InterruptedException {
this.player = player;
System.out.printf("Player %d before wait%n", player);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
nameLabel.setText(names[player]);
}
});
synchronized (LOCK) {
waiting = true;
while (waiting) {
LOCK.wait();
}
}
}

public boolean isFull() {
for (int row = 0; row < size; row++) {
for (int col = 0; col < size; col++) {
if (buttonGrid[row][col].isEnabled()) {
return false;
}
}
}
return true;
}

public boolean haveWinner() {
// TODO finish this
return false;
}

public void doNotification() {
new Thread(new Runnable() {
public void run() {
synchronized (LOCK) {
waiting = false;
LOCK.notifyAll();
}
}
}).start();
}

public void tttButtonPressed(ActionEvent e) {
AbstractButton source = (AbstractButton) e.getSource();
for (int r = 0; r < size; r++) {
for (int c = 0; c < size; c++) {
if (buttonGrid[r][c] == source) {
String text = player == 0 ? "X" : "0";
source.setText(text);
source.setEnabled(false);
}
}
}
doNotification();
}

}

class ButtonListener implements ActionListener {
private TicTacToeGame ticTacToeGame;

public ButtonListener(TicTacToeGame ticTacToeGame) {
this.ticTacToeGame = ticTacToeGame;
}

public void actionPerformed(ActionEvent e) {
ticTacToeGame.tttButtonPressed(e);
};
}

关于java - 如何阻止 ActionListener 停止所有其他代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21899105/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com