您好我正在尝试使用此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);
}
}
答案 0 :(得分:4)
在您的reset()方法中添加System.out.println()
以查看正在进行的操作:
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());
}
您没有删除之前添加的旧按钮。考虑使用适当的LayoutManager,例如GridLayout。我还会重新考虑你的设计:你不需要删除然后重新添加按钮。将数据状态与UI分开存储。
试试这个......创建一个变量来存储游戏状态:
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();