java - 按下 JButton 时重置游戏的 ActionListener

标签 java arrays swing user-interface actionlistener

您好,我正在尝试使用此 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/

相关文章:

java - [亚马逊](500310) 无效操作 : syntax error at or near "S" RedShift Error

java - 带有安装程序/激活程序的 NetBeans 平台模块。如何强制从对话框发出通知

java - 使用自定义 TreeCellRenderer(Java、Swing)在 JTree 中拖放反馈

java - java框架对象的问题

java - JDialog 上的 stopCellEditing

Java : HashSet vs. 哈希表

java - 使用 JInitator 的 Internet Explorer 8 中的 Oracle Forms/Applications

python - 为什么我的继承自 array.array 的类不起作用?

arrays - 如何返回数组中的多个值 - Excel VBA

arrays - Excel VBA 对象变量未设置问题,可能不正确的 Redim 使用?