62,621
社区成员
发帖
与我相关
我的任务
分享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();
}
}