Login windows are generally very common, so let’s write one ourselves!
PS: Many imports are duplicate because I wrote them in several categories and must be imported separately.
//Simulate the qq login window import java.awt.*;import java.io.*;import java.awt.event.*;import javax.swing.*;public class QQGUI extends JFrame implements ActionListener{ private JLabel userLa; private JLabel pwdLa; private JLabel verCodeLa;//Verification code private JTextField userTxt; private JPasswordField pwdTxt; private JTextField verCodeTxt;//Verification code private JButton sureBt; private JButton quitBt; private Mypanel mp; //Construction method public QQGUI() { Init(); } public void Init() { Frame frame = new Frame("QQ login"); //Create the control object (because the above is just declared and does not give the actual space) //User text userLa = new JLabel(); userLa.setText("User name:"); userLa.setSize(60, 50); userLa.setLocation(100, 80); //Password text pwdLa = new JLabel(); pwdLa.setText("Password:"); pwdLa.setSize(50, 50); pwdLa.setLocation(100, 120); //User input box userTxt = new JTextField(); userTxt.setSize(100, 20); //This.setSize(width, height) userTxt.setLocation(170, 95); //Password input box pwdTxt = new JPasswordField(); pwdTxt.setSize(100, 20); pwdTxt.setLocation(170, 135); //Confirm button sureBt = new JButton("Login"); sureBt.setSize(60, 25); sureBt.setLocation(135, 260); //Exit button quitBt = new JButton("Exit"); quitBt.setSize(60, 25); quitBt.setLocation(240, 260); //Verification code text verCodeLa = new JLabel(); verCodeLa.setText("Verification code:"); verCodeLa.setSize(60, 50); verCodeLa.setLocation(100, 165); //Verification code text box verCodeTxt = new JTextField(); verCodeTxt.setSize(100, 20); verCodeTxt.setLocation(170, 180); //Verification code mp = new Mypanel(); mp.setSize(100, 30); mp.setLocation(280, 175); //Login method selection box JComboBox xlk=new JComboBox(); xlk.setSize(60, 20); xlk.setLocation(250, 220); xlk.addItem("online"); xlk.addItem("invisible"); xlk.addItem("leave"); this.setLayout(null); this.setSize(500, 400); this.add(userLa); this.add(pwdLa); this.add(userTxt); this.add(sureBt); this.add(quitBt); this.add(verCodeLa); this.add(verCodeTxt); this.add(mp); this.add(xlk); sureBt.addActionListener(this); quitBt.addActionListener(this); this.setVisible(true); } // Handling of specific events public void actionPerformed(ActionEvent e) { //Get the event source casting JButton bt = (JButton)e.getSource(); //Get the text displayed on the button String str = bt.getText(); if(str.equals("Login")) { if(!CheckIsNull()) { //Get the user name entered by the user String user = userTxt.getText().trim(); //Get the password entered by the user String pwd = pwdTxt.getText().trim(); if(checkUserAndPwd(user,pwd)) { //Hide the current login window this.setVisible(false); //Verify that a main window is successfully created MainFrame frame = new MainFrame(); } else { //If it is wrong, a display box will pop up JOptionPane pane = new JOptionPane("User or Password error"); JDialog dialog = pane.createDialog(this,"Warning"); dialog.show(); } } } else { //Call one of the system classes to exit System.exit(0); } } private boolean CheckIsNull() { boolean flag = false; if(userTxt.getText().trim().equals(")) { flag = true; } else { if(pwdTxt.getText().trim().equals(")) { flag = true; } } return flag; } private boolean checkUserAndPwd(String user,String pwd) { boolean result = false; try { FileReader file = new FileReader("D://Workspaces//MyEclipse 8.5//testGUI.txt"); BufferedReader bre = new BufferedReader(file); String str = bre.readLine(); while(str!=null) { String[] strs = str.split(","); if(strs[0].equals(user)) { if(strs[1].equals(pwd)) result = true; } str = bre.readLine(); } file.close(); } catch(Exception ex) { System.out.print(""); } return result; }}//MainFrame class import javax.swing.*;public class MainFrame extends JFrame { public MainFrame() { this.setSize(300, 300); this.setVisible(true); }}//Generation of verification code import java.awt.*;import java.util.*;public class Mypanel extends Panel { public void paint(Graphics g) { int height = 50; int width = 90; //Verification code box background color g.setColor(Color.LIGHT_GRAY); //Fill the verification code background g.fillRect(0, 0, width, height); g.setColor(Color.BLACK); g.drawRect(0, 0, width-1, height-1); Random r = new Random(); //Set the interference point for(int i = 0;i<100;i++) { int x = r.nextInt(width)-1; int y = r.nextInt(height)-1; g.drawOval(x, y, 2, 2); } g.setFont(new Font("Bold",Font.BOLD,20)); //Set the verification code font and size g.setColor(Color.RED); //Set the verification code font color//Generate a random verification code char[] tmp = ("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray(); StringBuilder sb = new StringBuilder(); for(int i = 0;i<4;i++) { int pos = r.nextInt(tmp.length); char c = tmp[pos]; sb.append(c + " "); } g.drawString(sb.toString(), 10, 15);//Write verification code}}//Implementation of drop-down box import java.awt.*;import java.awt.event.*;import javax.swing.*;public class xiaalakuang extends JFrame { private JComboBox comboBox;//Define a combobox public void xia() { //JPanel panel = new JPanel();//Create a JPanel panel comboBox = new JComboBox(); comboBox.addItem("online"); comboBox.addItem("invisible"); comboBox.addItem("leave"); this.add(comboBox); //this.add(panel); this.setSize(200, 100); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); }}//Test public class testQQGUI { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub QQGUI frame = new QQGUI(); }}The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.