[英]Array and action listener java
真的感谢最后一个问题的帮助,但是仍然有一些错误。 我正在制作一个寻宝游戏,用户单击gui尝试显示藏宝箱的位置。 如果找到了位置,但我使用动作侦听器在按钮上显示宝箱的图像,但这是固定的位置,因此我想将其随机化。 有一些建议在按钮和随机数生成器上使用数组,然后使用if / else进行检查。 出现编译器错误,我将在下面的代码中进行注释。 好的编码员可能会在几秒钟内发现我的新手错误!
import java.awt.*;
import javax.swing.*;
import java.util.Random;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Test extends JFrame {
JLabel label1, label2, label3;
ImageIcon image1, image2, image3, image4, image5;
JTextField textResult;
public static void main(String[] args) {
new Test();
}
public Test (){
this.setSize(700,700);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("Treasure Hunt Game");
JPanel thePanel = new JPanel();
thePanel.setLayout(new GridLayout(0,3,0,0));
image1 = new ImageIcon(getClass().getResource("Treasure.jpg"));
image2 = new ImageIcon(getClass().getResource("Pirate.jpg"));
image3 = new ImageIcon(getClass().getResource("sand2.jpg"));
image4 = new ImageIcon(getClass().getResource("emptyhole.jpg"));
image5 = new ImageIcon(getClass().getResource("map.jpg"));
label1 = new JLabel("Click the buttons to find the Treasure!");
label2 = new JLabel(image5);
label3 = new JLabel(image2);
JButton [] buttons = new JButton[9];
buttons[0] = new JButton(image3);
buttons[1] = new JButton(image3);
buttons[2] = new JButton(image3);
buttons[3] = new JButton(image3);
buttons[4] = new JButton(image3);
buttons[5] = new JButton(image3);
buttons[6] = new JButton(image3);
buttons[7] = new JButton(image3);
buttons[8] = new JButton(image3);
thePanel.add(buttons[0]);
thePanel.add(buttons[1]);
thePanel.add(buttons[2]);
thePanel.add(buttons[3]);
thePanel.add(buttons[4]);
thePanel.add(buttons[5]);
thePanel.add(buttons[6]);
thePanel.add(buttons[7]);
thePanel.add(buttons[8]);
thePanel.add(label1);
thePanel.add(label2);
thePanel.add(label3);
this.add(thePanel);
this.setVisible(true);
int treasureLocation = new Random().nextInt(buttons.length);
System.out.println(treasureLocation);
在编译器上,它给我一条错误消息,表明它不知道下面的if else语句中的“ buttons”或“ treasureLocation”。
}
public void actionPerformed(ActionEvent evt) {
if (evt.getSource() == buttons[treasureLocation]) {
}
else {
}
}
}
您是否可以使用其他侦听器类?
public class Test extends JFrame {
public Test(){
Button[] buttons;
int treasureLocation;
}
private class ButtonListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
if (evt.getSource() == buttons[treasureLocation]) {
}
}
}
这将导致错误,因为buttons
和treasureLocation
位置不在范围内。 如果不是这种情况,对我来说,这似乎仍然是一个范围问题。 尝试将变量声明为类成员
public class Test extends JFrame {
Button[] buttons;
int treasureLocation;
public Test(){
}
}
您实际上在构造函数中定义了buttons
和treasureLocation
,实际上not
建议这样做。 因此,它们的生命周期和访问权限仅限于此方法。 在您的课程中定义它们并对其进行初始化。
JLabel label1, label2, label3;
ImageIcon image1, image2, image3, image4, image5;
JTextField textResult;
JButton [] buttons; // Move the declaration here
int treasureLocation; // Move the declaration here
并改变
JButton [] buttons = new JButton[9];
至
buttons = new JButton[9];
和
int treasureLocation = new Random().nextInt(buttons.length);
至
treasureLocation = new Random().nextInt(buttons.length);
您需要将buttons
和treasureLocation
定义为属性。 因此,不要在方法或构造函数中定义它们,而应在后面的行中定义它们
ImageIcon image1, image2, image3, image4, image5;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.