Packagecom.neusoft.test;Importjava.awt.BorderLayout;ImportJava.awt.Choice;ImportJava.awt.Color;Importjava.awt.GridLayout;Importjava.awt.event.ActionEvent;ImportJava.awt.event.ActionListener;Importjava.awt.event.ItemEvent;ImportJava.awt.event.ItemListener;ImportJavax.swing.ImageIcon;ImportJavax.swing.JButton;ImportJavax.swing.JFrame;ImportJavax.swing.JLabel;ImportJavax.swing.JPanel; Public classTest05extendsJFrameImplementsActionListener, itemlistener{intm = 1; intn = 2; intPeace = 0; intWin = 0; intLose = 0; intAll = 0; JButton jb1,jb2; JLabel JL1,JL2,JL2I,JL3,JL3I,JL4,JL5; ImageIcon Img1,img2; Choice ch; JPanel JP1,JP2,JP3,JP4,JP5,JP6; PublicTest05 () {jb1=NewJButton ("Make sure the punch"); JB2=NewJButton ("Exit"); //Setting up monitoringJb1.addactionlistener ( This); Jb2.addactionlistener ( This); JL1=NewJLabel ("Scissors mini-game"); Jl2=NewJLabel ("Our fist")); Jl3=NewJLabel ("Enemy Punch"); Jl4=NewJLabel ("Please take a punch ..."); JL5=NewJLabel ("You played the" +all+ "bureau, Ping the" +peace+ "bureau, won the" +win+ "bureau, lost" +lose+ "bureau); System.out.println (m); IMG1=NewImageIcon ("d:/mkn/test/img/" +m+ ". png"); Img2=NewImageIcon ("d:/mkn/test/img/" +n+ ". png"); Jl2i=NewJLabel (IMG1); Jl3i=NewJLabel (IMG2); CH=NewChoice (); Ch.add (Stone); Ch.add (Scissors); Ch.add (Cloth); Ch.additemlistener ( This); JP1=NewJPanel (NewBorderLayout ()); JP2=NewJPanel (); JP3=NewJPanel (NewBorderLayout ()); JP4=NewJPanel (NewBorderLayout ()); JP5=NewJPanel (NewGridLayout (5,1)); JP6=NewJPanel (NewBorderLayout ()); Jp2.setsize (600, 25); Jp2.setbackground (color.red); Jp3.setsize (280, 300); Jp4.setsize (280, 300); Jp5.setsize (40, 300); Jp6.setsize (600, 25);//Jp6.setbackground (color.gray);Jp2.add (JL1);//titleJp3.add (Jl2,borderlayout.north);//Jp3.add (Jl2i,borderlayout.center); Jp4.add (Jl3,borderlayout.north); Jp4.add (jl3i, borderlayout.center);//the enemy punches .jp5.add (CH); Jp5.add (JB1); Jp5.add (JB2); Jp6.add (jl4,borderlayout.west); Jp6.add (jl5,borderlayout.east);//ResultsJp1.add (Jp2,borderlayout.north); Jp1.add (jp3,borderlayout.west); Jp1.add (Jp4,borderlayout.center); Jp1.add (Jp5,borderlayout.east); Jp1.add (Jp6,borderlayout.south); This. Add (JP1); This. Settitle ("Scissors mini-game"); This. SetSize (540,300); //this.setlayout (New BorderLayout ()); This. Setlocationrelativeto (NULL);//Center This. Setdefaultcloseoperation (Jframe.exit_on_close); This. setvisible (true); This. setresizable (false);//variable Window size} @Override Public voidactionperformed (ActionEvent e) {//TODO auto-generated Method Stub if(E.getactioncommand () = = "Make sure the punch") {n= (int) (1+math.random () * (3)); Jl3i.seticon (NewImageIcon ("d:/mkn/test/img/" +n+ ". png")); if(M-n = = 0) {Jl4.settext ("You and the computer draw"); Peace++; } if(m-n = = -1| | M-n = = 2) {Jl4.settext ("You won!!!" "); Win++; } if(m-n = = -2| | M-n = = 1) {Jl4.settext ("You lost!!!" "); Lose++; } All++; Jl5.settext ("You played the" +all+ "bureau, Ping the" +peace+ "bureau, won the" +win+ "bureau, lost" +lose+ "bureau);//System.out.println (n);}Else{ This. Dispose (); } } Public Static voidMain (string[] args) {Test05 test05=NewTest05 (); } @Override Public voiditemstatechanged (itemevent e) {if(E.getsource () = =ch) {Choice temp=(Choice) E.getsource (); if(Temp.getselecteditem () = = "Stone") {m= 1; } if(Temp.getselecteditem () = = "Scissors") {m= 2; } if(Temp.getselecteditem () = = "Cloth") {m= 3; } Jl2i.seticon (NewImageIcon ("d:/mkn/test/img/" +m+ ". png")); //System.out.println (m); } } }
Operation Result:
Java Stone Scissors Cloth Games