Import the Java. The awt. EventQueue;
Import the Java. The awt. Event. MouseAdapter;
Import the Java. The awt. Event. MouseEvent;
The import javax.mail. Swing. ImageIcon;
import javax.swing.JFrame;
The import javax.mail. Swing. JLabel;
Public class Shrewmouse extends JFrame implements Runnable {
Private JLabel [] mouses.//store display base tag array
Private ImageIcon imgMouse;//mouse image with object
Public static void main (String args []) {
EventQueue. InvokeLater (new Runnable () {
Public void the run () {
Try {
Shrewmouse frame=new Shrewmouse ();//create the form
Frame. SetVisible (true);//show the form
New Thread (frame). The start ();//start the thread
} the catch (Exception e) {
e.printStackTrace();
}
}
});
}
Public Shrewmouse () {
super();
Enclosing setResizable (false);//prohibited form adjust size
Enclosing getContentPane (). SetLayout (null);//form do not use the layout manager
Enclosing setTitle (" simple game of whack-a-mole ");//set the window title
Enclosing setDefaultCloseOperation (JFrame. DISPOSE_ON_CLOSE);
ImageIcon img=new ImageIcon (getClass () getResource (" background. JPG "));
//initialize the background picture
ImgMouse=new ImageIcon (getClass (). GetResource (" mouse. PNG "));
//initialization to rat image object
Mouses=new JLabel [6].//create an array of explicitly rat tag
for(int i=0; i<6; I++) {//iterate through group
Mouses [I]=new JLabel ();//initialize each array element
Mouses [I] setSize (imgMouse getIconWidth (), imgMouse. GetIconHeight ());
//set the label the same as the mouse image with size
Mouses [I] addMouseListener (new MouseAdapter () {//add mouse events to monitor adapter to tag
/* *
* how can we deal with the mouse click event
*/
@ Override
Public void mouseClicked (MouseEvent e) {
The Object source=um participant etSource ();//get the event source, namely, ground squirrels tag
If (source instanceof JLabel) {//if the event is the label component
JLabel mouse=(JLabel) source;//cast to JLabel tag
Mouse. SetIcon (null);//cancel label icon
}
}
});
Enclosing getContentPane (). The add (mouses [I]);//add explicitly rat label to form
}
Mouses [0]. SetLocation (253, 300);//set the position of each tag
Mouses [1]. SetLocation (333, 250);
Mouses [2]. SetLocation (388, 296);
Mouses [3]. SetLocation (362, 364);
Mouses [4]. SetLocation (189, 353);
Mouses [5]. SetLocation (240, 409);
Final JLabel backLabel=new JLabel ();//create display background tag
BackLabel. SetBounds (0, 0, img. GetIconWidth (), img. GetIconHeight ());
Enclosing setBounds (100100, img. GetIconWidth (), img. GetIconHeight ());
BackLabel. SetIcon (img);//add the background to the tag
Enclosing getContentPane (). The add (backLabel);//add background tags to the form
}
/* *
* thread core method
*/
Public void the run () {
While (true) {//use the infinite loop
Try {
Thread.sleep (1000);//the thread to sleep
1 SECInt index=(int) (Math. The random () * 6);//to generate random index of ground squirrels
If (mouses [index] getIcon ()==null) {//if the mouse tags do not have set the picture
Mouses [index] setIcon (imgMouse);//to add to the tags for rat picture
}
{} catch InterruptedException (e)
e.printStackTrace();
}
}
}
}
CodePudding user response:
Code of the paste, embarrassedCodePudding user response:
Code no for years, can only appreciate it