Java 随机按钮位置

Java 随机按钮位置,java,swing,jbutton,actionlistener,Java,Swing,Jbutton,Actionlistener,我正在做的一个项目有一个小问题。我正在制作一个非常基本的寻宝游戏,用户点击gui上的按钮试图找到宝藏。如果用户确定了按钮的位置,则按钮将变为宝箱图像,否则将变为空洞图像。我的问题是我想随机化的宝箱将在游戏开始时,而不是被固定在按钮5。我想用一个随机数发生器,但无法确定如何使用它 import java.awt.*; import javax.swing.*; import java.util.Random; import java.awt.event.ActionListener; i

我正在做的一个项目有一个小问题。我正在制作一个非常基本的寻宝游戏,用户点击gui上的按钮试图找到宝藏。如果用户确定了按钮的位置,则按钮将变为宝箱图像,否则将变为空洞图像。我的问题是我想随机化的宝箱将在游戏开始时,而不是被固定在按钮5。我想用一个随机数发生器,但无法确定如何使用它

 import java.awt.*;
 import javax.swing.*;
 import java.util.Random;
 import java.awt.event.ActionListener;
 import java.awt.event.ActionEvent;



 public class Grid extends JFrame implements ActionListener {

 JButton but0, but1, but2, but3, but4, but5, but6, but7, but8;

 JLabel label1, label2, label3;    

  ImageIcon image1, image2, image3, image4, image5;


  JTextField textResult;    

  public static void main(String[] args) {



  new Grid();

   }


   public Grid (){

  Random rand = new Random();
  int n = rand.nextInt(9) + 1;
  System.out.println(n);


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



  but0 = new JButton(image3);
  but1 = new JButton(image3);
  but2 = new JButton(image3);
  but3 = new JButton(image3);
  but4 = new JButton(image3);
  but5 = new JButton(image3);
  but6 = new JButton(image3);
  but7 = new JButton(image3);
  but8 = new JButton(image3);



  thePanel.add(but1);
  thePanel.add(but2);
  thePanel.add(but3);
  thePanel.add(but4);
  thePanel.add(but5);
  thePanel.add(but6);
  thePanel.add(but7);
  thePanel.add(but8);
  thePanel.add(but0);
  thePanel.add(label1); 
  thePanel.add(label2);
  thePanel.add(label3);



  this.add(thePanel);

  this.setVisible(true);

  but1.addActionListener(this);
  but2.addActionListener(this);
  but3.addActionListener(this);
  but4.addActionListener(this);
  but5.addActionListener(this);
  but6.addActionListener(this);
  but7.addActionListener(this);
  but8.addActionListener(this);
  but0.addActionListener(this);


    }
     public void actionPerformed(ActionEvent evt) {
     Object obj1=evt.getSource();
    if(obj1==but5){
     label1.setText("You've found the treasure!");

  }
  Object obj2=evt.getSource();
  if(obj2==but1){
     label1.setText("Empty hole!");

  }
  Object obj3=evt.getSource();
  if(obj3==but2){
     label1.setText("Empty hole!");
  }
  Object obj4=evt.getSource();
  if(obj4==but3){
     label1.setText("Empty hole!");
  }
  Object obj5=evt.getSource();
  if(obj5==but4){
     label1.setText("Empty hole!");

  }
  Object obj6=evt.getSource();
  if(obj6==but6){
     label1.setText("Empty hole!");
  }
  Object obj7=evt.getSource();
  if(obj7==but7){
     label1.setText("Empty hole!"); 

  }
  Object obj8=evt.getSource();
  if(obj8==but8){
     label1.setText("Empty hole!");
  }
  Object obj9=evt.getSource();
  if(obj9==but0){
     label1.setText("Keep looking!");
  }
  Object obj10=evt.getSource();
  if(obj10==but1){
     but1.setIcon(image4);
  }
  Object obj11=evt.getSource();
  if(obj11==but2){
     but2.setIcon(image4);
  }
  Object obj12=evt.getSource();
  if(obj12==but3){
     but3.setIcon(image4);
  }
  Object obj13=evt.getSource();
  if(obj13==but4){
     but4.setIcon(image4);
  }
  Object obj14=evt.getSource();
  if(obj14==but5){
     but5.setIcon(image1);
  }
  Object obj15=evt.getSource();
  if(obj15==but6){
     but6.setIcon(image4);
  }
  Object obj16=evt.getSource();
  if(obj16==but7){
     but7.setIcon(image4);
  }
  Object obj17=evt.getSource();
  if(obj17==but8){
     but8.setIcon(image4);
  }
  Object obj18=evt.getSource();
  if(obj18==but0){
     but0.setIcon(image4);
    }




   }
 }

我会创建一个按钮数组,因为它更高效、更安全 更容易随机化

JButton[] buttons = new JButton[9]
buttons[0] = new JButton(image3)
buttons[1] = new JButton(image3)
...
(或者你可以反复浏览)

在程序开始时,随机导入

import java.util.Random;
然后在节目的后面

int treasureLocation = new Random().nextInt(buttons.lenth)
这将给您一个介于0和9之间的随机数

在actionPerformed方法中

if(evt.getSource() == buttons[treasureLocation]) {
    // Do whatever, they found the treasure
}
else {
    // They didn't find the treasure
}

希望这有帮助:)

非常感谢您的帮助。基于您的想法,仍然存在编译器错误,我敢肯定我的新手编码有问题!它似乎没有选择if-else语句中的按钮或位置。如果您能提供更多帮助,请发布一个带有代码的新问题。@user3077880确定我已经回答了:)