Advanced lottery system (interface beautification, click the button to jump to the website,)

catalogue

Interface and basic components

IO stream save id content part

Display effect

  New knowledge

Interface beautification

add menu

Operating system process

Complete code and picture content

Interface and basic components

Simple lottery system -- java implementation_ Worry free # blog - CSDN blog

IO stream save id content part

Intermediate lottery -- java implementation (save the winning list through IO stream)_ Worry free # blog - CSDN blog

Display effect

  New knowledge

ImageIconPicture path
JMenuBar(JMenu,JMenuItem)Menu control
ProcessBuilderOperating system process

Interface beautification

Main knowledge and methods: ImageIcon, setIcon();

Get the picture path through ImageIcon, and set the label to a picture through setIcon().

Form background beautification: beautify the form background through the hierarchical panel, and take a picture as the form background.

		    JLabel jl_bg = new JLabel(img); //background
		    jl_bg.setBounds(0, 0, 500, 400); //To set the position and size, you must setLayout(null)
		    getLayeredPane().add(jl_bg, new Integer(Integer.MIN_VALUE));//Layered panel
		    ((JPanel)this.getContentPane()).setOpaque(false); //Set transparency

Button beautification: set a picture for JButton.

ImageIcon img = new ImageIcon("src/bg.jpg");
JButton b1=new JButton(img);

add menu

  • JMenuBar is responsible for creating menu bars, that is, an instance of JMenuBar is a menu bar.
  • JMenu is responsible for creating menus, that is, an instance of JMenu is a menu.
  • JMenuItem is responsible for creating menu items, that is, an instance of JMenuItem is a menu item.

The relationship between the three: the menu item is placed in the menu, and the menu is placed in the menu bar.

Specific implementation:

JMenuBar jmb=new JMenuBar();
JMenu gailv = new JMenu(""About the author"");
JMenuItem jmi1=new JMenuItem("Handsome and good quality");
JMenuItem jmi2=new JMenuItem("Outdoor Expert");
JMenuItem jmi3=new JMenuItem("study hard Java");
jmb.add(about);
about.add(jmi1);
about.add(jmi2);
about.add(jmi3);

Operating system process

In some software, clicking a button or an option will jump to a web address.

Scenario: in this example, to obtain the id, you must go to one of my websites to participate in an activity to obtain the id. therefore, after clicking to obtain the id, you can jump to a website through the operating system process using the computer's own browser.

The general edge browser is located in C:\Program Files (x86)\Microsoft\Edge\Application.

 

1. Jump to your own html

try {
			    	ProcessBuilder pb=new ProcessBuilder("C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge_proxy.exe", 
			    			"D:\\confidential\\front end\\html\\Special effects.html");
						pb.start();					
						} catch (IOException e1) {
					// TODO Auto-generated catch block
						e1.printStackTrace();
					}

2. Jump to Baidu

try {
			    	ProcessBuilder pb=new ProcessBuilder("C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge_proxy.exe", 
			    			"https://www.baidu.com/");
						pb.start();					
						} catch (IOException e1) {
					// TODO Auto-generated catch block
						e1.printStackTrace();
					}

Complete code and picture content

In order to copy and test the function directly, I write all the code in one class.

import javax.swing.*;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class Frame extends JFrame{
	/**
	 * @author ZYY
	 */
	private static final long serialVersionUID = 1L;
	public List<String> list=new ArrayList<String>();
	public File f0=new File("d:/confidential/Text document");//Create a file object for creating a folder
	public File f1=new File("d:/confidential/Text document/id content.txt");//Create a text document
	public String prize[]= {"prize","A box of expired milk","A second-hand toothbrush","Make up a secret for Ollie","A bottle of rabbit","Six nukes a bottle",
			"Half bottle of opener","A bottle of Keri cola","A bag of wangzi beef candy","A bottle of Pepsi Cola","A bag of brain remnant sauerkraut beef noodles"};//Prize information
	
	FileInputStream fis;//Byte stream
	DataInputStream dis;//Encapsulate byte stream with data stream and read data through data stream
	@SuppressWarnings("removal")
	public Frame() {
		f0.mkdirs();//If you do not have this folder, create all folders of this directory
		if(!f1.exists()) {//If the txt document does not exist, create the txt document
			try {
				f1.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		try {//Read data
			fis=new FileInputStream(f1);
			dis=new DataInputStream(fis);
			String str1;
			try {
				str1 = dis.readUTF();				
				String str2[]=str1.split(",");//Through the "," sign, it is also divided into string arrays
				for(int i=0;i<str2.length;i++) {
						list.add(str2[i]);	//Assign the contents of the array to the list	
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}	
			try {//Close the flow from bottom to top
				dis.close();
				fis.close();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		//Interface part
		//setTitle("advanced lottery system");
		
		 ImageIcon img = new ImageIcon("src/bg.jpg");
		 ImageIcon img1 = new ImageIcon("src/1.jpg");
		 ImageIcon img2 = new ImageIcon("src/2.jpg");
		 ImageIcon img3 = new ImageIcon("src/3.jpg");
		 ImageIcon img4 = new ImageIcon("src/4.jpg");
		 ImageIcon img5 = new ImageIcon("src/5.jpg");
		 ImageIcon img6 = new ImageIcon("src/6.jpg");
		 ImageIcon img7 = new ImageIcon("src/7.jpg");
		 ImageIcon img8 = new ImageIcon("src/8.jpg");
		 ImageIcon img9 = new ImageIcon("src/9.jpg");
		 ImageIcon img10 = new ImageIcon("src/10.jpg");
		 ImageIcon img11 = new ImageIcon("src/bs.jpg");
		 ImageIcon img12 = new ImageIcon("src/Failed to win the prize.jpg");
		 ImageIcon img13 = new ImageIcon("src/qualifications.jpg");
		 
		    JLabel jl_bg = new JLabel(img); //background
		    jl_bg.setBounds(0, 0, 500, 400); //To set the position and size, you must setLayout(null)
		    getLayeredPane().add(jl_bg, new Integer(Integer.MIN_VALUE));//Layered panel
		    ((JPanel)this.getContentPane()).setOpaque(false); //Set transparency
		        
		JLabel l1=new JLabel("Advanced lottery system");//Label assembly
		JLabel l2=new JLabel("Please enter your id:");
		JLabel l3=new JLabel("");
		JLabel l4=new JLabel("View prizes:");
		JLabel l5=new JLabel("");
		JLabel l6=new JLabel("List of previous winners:");
		JLabel l7=new JLabel("");
		ImageIcon ani=new ImageIcon("src/cj.jpg");
		JButton b1=new JButton(ani);//Button assembly
		//JButton b2=new JButton("prompt");
		JButton b3=new JButton("sign out");
		JMenuBar jmb=new JMenuBar();
	    JMenu gailv = new JMenu("Winning probability");
	    JMenu about = new JMenu("About the author");
	    JMenu getid = new JMenu("obtain id");
	    JMenu rule = new JMenu("id Lottery rules");
	    JMenuItem jmi1=new JMenuItem("Handsome and good quality");
	    JMenuItem jmi2=new JMenuItem("Outdoor Expert");
	    JMenuItem jmi3=new JMenuItem("study hard Java");
	    JMenuItem jmi4=new JMenuItem("wrong vip=50%");
	    JMenuItem jmi5=new JMenuItem("vip=100%");
	    JMenuItem jmi6=new JMenuItem("clown=0%");
	    JMenuItem jmi7=new JMenuItem("Web address");
	    JMenuItem jmi8=new JMenuItem("id Length 6");
	    JMenuItem jmi9=new JMenuItem("v Get exclusive prizes at the beginning");
	    JMenuItem jmi10=new JMenuItem("a You have a chance to win a prize at the beginning");
	    JMenuItem jmi11	=new JMenuItem("Other formats cannot participate");
	    about.add(jmi1);
	    about.add(jmi2);
	    about.add(jmi3);
	    gailv.add(jmi4);
	    gailv.add(jmi5);
	    gailv.add(jmi6);
	    getid.add(jmi7);
	    rule.add(jmi8);
	    rule.add(jmi9);
	    rule.add(jmi10);
	    rule.add(jmi11);
	    jmb.add(about);
	    jmb.add(gailv);
	    jmb.add(getid);
	    jmb.add(rule);
		JTextField jtf=new JTextField();//Text box component
		JComboBox<Object> jcb=new JComboBox<Object>(prize);//Drop down list component
		String ps[]=new String [list.size()+1];//Drop down list content
		ps[0]="id";//Name the first one
		for(int i=1;i<ps.length;i++) {
			ps[i]=list.get(i-1);//Name the rest
		}
		JComboBox<Object> cb=new JComboBox<Object>(ps);
		jtf.setColumns(20);//Set text box content length
		l1.setBounds(150,20,300,50);//Absolute coordinates, size
		l1.setFont(new Font("Song typeface",Font.BOLD,26));//style
		l2.setBounds(10,340,110,30);
		l2.setFont(new Font("Song typeface",Font.BOLD,15));
		l3.setFont(new Font("Blackbody",Font.BOLD,20));
		l5.setFont(new Font("Regular script",Font.BOLD,17));
		l3.setBounds(100,80,300,30);
		l4.setBounds(350,70,100,30);
		l5.setBounds(110,120,200,200);
		l6.setBounds(350,130,100,30);
		l7.setBounds(30,130,80,30);
		jtf.setBounds(130,340,120,30);
		b1.setBounds(260,340,90,30);
		b1.setFocusPainted(false);//Remove focus
		//b2.setBounds(435,320,70,30);
		b3.setBounds(365,340,70,30);
		jcb.setBounds(350,100,130,30);
		cb.setBounds(350,155,130,30);
		add(l1);//Add component to form
		add(l2);
		add(l3);
		add(l4);
		add(l5);
		add(l6);
		add(l7);
		add(b1);
		//add(b2);
		add(b3);
		add(jtf);
		add(jcb);
		add(cb);
		setJMenuBar(jmb);
		setBounds(320, 200, 500, 400);//Sets the coordinate size of the form
		setUndecorated(true);//Remove form border
		setLayout(null);//Absolute layout
		
		setVisible(true);//Show
		jmi7.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				 try {
			    	ProcessBuilder pb=new ProcessBuilder("C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge_proxy.exe", 
			    			"https://www.baidu.com/");
						pb.start();					
						} catch (IOException e1) {
					// TODO Auto-generated catch block
						e1.printStackTrace();
					}
			}
		});
		b1.addActionListener(new ActionListener() {//Add listening
			@Override
			public void actionPerformed(ActionEvent e) {
				
				if(list.contains(jtf.getText())) {//Determine whether the id exists
					l3.setText("Should id Participated in the lucky draw!");
					l3.setForeground(Color.RED);//Foreground
				}else {
					if(jtf.getText().length()!=6) {
						l3.setText("id Format error");
						l3.setForeground(Color.BLUE);
					}else {
						list.add(jtf.getText());
						l3.setText(jtf.getText()+"Successfully participated in the lucky draw!");
						l3.setForeground(Color.GREEN);
						Random r=new Random();//Random prize
						int gl=r.nextInt(100);
						if(jtf.getText().charAt(0)=='v') {
							l7.setText("prize:");
							l7.setFont(new Font("Blackbody",Font.BOLD,20));
							//l5.setText("character explosion, you get a second-hand white silk");
							l5.setBorder(BorderFactory.createLineBorder(Color.RED));
							l5.setIcon(img11);						
						}else if(jtf.getText().charAt(0)=='a'){	
							if(gl>=20&&gl<=70) {//Probability 50%
								int num=r.nextInt(prize.length)+1;
								String p=prize[num];
								//l5.setText("congratulations on getting" + p);
								l7.setText("prize:");
								l7.setFont(new Font("Blackbody",Font.BOLD,20));
								if(num==1) l5.setIcon(img1);
								if(num==2) l5.setIcon(img2);
								if(num==3) l5.setIcon(img3);
								if(num==4) l5.setIcon(img4);
								if(num==5) l5.setIcon(img5);
								if(num==6) l5.setIcon(img6);
								if(num==7) l5.setIcon(img7);
								if(num==8) l5.setIcon(img8);
								if(num==9) l5.setIcon(img9);	
								if(num==10) l5.setIcon(img10);
								l5.setBorder(BorderFactory.createLineBorder(Color.RED));
							}else {
								//l5.setText("no winning");
								l5.setIcon(img12);	
							}
						}
						else {
							l5.setIcon(img13);	
							//l5.setText("but not completely successful draw");
						}

					}
				}		
			}		
		});	
//		b2.addActionListener(new ActionListener() {
//
//			@Override
//			public void actionPerformed(ActionEvent e) {
//			    try {
//			    	ProcessBuilder pb=new ProcessBuilder("C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge_proxy.exe", "www.baidu.com");
//					pb.start();
//				} catch (IOException e1) {
//					// TODO Auto-generated catch block
//					e1.printStackTrace();
//				}
//			}
//			
//		});
		b3.addActionListener(new ActionListener() {//id used to save the raffle
			public void actionPerformed(ActionEvent e) {
				try {
					String str3=list.get(0);
					for(int i=1;i<list.size();i++) {
						str3+= (","+list.get(i));//Save id
					}
					FileOutputStream fos=new FileOutputStream(f1);
					DataOutputStream dos=new DataOutputStream(fos);//Save the data to txt document through data flow
					try {
						dos.writeUTF(str3);
					} catch (IOException e1) {
						e1.printStackTrace();
					}
					try {
						dos.close();
						fos.close();
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}					
				} catch (FileNotFoundException e1) {					
					e1.printStackTrace();
				}
				System.exit(EXIT_ON_CLOSE);
			}
		});
	}
	
	
public static void main(String[] args) {
	new Frame();	//Call constructor
}
	}

If I need pictures, I'll put them on Baidu online disk:

Link: https://pan.baidu.com/s/1WpuPam6XidBe3bKEuIgUJQ
Extraction code: zyy6

Storage location of pictures in the project:

  It's not easy to make small editing. If you like, pay little attention to and collect. If you don't understand, I can have private letters.

Tags: Java Eclipse

Posted on Thu, 25 Nov 2021 16:17:40 -0500 by ORiGIN