Lottery Program: With ArrayList class and Random class
Import java.awt.*;
Import javax.swing.*;
Import java.awt.event. ;
Import Java.util.;
Import java.util.List;
public class Jrandom extends JFrame {
Private JFrame frame;
Private JButton Startbutton;
PublicJrandom() {makeframe (); Frame.pack (); Frame.setvisible (true); Frame.setsize (400,215); Frame.setlocation (550,350); Frame.setdefaultcloseoperation (jframe.exit_on_close);}PrivatevoidMakeframe() {frame =New JFrame ("Draw a prize!" "); ImageIcon ImageIcon0 =New ImageIcon ("T/1.png"); Frame.seticonimage (Imageicon0.getimage ()); Container ContentPane = Frame.getcontentpane (); Startbutton =New JButton (); ImageIcon ImageIcon1 =New ImageIcon ("T/3.png"); ImageIcon ImageIcon2 =New ImageIcon ("T/4.png"); ImageIcon ImageIcon3 =New ImageIcon ("T/5.png"); Startbutton.seticon (ImageIcon1);Set button Shape Startbutton.setrollovericon (imageIcon2);Set the mouse over button image Startbutton.setpressedicon (ImageIcon3);Sets the shape of the mouse button pressed Startbutton.setmargin (New Insets (0,0,0,0));Sets the number of pixels between the button border and the content of the border startbutton.setborderpainted (FALSE);Do not draw bordersButton Event Listener Startbutton.addactionlistener (New ActionListener () {@OverridePublicvoidActionperformed(ActionEvent e) {TODO auto-generated Method Stub Makechoujiang (); } }); Contentpane.add (Startbutton, borderlayout.center);}PrivatevoidMakechoujiang() {Double Basenum =0.50; String Product ="100 yuan, 200 yuan, 500 yuan, 1000 Yuan"; String num ="30,20,5,3"; String[] p = product.split (","); string[] n = num.split (","); Random ran =New Random ();Double base = ran.nextdouble ();if (Base > Basenum) {imageicon ImageIcon =New ImageIcon ("T/2.png"); Joptionpane.showmessagedialog (Frame,"Please continue your efforts!" ","Lottery Results", joptionpane.plain_message, ImageIcon);Return } list<string>List =New Arraylist<string> ();for (int i =0; i < p.length; i++) {for (Int J =0; J < Integer.parseint (N[i]); J + +) {list.add (P[i]);} } //upset the prize pool data int L = ran.nextint ( List.size ()); ImageIcon ImageIcon = new ImageIcon ( "t/1.png"); Joptionpane.showmessagedialog (frame, "Congratulations" + list.get (L) + "Yo!!", "lottery results", joptionpane.plain_message, ImageIcon);} public static void main (string[] args) { //TODO auto-generated Method stub new jrandom ();}
}
The program interface is as follows:
Sixth time Assignment