简体   繁体   English

数组和动作监听器Java

[英]Array and action listener java

Really appreciated the help on the last question, still have a couple of errors however. 真的感谢最后一个问题的帮助,但是仍然有一些错误。 I am making a treasure Hunt game where the user clicks on a gui to try and reveal the location of a treasure chest. 我正在制作一个寻宝游戏,用户单击gui尝试显示藏宝箱的位置。 I was using an action listener to display a image of a treasure chest on the button if the location was found but that was a fixed position and I wanted to randomise this. 如果找到了位置,但我使用动作侦听器在按钮上显示宝箱的图像,但这是固定的位置,因此我想将其随机化。 Got some advice to use an array on buttons and random number generator then use an if/else to check. 有一些建议在按钮和随机数生成器上使用数组,然后使用if / else进行检查。 Having complier errors which I will comment on the code below. 出现编译器错误,我将在下面的代码中进行注释。 A good coder will probably pick my novice errors up in a matter of seconds! 好的编码员可能会在几秒钟内发现我的新手错误!

    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);

On the complier, it gives me a error message saying it does not know the "buttons" or "treasureLocation" in the if else statement below. 在编译器上,它给我一条错误消息,表明它不知道下面的if else语句中的“ buttons”或“ treasureLocation”。

        }
       public void actionPerformed(ActionEvent evt) {
      if (evt.getSource() == buttons[treasureLocation]) {
      }

  else {

  }

  } 

}

Is it possible you have a different Listener class? 您是否可以使用其他侦听器类?

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]) {
        }
    }
}

This would cause an error as buttons and treasureLocation are not within the scope. 这将导致错误,因为buttonstreasureLocation位置不在范围内。 If this is not the case, it still seems like a scope issue to me. 如果不是这种情况,对我来说,这似乎仍然是一个范围问题。 Try declaring the variables as class members 尝试将变量声明为类成员

public class Test extends JFrame {
    Button[] buttons;
    int treasureLocation;

    public Test(){

    }
}

You actually defined buttons and treasureLocation in your constructor, which is actually not recommended. 您实际上在构造函数中定义了buttonstreasureLocation ,实际上not建议这样做。 Their lifecycle and access is therefore limited to this method. 因此,它们的生命周期和访问权限仅限于此方法。 Define them in your class and initialize them. 在您的课程中定义它们并对其进行初始化。

JLabel label1, label2, label3;    
ImageIcon image1, image2, image3, image4, image5;
JTextField textResult;
JButton [] buttons; // Move the declaration here
int treasureLocation; // Move the declaration here

and change 并改变

JButton [] buttons = new JButton[9]; 

to

buttons = new JButton[9]; 

and

int treasureLocation = new Random().nextInt(buttons.length);

to

treasureLocation = new Random().nextInt(buttons.length);

You need to have buttons and treasureLocation defined as attributes. 您需要将buttonstreasureLocation定义为属性。 So don't define them in a method or a constructor, have them defined in the line after 因此,不要在方法或构造函数中定义它们,而应在后面的行中定义它们

ImageIcon image1, image2, image3, image4, image5;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM