您好,我正在尝试使用此 ActionListener 和方法重置我的游戏:
newGame.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
reset();
}
});
}
private void reset() {
for(byte row=0; row<8; row++)
{
for(byte col=0; col<8; col++)
{
ImageIcon randomValue = icons[(byte)(Math.random() * icons.length)];
shinyButtons[row][col] = new JButton(randomValue);
shinyButtons[row][col].setLocation(10+col*69, 10+row*69);
shinyButtons[row][col].setSize(69,69);
getContentPane().add(shinyButtons[row][col]);
}
}
}
但是每次我按下按钮时,都没有任何反应,任何关于如何真正让它重置的帮助都会很棒!
如果需要的话,这是我的完整代码:
import javax.swing.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
public class ShinyButtonsApp extends JFrame implements ActionListener
{
private static byte ROWS =8;
ShinyButtons shiny = new ShinyButtons();
public ImageIcon[] icons =
{
new ImageIcon("RedButton.png"),
new ImageIcon("OrangeButton.png"),
new ImageIcon("YellowButton.png"),
new ImageIcon("GreenButton.png"),
new ImageIcon("BlueButton.png"),
new ImageIcon("LightGrayButton.png"),
new ImageIcon("DarkGrayButton.png"),
};
JButton[][] shinyButtons;
public ShinyButtonsApp(String title)
{
super(title);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(578,634);
setResizable(false);
getContentPane().setLayout(null);
shinyButtons = new JButton[8][8];
for(byte row=0; row<8; row++)
{
for(byte col=0; col<8; col++)
{
ImageIcon randomValue = icons[(byte)(Math.random() * icons.length)];
shinyButtons[row][col] = new JButton(randomValue);
shinyButtons[row][col].setLocation(10+col*69, 10+row*69);
shinyButtons[row][col].setSize(69,69);
getContentPane().add(shinyButtons[row][col]);
}
}
JButton red = new JButton(icons[0]);
red.setLocation(200,200);
red.setSize(69,69);
getContentPane().add(red);
//add the NEW GAME button
JButton newGame = new JButton("New Game");
newGame.setLocation(350,570);
newGame.setSize(100, 25);
getContentPane().add(newGame);
//add the QUIT button
JButton quit = new JButton("Quit");
quit.setLocation(460, 570);
quit.setSize(100,25);
getContentPane().add(quit);
//add the SCORE text field
JTextField score = new JTextField();
score.setText(Integer.toString(shiny.score));
score.setEditable(false);
score.setLocation(70, 577);
score.setSize(100,20);
getContentPane().add(score);
//add the SCORE label
JLabel scoreLabel = new JLabel("Score:");
scoreLabel.setLocation(18,537);
scoreLabel.setSize(100,100);
getContentPane().add(scoreLabel);
quit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
newGame.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
reset();
}
});
}
private void reset() {
for(byte row=0; row<8; row++)
{
for(byte col=0; col<8; col++)
{
ImageIcon randomValue = icons[(byte)(Math.random() * icons.length)];
shinyButtons[row][col] = new JButton(randomValue);
shinyButtons[row][col].setLocation(10+col*69, 10+row*69);
shinyButtons[row][col].setSize(69,69);
getContentPane().add(shinyButtons[row][col]);
}
}
}
public void actionPerformed(ActionEvent e)
{
}
public static void main(String args[])
{
ShinyButtonsApp buttons;
buttons = new ShinyButtonsApp("Shiny Buttons");
buttons.setVisible(true);
}
}
最佳答案
将 System.out.println()
添加到您的 Reset() 方法中以查看发生了什么:
private void reset() {
for (byte row = 0; row < 8; row++) {
for (byte col = 0; col < 8; col++) {
ImageIcon randomValue = icons[(byte) (Math.random() * icons.length)];
shinyButtons[row][col] = new JButton(randomValue);
shinyButtons[row][col].setLocation(10 + col * 69, 10 + row * 69);
shinyButtons[row][col].setSize(69, 69);
getContentPane().add(shinyButtons[row][col]);
}
}
System.out.println(getContentPane().getComponentCount());
}
您不会删除之前添加的旧按钮。考虑使用适当的布局管理器,例如 GridLayout。我还会重新考虑您的设计:您不需要删除然后重新添加按钮。将数据状态与用户界面分开存储。
试试这个...创建一个变量来存储游戏状态:
private final ImageIcon[][] data = new ImageIcon[8][8];
在reset()方法中设置游戏状态:
private void reset() { // no need to add the buttons again
for (byte row = 0; row < 8; row++) {
for (byte col = 0; col < 8; col++) {
data[row][col] = icons[(byte) (Math.random() * icons.length)];
shinyButtons[row][col].setIcon(data[row][col]);
}
}
}
首次创建按钮时不要忘记初始化游戏状态:
for (byte row = 0; row < 8; row++) {
for (byte col = 0; col < 8; col++) {
shinyButtons[row][col] = new JButton();
shinyButtons[row][col].setLocation(10 + col * 69, 10 + row * 69);
shinyButtons[row][col].setSize(69, 69);
getContentPane().add(shinyButtons[row][col]);
}
}
reset();
关于java - 按下 JButton 时重置游戏的 ActionListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22261264/