我正在尝试创建一个12x12的JButtons网格,然后随机分配十个按钮以获得一个"金块"在下面,每次运行程序。当您单击下面有一个金块的按钮时,它应该将按钮的文本更改为" NUGGET,"如果你点击没有金块的那个,它应该将文本改为" Missed!"我已经设置了网格和按钮,但是我无法弄清楚ActionListener发生了什么,以及它为什么只更改第一个按钮(这将是[1] [1] ]在多维数组中)无论你点击什么按钮,它只会将按钮[1] [1]改为"错过!"这是我的JPanel的代码:
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.*;
public class Panel extends JPanel{
private JButton[][]buttons;
private final int size = 12;
private GridLayout experimentLayout;
private int clicked;
private int k = 0;
private int j = 0;
private int max = 11;
private int min = 0;
private int randomNum1;
private int randomNum2;
public Random rand = new Random();
public Panel(){
experimentLayout = new GridLayout(size,size);
setLayout(experimentLayout);
buttons = new JButton[size][size];
for (int k = 0; k < size; k++) {
for (int j = 0; j < size; j++) {
buttons[k][j] = new JButton("o");
add(buttons[k][j]);
}
}
ArrayList<JButton> nuggetButtonList = new ArrayList<JButton>();
for(int i = 0; i < 10; i++){
randomNum1 = rand.nextInt((max - min) + 1) + min;
randomNum2 = rand.nextInt((max - min) + 1) + min;
nuggetButtonList.add((JButton)buttons[randomNum1][randomNum2]);
System.out.println(nuggetButtonList);
}
ActionListener listener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(nuggetButtonList.contains((JButton)e.getSource())){
buttons[k][j].setText("NUGGET.");
} else {
buttons[k][j].setText("Missed!");
}
}
};
buttons[k][j].addActionListener(listener);
}
}
驱动程序的代码:
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JFrame;
public class Driver {
public static void main(String[] args) {
JFrame myFrame = new JFrame("Dig for nuggets~!");
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Panel nuggetGraphics = new Panel();
nuggetGraphics.setPreferredSize(new Dimension(1500, 1000));
myFrame.getContentPane().add(nuggetGraphics, BorderLayout.CENTER);
myFrame.pack();
myFrame.setVisible(true);
}
}
答案 0 :(得分:2)
每次按下ActionListener时,您似乎都在重新随机化事物,这听起来不像您的要求所要求的那样。
相反,您应该在构造函数中分配随机按钮一次,而不是在ActionListener中。也许创建一个List<JButton>
来保存所有选择的金块按钮,然后在ActionListener中,如果源JButton由列表(if nuggetButtonList.contains(sourceButton)
)保存,则向用户显示它是一个Nugget按钮
例如,考虑为您的计划提供两个 List<JButton>
:
List<JButton> allButtons = new ArrayList<>();
List<JButton> selectedButtons = new ArrayList<>();
第一个按住所有按钮,第二个按下所选按钮。
在构造函数中,在创建按钮时填充allButtons列表,并将ActionListener添加到每个按钮。然后随机播放allButtons列表并使用随机列表填充所选按钮列表。如,
MyListener myListener = new MyListener();
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
JButton button = new JButton(DEFAULT);
button.addActionListener(myListener);
add(button);
allButtons.add(button);
}
}
Collections.shuffle(allButtons);
for (int i = 0; i < NUGGET_COUNT; i++) {
selectedButtons.add(allButtons.get(i));
}
然后在你的ActionListener中,只需检查按钮是否在所选列表中:
public void actionPerformed(ActionEvent e) {
JButton source = (JButton) e.getSource();
if (selectedButtons.contains(source)) {
source.setText(NUGGET);
source.setBackground(Color.yellow);
} else {
source.setText(MISSED);
}
}