Using the card game taught by the teacher, I made the code improvements into a lottery system and displayed the results in a graphical user interface.
Importjava.awt.event.ActionEvent;ImportJava.awt.event.ActionListener;Importjava.util.ArrayList;Importjava.util.Collections;ImportJava.util.Random;ImportJavax.swing.JButton;ImportJavax.swing.JFrame;ImportJavax.swing.JTextArea; Public classReward {ArrayList<Integer>lists; PublicArraylist<integer>deal () {if(lists==NULL) {Lists=NewArraylist<integer>(); for(intI =1;i<200;i++) {lists.add (i); }} collections.shuffle (lists); returnlists; } Public voidrun () {JFrame frm=NewJFrame ("lottery Platform");//Create a FrameJButton BTN =NewJButton ("Sweepstakes");//Create button FinalJTextArea Text =NewJTextArea ();//Create a text boxText.setbounds (130, 40, 200, 100);//Set the text box size positionText.seteditable (false);//Setting a text box is not editable /** Add Event response for Button*/Btn.addactionlistener (NewActionListener () { Public voidactionperformed (ActionEvent e) {Random R=NewRandom (); intA = R.nextint (200); Text.settext ("First Prize:" + Lists.get (a) + "No. \ n"); Lists.remove (a); for(intj=1;j<=3;j++) {a= R.nextint (200); Text.append ("Second Prize:" +lists.get (a) + "No. \ n"); Lists.remove (a); } }}); Btn.setbounds (200, 170, 60, 30);//Set Button size positionFrm.add (text);//Add a text box to a frameFrm.add (BTN);//Add a button to the frameFrm.setbounds (200,300, 450, 300);//Set Frame size positionFrm.setlayout (NULL);//Set Layout modeFrm.setvisible (true);//set to visible } Public Static voidMain (string[] args) {reward rew=Newreward (); Rew.deal (); Rew.run (); }}
Java Jobs (vi)