如何通过if else语句更改Jbutton图像?

时间:2013-12-08 17:42:00

标签: java swing user-interface jbutton

真的很感激上一个问题的帮助,但遇到另一个问题是想知道是否有人可以提供帮助。在这个程序中,我正在尝试制作一个非常基本的寻宝游戏,用户必须点击查找gui上的宝藏,当点击按钮时,按钮的图片变成了宝箱的图片。我的问题是,我想让其他按钮将他们的照片更改为空洞,但不会覆盖宝箱图片。我想也许可以使用if / else语句,但不知道如何声明not buttons [treasureHunt]变量。任何帮助将不胜感激。

     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 implements ActionListener  {

   JLabel label1, label2, label3;


   ImageIcon image1, image2, image3, image4, image5; 

   JTextField textResult; 

   JButton [] buttons; 

   int treasureLocation; 




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


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

  buttons[0].addActionListener(this);
  buttons[1].addActionListener(this);
  buttons[2].addActionListener(this);
  buttons[3].addActionListener(this);
  buttons[4].addActionListener(this);
  buttons[5].addActionListener(this);
  buttons[6].addActionListener(this);
  buttons[7].addActionListener(this);
  buttons[8].addActionListener(this);



  this.add(thePanel);

  this.setVisible(true);



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


   System.out.println(treasureLocation);

  }

   public void actionPerformed(ActionEvent evt){

    if (evt.getSource() == buttons[treasureLocation]) {


     buttons[treasureLocation].setIcon(image1);

   }
   else 

   }




  }



 }

1 个答案:

答案 0 :(得分:3)

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

      buttons[treasureLocation].setIcon(image1);
   }
   else 
   {
      ((JButton) source).setIcon(someImageFile);
   }
}

这应该回答你的问题。 evt.getSource()返回触发事件的object,在这种情况下是单击的按钮。在else中,您必须将其强制转换为JButton才能更改其图标(=调用setIcon()方法)。

如果它是宝藏,你实际上也可以执行强制转换,而不是检索索引上的按钮。我没有改变它,因为我可以想象做一个演员是一个比在索引上检索它更昂贵的任务。

相关问题