GUI给多个按钮设置事件监听器的问题

weiyuqi723 2017-05-26 04:41:33
这个程序会显示几张图片,然后让USER判断图片的数量。第一次会出现10张图片,判断正确后则会随机出现图片。
我给多个有图片的按钮设置事件监听器,使USER点击某个按钮时,该按钮消失。
有两个问题:
1.第一次显示10张图片时,点击图片按钮,为什么会显示数组下标越界?
2.第一次输入正确后,之后的随机显示图片,再点击图片,为什么图片不会消失?
import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.awt.event.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.Image;
import javax.imageio.ImageIO;
import java.io.*;

public class JungleParty{
//Declaration of the wedgets of the GUI.
JFrame JP = new JFrame();
JLabel ques = new JLabel("How many animals have come to the party?");
JButton[] pic = new JButton[10];
JLabel instruction = new JLabel("Click on animals you wish to kick out of the party!");
ImageIcon[] array = new ImageIcon[10];
JPanel Animals = new JPanel();
JPanel Ins = new JPanel();
JPanel Q = new JPanel();

JComboBox Selector;
int i = 10, l = 0;

/** This is the initialisation of the wedgets*/
public void Init(){
//Read the images
readImage();

//Set the attibutes of the JFrame
JP.setLayout(new BorderLayout());
JP.setTitle("Welcome to the Jungle Party!");
JP.setSize(600,400);

Animals.setLayout(new GridLayout(0, 5));
setIcon(i);
Q.setLayout(new FlowLayout(FlowLayout.CENTER));
instruction.setForeground(Color.red);

//Set the EventListener
Vector<String> choice = new Vector<String>();
choice.add("0");
choice.add("1");
choice.add("2");
choice.add("3");
choice.add("4");
choice.add("5");
choice.add("6");
choice.add("7");
choice.add("8");
choice.add("9");
choice.add("10");
Selector = new JComboBox(choice);
Selector.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
Object o = Selector.getSelectedItem();
int ans = Integer.parseInt(String.valueOf(o));
if(ans == i){
int rand = (int)(Math.random()*9+1);
ques.setText("Correct! How many animals are in the party now?");
Animals.removeAll();
Animals.updateUI();
setIcon(rand);
i = rand;
}

else if(ans != i){
ques.setText("Wrong!Try again!");
}

}
});

//Add other wedgets to the main JFrame
Q.add(ques);
Q.add(Selector);
Ins.add(instruction);
JP.add(BorderLayout.NORTH, Animals);
JP.add(BorderLayout.CENTER, Q);
JP.add(BorderLayout.SOUTH, Ins);
JP.setVisible(true);
JP.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JP.pack();
}

/** This is the method to read the images*/
public void readImage(){
for(int j=0; j<10; j++){
switch(j){
case 0: array[j] = new ImageIcon("animal1.png"); break;
case 1: array[j] = new ImageIcon("animal2.png"); break;
case 2: array[j] = new ImageIcon("animal3.png"); break;
case 3: array[j] = new ImageIcon("animal4.png"); break;
case 4: array[j] = new ImageIcon("animal5.png"); break;
case 5: array[j] = new ImageIcon("animal6.png"); break;
case 6: array[j] = new ImageIcon("animal7.png"); break;
case 7: array[j] = new ImageIcon("animal8.png"); break;
case 8: array[j] = new ImageIcon("animal9.png"); break;
case 9: array[j] = new ImageIcon("animal10.png"); break;
}
}
}

/** This is the method to add the images to the Animals JPanel
* @param k The number of images that should be displayed.
*/
public void setIcon(int k){
for(l = 0; l<k; l++){
pic[l]= new JButton(array[l]);
Animals.add(pic[l]);
pic[l].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
pic[l].setVisible(false);
Animals.updateUI();
if(i != 0) i--;
else i = 0;
instruction.setText("Animal gone! How many animals are in the party now?");
}
});
}
}

/** The main function*/
public static void main(String[] args){
JungleParty J = new JungleParty();
J.Init();
}
}
...全文
221 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧