gpt4 book ai didi

java - 无法将面板重置为初始状态

转载 作者:行者123 更新时间:2023-11-30 06:30:18 24 4
gpt4 key购买 nike

Play Again 按钮有问题,该按钮会将面板重置为 initial 状态。

Play Again 按钮应该重置面板,它会重新调整 gameList,但不会重置面板,这意味着所有按钮都会保留并丢失 ActionListener 函数。

这是我的程序:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import static java.util.Collections.*;

public class MemoryGame extends JFrame
{
private JButton exitButton, replayButton;
private JButton[] gameButton = new JButton[16];
private ArrayList<Integer> gameList = new ArrayList<Integer>();
private int counter = 0;
private int[] buttonID = new int[2];
private int[] buttonValue = new int[2];

public static Point getCenterPosition(int frameWidth, int frameHeight)
{
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension dimension = toolkit.getScreenSize();

int x = (dimension.width - frameWidth)/2;
int y = (dimension.height - frameHeight)/2;
return (new Point(x,y));
}

public MemoryGame(String title)
{
super(title);

initial();
}

public void initial()
{
for (int i = 0; i < gameButton.length; i++)
{
gameButton[i] = new JButton();
gameButton[i].setFont(new Font("Serif", Font.BOLD, 28));
gameButton[i].addActionListener(new ButtonListener());
}
exitButton = new JButton("Exit");
replayButton = new JButton("Play Again");
exitButton.addActionListener(new ButtonListener());
replayButton.addActionListener(new ButtonListener());

Panel gamePanel = new Panel();
gamePanel.setLayout(new GridLayout(4, 4));
for (int i = 0; i < gameButton.length; i++)
{
gamePanel.add(gameButton[i]);
}

Panel buttonPanel = new Panel();
buttonPanel.add(replayButton);
buttonPanel.add(exitButton);
buttonPanel.setLayout(new GridLayout(1, 0));

add(gamePanel, BorderLayout.CENTER);
add(buttonPanel, BorderLayout.SOUTH);

for (int i = 0; i < 2; i++)
{
for (int j = 1; j < (gameButton.length / 2) + 1; j++)
{
gameList.add(j);
}
}
shuffle(gameList);

int newLine = 0;
for (int a = 0; a < gameList.size(); a++)
{
newLine++;
System.out.print(" " + gameList.get(a));
if (newLine == 4)
{
System.out.println();
newLine = 0;
}
}
}

public boolean sameValues()
{
if (buttonValue[0] == buttonValue[1])
{
return true;
}
return false;
}

private class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if (exitButton == e.getSource())
{
System.exit(0);
}
else if (replayButton == e.getSource())
{
initial();
}
for (int i = 0; i < gameButton.length; i++)
{
if (gameButton[i] == e.getSource())
{
gameButton[i].setText("" + gameList.get(i));
gameButton[i].setEnabled(false);
counter++;

if (counter == 3)
{
if (sameValues())
{
gameButton[buttonID[0]].setEnabled(false);
gameButton[buttonID[1]].setEnabled(false);
}
else
{
gameButton[buttonID[0]].setEnabled(true);
gameButton[buttonID[0]].setText("");
gameButton[buttonID[1]].setEnabled(true);
gameButton[buttonID[1]].setText("");
}
counter = 1;
}
if (counter == 1)
{
buttonID[0] = i;
buttonValue[0] = gameList.get(i);
}
if (counter == 2)
{
buttonID[1] = i;
buttonValue[1] = gameList.get(i);
}
}
}
}
}

public static void main(String[] args)
{
int x, y;
int width = 500;
int height = 500;

Point position = getCenterPosition(width, height);
x = position.x;
y = position.y;

JFrame frame = new MemoryGame("Memory Game");
frame.setBounds(x, y, width, height);

frame.setVisible(true);
frame.setResizable(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

我该如何解决这个问题?

编辑:

另一个问题是在reset()之后,所有的按钮看起来都变成了sameValue()。我向按钮插入了一些音频,它发生在 ButtonListener 的情况下。这是部分:

if (counter == 3) 
{
if (sameValues())
{
gameButton[buttonID[0]].setEnabled(false);
gameButton[buttonID[1]].setEnabled(false);
}

似乎满足条件sameValue(),但为什么呢?

最新更新:

问题已解决。最新代码未上传。

最佳答案

initial() 方法中,代码创建了一个新的 GUI。最好保留原始控件的句柄,并将它们重置为“开始游戏”状态和正确的值(数字)。

您当前方法的替代方法是为每个组件添加 remove()add() new(不推荐)或使用 CardLayout(没有必要 - 请参阅第一个建议)。

关于java - 无法将面板重置为初始状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10565053/

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