Java 多维数组的ActionListener仅适用于第一个按钮?
我试图创建一个由12x12个JButton组成的网格,然后在每次运行程序时随机分配10个按钮,使其下面有一块“金块”。当你点击一个下面有金块的按钮时,它会将按钮的文本改为“金块”,如果你点击了一个没有金块的按钮,它会将文本改为“Missed!”,我已经设置了网格和按钮,但我不知道ActionListener到底发生了什么,为什么它只会更改第一个按钮(即多维数组中的[1][1]),无论单击哪个按钮,它都会将按钮[1][1]更改为“Missed!”。以下是我的JPanel的代码:Java 多维数组的ActionListener仅适用于第一个按钮?,java,arrays,swing,multidimensional-array,actionlistener,Java,Arrays,Swing,Multidimensional Array,Actionlistener,我试图创建一个由12x12个JButton组成的网格,然后在每次运行程序时随机分配10个按钮,使其下面有一块“金块”。当你点击一个下面有金块的按钮时,它会将按钮的文本改为“金块”,如果你点击了一个没有金块的按钮,它会将文本改为“Missed!”,我已经设置了网格和按钮,但我不知道ActionListener到底发生了什么,为什么它只会更改第一个按钮(即多维数组中的[1][1]),无论单击哪个按钮,它都会将按钮[1][1]更改为“Missed!”。以下是我的JPanel的代码: import ja
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);
}
}
每次按下ActionListener时,您似乎都在重新随机化,这听起来不像您的需求所要求的 相反,您应该在构造函数中而不是ActionListener中分配一次随机按钮。也许可以创建一个包含所有选中的掘金按钮的
列表
,然后在ActionListener中,如果源JButton由列表持有(if nuggetButtonList.contains(sourceButton)
),则向用户显示它是一个掘金按钮
例如,考虑给你的程序<强> 2<强> >代码>列表< /> >:
非常感谢,这很有道理。我试图改变它,但它似乎仍然不起作用——但我不确定为什么。@Sarah:
“它似乎仍然不起作用”
没有提供足够的信息让我们能够提供帮助。你的代码现在到底在做什么?@Sarah:在尝试随机或添加到列表之前,一定要创建你的JButtons。否则,您只会在列表中添加空值!它所做的是提供相同的输出。它没有给出任何错误,但除了第一个按钮外,所有按钮都不会更改文本。第一个将改为“Missed!”谢谢,我修复了一个问题,它只是空值。除了第一个按钮,它仍然没有改变任何东西。
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);
}
}
List<JButton> allButtons = new ArrayList<>();
List<JButton> selectedButtons = new ArrayList<>();
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));
}
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);
}
}