The student management system is simple to implement and is used by beginners with Java Swing.
import java.awt.Dimension;import java.awt.Toolkit;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JPasswordField;import javax.swing.JTextField;//Main class, entry of program public class begin{ public static void main(String[] args) { new begindemo("This is my management system"); }}class begindemo extends JFrame{ //Login username and password private final String userName = "123"; private final String password = "123"; //Declare the width and height of the screen, the width and height of the program window private int windowWidth; private int windowHeight; private int screenSizeWidth; private int screenSizeHeight; //Constructor, public begindemo(String title) { super(title); //Set the title this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Set the window to be closed this.setSize(600, 600); //Set the size of the window this.setLayout(null); //Set the default layout format of the program to be empty, so that you can simply set the layout this.setResizable(false); //Set not scalable init(); //Execute the initialization function (add the username, password and other components to the panel) this.setVisible(true); //Make the program visible} public void init() { //Assign the value to the width and height of the screen and the width and height of the program window Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize(); screenSizeWidth = (int) dimension.getWidth(); screenSizeHeight = (int) dimension.getHeight(); windowWidth = this.getWidth(); windowHeight = this.getHeight(); //Set the position of the program window to the center of the screen this.setLocation(screenSizeWidth / 2 - windowWidth / 2, screenSizeHeight / 2 - windowHeight / 2); // Declare the name and password JLabel username_label = new JLabel("name"); JLabel password_label = new JLabel("Password"); // Declare the name input box and password input box final JTextField user_field = new JTextField(); final JPasswordField password_field = new JPasswordField(); // Declare the login button JButton login_btn = new JButton("Login"); // Set the size and location of each label and input box username_label.setBounds(150, 100, 100, 50); password_label.setBounds(150, 200, 100, 50); user_field.setBounds(200, 100, 300, 50); password_field.setBounds(200, 200, 300, 50); login_btn.setBounds(300, 300, 100, 50); this.add(username_label); this.add(password_label); this.add(password_field); this.add(password_field); this.add(login_btn); //Login button's listener login_btn.addActionListener(new ActionListener() { @SuppressWarnings("deprecation") @Override //This method is automatically mobilized when the button is clicked public void actionPerformed(ActionEvent event) { //If the username and password are both 123, a dialog box pops up showing that the login is successful and another main framework is opened (home page) if (user_field.getText().equals(userName) && password_field.getText().equals(password)) { JOptionPane.showMessageDialog(null, "Login successfully", "Login", JOptionPane.INFORMATION_MESSAGE); //Declare the home page JFrame home_page = new JFrame("Home"); //Set the location for the home page home_page.setLocation(screenSizeWidth / 2 - windowWidth / 2 + 50, screenSizeHeight / 2 - windowHeight / 2 + 50); //Set the size for the home page home_page.setSize(windowWidth, windowHeight); //Set the home page can be closed, and after logging in, hide the login page home_page.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); home_page.setVisible(true); setVisible(false);//Hide the login page} else //On the contrary, if the login is not successful, log in again { JOptionPane.showMessageDialog(null, "Login failed, please log in again", "Login", JOptionPane.INFORMATION_MESSAGE); //Set the content of the input box to empty, let the user re-enter user_field.setText(""); password_field.setText(""); } } }); }}Added a student class for future use
package demo;import java.awt.Dimension;import java.awt.Toolkit;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JPasswordField;import javax.swing.JTextField;//Main class, entry of program public class begin{ public static void main(String[] args) { new begindemo("This is my management system"); new student(); }}class begindemo extends JFrame{ //Login username and password private final String userName = "123"; private final String password = "123"; //Declare the width and height of the screen, the width and height of the program window private int windowWidth; private int windowHeight; private int screenSizeWidth; private int screenSizeHeight; //Constructor, public begindemo(String title) { super(title); //Set the title this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Set the window to be closed this.setSize(600, 600); //Set the size of the window this.setLayout(null); //Set the default layout format of the program to be empty, so that you can simply set the layout this.setResizable(false); //Set not scalable init(); //Execute the initialization function (add the username, password and other components to the panel) this.setVisible(true); //Make the program visible} public void init() { // Assign the value to the width and height of the screen and the width and height of the program window Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize(); screenSizeWidth = (int) dimension.getWidth(); screenSizeHeight = (int) dimension.getHeight(); windowWidth = this.getWidth(); windowHeight = this.getHeight(); //Set the position of the program window to the center of the screen this.setLocation(screenSizeWidth / 2 - windowWidth / 2, screenSizeHeight / 2 - windowHeight / 2); // Declare the name and password JLabel username_label = new JLabel("name"); JLabel password_label = new JLabel("Password"); // Declare the name input box and password input box final JTextField user_field = new JTextField(); final JPasswordField password_field = new JPasswordField(); // Declare the login button JButton login_btn = new JButton("Login"); // Set the size and location of each label and input box username_label.setBounds(150, 100, 100, 50); password_label.setBounds(150, 200, 100, 50); user_field.setBounds(200, 100, 300, 50); password_field.setBounds(200, 200, 300, 50); login_btn.setBounds(300, 300, 100, 50); this.add(username_label); this.add(password_label); this.add(password_field); this.add(password_field); this.add(login_btn); //Login button's listener login_btn.addActionListener(new ActionListener() { @SuppressWarnings("deprecation") @Override //This method is automatically mobilized when the button is clicked public void actionPerformed(ActionEvent event) { //If the username and password are both 123, a dialog box pops up showing that the login is successful and another main framework is opened (home page) if (user_field.getText().equals(userName) && password_field.getText().equals(password)) { JOptionPane.showMessageDialog(null, "Login successfully", "Login", JOptionPane.INFORMATION_MESSAGE); //Declare the home page JFrame home_page = new JFrame("Home"); //Set the location for the home page home_page.setLocation(screenSizeWidth / 2 - windowWidth / 2 + 50, screenSizeHeight / 2 - windowHeight / 2 + 50); //Set the size for the home page home_page.setSize(windowWidth, windowHeight); //Set the home page can be closed, and after logging in, hide the login page home_page.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); home_page.setVisible(true); setVisible(false);//Hide the login page} else //On the contrary, if the login is not successful, log in again { JOptionPane.showMessageDialog(null, "Login failed, please log in again", "Login", JOptionPane.INFORMATION_MESSAGE); //Set the content of the input box to empty, let the user re-enter user_field.setText(""); password_field.setText(""); } } }); }}//Declare a student class to facilitate the addition of student information in the future using class student{ private String name; private String sex; private int number; //Student number private String class_; //Class private double grade; //Default constructor, public student() will be automatically called when new object { this.name = ""; this.number = 0; this.class_ = ""; this.grade = 0; System.out.println("This is a student"); } //Overloaded constructor public student(String name, int number, String class_, double grade) { this.name = name; this.number = number; this.class_ = class_; this.grade = grade; } //The following is a function that sets the name, gender, student number, etc., which will be called in the future when entering student information storage. Now write it out first to facilitate call public void setName(String name) { this.name = name; } public void setSex(String sex) { this.sex = sex; } public void setNumber(int number) { this.number = number; } public void setClass(String class_) { this.class_ = class_; } public void setGrade(double grade) { this.grade = grade; } //The following are several functions that get the student's name and gender, etc. When displaying the student's information in the future, it is called to display the student's information on the window. public String getName() { return this.name; } public String getSex() { return this.sex; } public int getNumber() { return this.number; } public String getClass_() { return this.class_; } public double getGrade() { return this.grade; } //It is similar to the above function to set all the personal information of a student public void setAll(String name, String sex, int number, String class_,double grade) { this.name=name; this.number=number; this.sex=sex; this.class_ = class_; this.grade = grade; } //After all the information of a student, you don’t need to getName or getSex one by one. public String getAll() { String output=""; output+=getName()+" "+getSex()+" "+getNumber()+" "+getClass_()+" "+getGrade(); return output; } }For more learning materials, please pay attention to the special topic "Management System Development".
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.