Java 如何重置GUI和游戏?
我正在制作一个游戏,希望在游戏结束并单击“休息”按钮后重置棋盘。目前游戏在第一次运行时表现良好 我认为问题出在重置方法“this.play()”中。有人能解释一下我打电话时发生了什么事吗 使用“this.play()”GUI不会重置,游戏也不会玩。没有“this.play()”GUI将重置,但游戏无法运行 另外,我希望在每次单击按钮后运行check方法,而不是循环。我不断得到错误,非静态方法不能从静态上下文引用。我一直在buttons类的actionPerformed方法中使用check方法 这是我的密码 板级Java 如何重置GUI和游戏?,java,swing,oop,user-interface,jbutton,Java,Swing,Oop,User Interface,Jbutton,我正在制作一个游戏,希望在游戏结束并单击“休息”按钮后重置棋盘。目前游戏在第一次运行时表现良好 我认为问题出在重置方法“this.play()”中。有人能解释一下我打电话时发生了什么事吗 使用“this.play()”GUI不会重置,游戏也不会玩。没有“this.play()”GUI将重置,但游戏无法运行 另外,我希望在每次单击按钮后运行check方法,而不是循环。我不断得到错误,非静态方法不能从静态上下文引用。我一直在buttons类的actionPerformed方法中使用check方法 这
public class ticTacBoard extends JFrame implements ActionListener
{
Toebuttons toe[] = new Toebuttons[9];
JFrame over = new JFrame("Game Over");
JPanel panel = new JPanel();
JLabel winner = new JLabel("");
static boolean win = false;
public static void start()
{
ticTacBoard b = new ticTacBoard();
b.play();
}
public void play()
{
//ticTacBoard one = new ticTacBoard();
while(!win && Toebuttons.count < 9)
{
this.check();
}
endFrame();
}
public ticTacBoard()
{
super("Tic tac board");
toFront();
setSize(500,500);
setLayout(new GridLayout(3,3));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
for(int i = 0; i<toe.length; i++)
{
toe[i] = new Toebuttons();
add(toe[i]);
}
setVisible(true);
}
public void check()
{
checkRow();
checkDiagonal();
checkColumn();
}
public void checkRow()
{
if((toe[0].getText().equals("X")||toe[0].getText().equals("O"))&&(toe[0].getText().equals(toe[1].getText()) && toe[1].getText().equals(toe[2].getText())))
{
winner.setText(toe[2].getText()+" WINS!!!\nROW");
win = true;
}
if((toe[3].getText().equals("X")||toe[3].getText().equals("O"))&&(toe[3].getText().equals(toe[4].getText()) && toe[4].getText().equals(toe[5].getText())))
{
winner.setText(toe[3].getText()+" WINS!!!\nROW");
win = true;
}
if((toe[6].getText().equals("X")||toe[6].getText().equals("O"))&&(toe[6].getText().equals(toe[7].getText()) && toe[7].getText().equals(toe[8].getText())))
{
winner.setText(toe[6].getText()+" WINS!!!\nROW");
win = true;
}
}
public void checkDiagonal()
{
if((toe[0].getText().equals("X")||toe[0].getText().equals("O"))&&(toe[0].getText().equals(toe[4].getText()) && toe[4].getText().equals(toe[8].getText())))
{
winner.setText(toe[0].getText()+" WINS!!!\nDIAGONAL");
win = true;
}
if((toe[2].getText().equals("X")||toe[2].getText().equals("O"))&&(toe[2].getText().equals(toe[4].getText()) && toe[4].getText().equals(toe[6].getText())))
{
winner.setText(toe[0].getText()+" WINS!!!\nDIAGONAL");
win = true;
}
}
public void checkColumn()
{
if((toe[0].getText().equals("X")||toe[0].getText().equals("O"))&&(toe[0].getText().equals(toe[3].getText()) && toe[3].getText().equals(toe[6].getText())))
{
winner.setText(toe[0].getText()+" WINS!!!\nCOLUMN");
win = true;
}
if((toe[1].getText().equals("X")||toe[1].getText().equals("O"))&&(toe[1].getText().equals(toe[4].getText()) && toe[4].getText().equals(toe[7].getText())))
{
winner.setText(toe[1].getText()+" WINS!!!\nCOLUMN");
win = true;
}
if((toe[2].getText().equals("X")||toe[2].getText().equals("O"))&&(toe[2].getText().equals(toe[5].getText()) && toe[5].getText().equals(toe[8].getText())))
{
winner.setText(toe[2].getText()+" WINS!!!\nCOLUMN");
win = true;
}
}
public void endFrame()
{
System.out.println("2222");
over.setLocationRelativeTo(null);
panel.setLayout(new FlowLayout());
over.setLayout(new FlowLayout());
panel.add(winner);
panel.repaint();
over.add(panel);
over.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
over.setSize(500,100);
JButton r = new JButton("Reset");
JPanel p = new JPanel();
p.add(r);
over.add(p);
r.addActionListener(this);
over.repaint();
over.setVisible(true);
}
public void reset()
{
Toebuttons.x = true;
for(int i = 0; i<toe.length;i++)
{
toe[i].setText("blank");
}
win = false;
Toebuttons.count = 0;
this.play();
}
public void actionPerformed(ActionEvent e)
{
over.hide();
reset();
}
}
使用“this.play()”GUI不会重置,游戏也不会玩。没有“this.play()”GUI将重置,但游戏无法运行
问题是,您正在阻止事件调度线程
public void play()
{
//ticTacBoard one = new ticTacBoard();
while(!win && Toebuttons.count < 9)
{
this.check();
}
endFrame();
}
我想通过调用一个静态方法来启动整个程序。可能吗?为什么?你会得到什么好处?此外,一旦你摆脱了
play
,你可以继续调用static
方法,直到奶牛回家play
不是静态的,start
是静态的。但是,既然你已经重置了当前棋盘的状态,我不认为再次调用它有什么意义,你已经重置了UI/游戏的状态,只需继续使用你拥有的当前实例-否则,reset
方法的整个概念毫无意义,只需创建一个新的板实例并处理当前的板实例即可:/I将play方法中的代码复制并粘贴到静态方法start()中,并调用start,但这是相同的错误。如何处置一个板实例并创建另一个?我喜欢这样idea@HovercraftFullOfEels似乎OP不喜欢听到“不要按你现在的方式做,你需要改变”:/-有水,不能强迫他们喝水。。。想淹死他们
public void play()
{
//ticTacBoard one = new ticTacBoard();
while(!win && Toebuttons.count < 9)
{
this.check();
}
endFrame();
}
import java.awt.CardLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class TicTac {
public static void main(String[] args) {
new TicTac();
}
public TicTac() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.add(new TicTacToePane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public enum Player {
X, O, NONE;
}
public interface TicTacToeModelListener {
public void ticTacToeGameWon(TicTacToeModel model);
}
public class TicTacToeModel {
private Player[] board;
private Player turn;
private Player winner;
private List<TicTacToeModelListener> listeners;
public TicTacToeModel() {
board = new Player[3 * 3];
listeners = new ArrayList<>(25);
reset();
}
public boolean isX() {
return turn == Player.X;
}
public void nextTurn() {
if (isX()) {
turn = Player.O;
} else {
turn = Player.X;
}
}
public Player getTurn() {
return turn;
}
public Player getWinner() {
return winner;
}
public void reset() {
for (int index = 0; index < board.length; index++) {
board[index] = Player.NONE;
}
turn = Player.X;
winner = Player.NONE;
}
public void set(int col, int row) {
int index = (row * 3) + col;
if (board[index] == Player.NONE) {
board[index] = turn;
} else {
System.out.println("!! Spot already occupied");
}
check();
}
public void check() {
checkRow();
checkDiagonal();
checkColumn();
if (winner != Player.NONE) {
fireGameWon();
}
}
public void addModelListener(TicTacToeModelListener listener) {
listeners.add(listener);
}
public void removeModelListener(TicTacToeModelListener listener) {
listeners.remove(listener);
}
protected void fireGameWon() {
for (TicTacToeModelListener listener : listeners) {
listener.ticTacToeGameWon(this);
}
}
public void checkRow() {
if ((board[0] == Player.X || board[0] == Player.O) && (board[0] == board[1] && board[1] == board[2])) {
winner = turn;
}
if ((board[3] == Player.X || board[3] == Player.O) && (board[3] == board[4] && board[4] == board[5])) {
winner = turn;
}
if ((board[6] == Player.X || board[6] == Player.O) && (board[6] == board[7] && board[7] == board[8])) {
winner = turn;
}
}
public void checkDiagonal() {
if ((board[0] == Player.X || board[0] == Player.O) && (board[0] == (board[4]) && board[4] == (board[8]))) {
winner = turn;
}
if ((board[2] == Player.X || board[2] == Player.O) && (board[2] == (board[4]) && board[4] == (board[6]))) {
winner = turn;
}
}
public void checkColumn() {
if ((board[0] == Player.X || board[0] == Player.O) && (board[0] == (board[3]) && board[3] == (board[6]))) {
winner = turn;
}
if ((board[1] == Player.X || board[1] == Player.O) && (board[1] == (board[4]) && board[4] == (board[7]))) {
winner = turn;
}
if ((board[2] == Player.X || board[2] == Player.O) && (board[2] == (board[5]) && board[5] == (board[8]))) {
winner = turn;
}
}
}
public class TicTacToePane extends JPanel {
private TicTacToeModel model;
private GamePane gamePane;
private WinPane winPane;
public TicTacToePane() {
CardLayout cardLayout = new CardLayout();
model = new TicTacToeModel();
model.addModelListener(new TicTacToeModelListener() {
@Override
public void ticTacToeGameWon(TicTacToeModel model) {
winPane.setWinner(model.getWinner());
cardLayout.show(TicTacToePane.this, "GameOverMan");
}
});
winPane = new WinPane();
gamePane = new GamePane(model);
winPane.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals(GameActions.PLAY_AGAIN.getCommand())) {
gamePane.reset();
cardLayout.show(TicTacToePane.this, "ReadyPlayer");
} else if (e.getActionCommand().equals(GameActions.STOP_PLAYING.getCommand())) {
SwingUtilities.windowForComponent(TicTacToePane.this).dispose();
}
}
});
setLayout(cardLayout);
add(winPane, "GameOverMan");
add(gamePane, "ReadyPlayer");
cardLayout.show(this, "ReadyPlayer");
System.out.println("...");
}
}
public class GamePane extends JPanel {
private JButton board[];
private TicTacToeModel model;
public GamePane(TicTacToeModel model) {
this.model = model;
setLayout(new GridLayout(3, 3));
board = new JButton[9];
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
int index = (row * 3) + col;
System.out.println(index);
board[index] = new JButton("-"); // Icon might be better
board[index].addActionListener(new ButtonActionListener(col, row));
add(board[index]);
}
}
reset();
}
public void reset() {
model.reset();
for (int index = 0; index < board.length; index++) {
board[index].setText("-");
}
}
protected class ButtonActionListener implements ActionListener {
private int col;
private int row;
public ButtonActionListener(int col, int row) {
this.col = col;
this.row = row;
}
@Override
public void actionPerformed(ActionEvent e) {
// Normally I'd use instanceof to check the source, but
// I've deliberly limited the possible scope.
JButton btn = (JButton) e.getSource();
btn.setText(model.isX() ? "X" : "0");
model.set(col, row);
model.nextTurn();
}
}
}
public enum GameActions {
PLAY_AGAIN("playAgain"), STOP_PLAYING("stopPlaying");
private String command;
private GameActions(String command) {
this.command = command;
}
public String getCommand() {
return command;
}
}
public class WinPane extends JPanel {
private JLabel winner;
private JButton playAgain;
private JButton end;
public WinPane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.insets = new Insets(8, 8, 8, 8);
winner = new JLabel("");
playAgain = new JButton("Play Again");
playAgain.setActionCommand(GameActions.PLAY_AGAIN.getCommand());
end = new JButton("Stop Playing");
end.setActionCommand(GameActions.STOP_PLAYING.getCommand());
add(new JLabel("Game Over!"), gbc);
add(new JLabel("Player"), gbc);
add(winner, gbc);
add(new JLabel("Wins!"), gbc);
JPanel buttons = new JPanel(new GridLayout(1, 0));
buttons.add(playAgain);
buttons.add(end);
add(buttons, gbc);
}
public void setWinner(Player player) {
if (player == Player.X) {
winner.setText("X");
} else if (player == Player.O) {
winner.setText("0");
} else {
winner.setText("No boby knows");
}
}
public void addActionListener(ActionListener listener) {
playAgain.addActionListener(listener);
end.addActionListener(listener);
}
public void removeActionListener(ActionListener listener) {
playAgain.removeActionListener(listener);
end.removeActionListener(listener);
}
}
}