Simple Implementation of java Student Management System Interface (full)
- 2020-12-22 17:38:24
- OfStack
The student management system is simple to implement, for beginners Java Swing students to learn to use.
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, the entrance to the 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";
// Declares the width and height of the screen and the width and height of the program window
private int windowWidth;
private int windowHeight;
private int screenSizeWidth;
private int screenSizeHeight;
// The constructor,
public begindemo(String title)
{
super(title); // Set the title
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Set to close the window
this.setSize(600, 600); // Set the size of the window
this.setLayout(null); // Set the default layout format of the program is empty, in order to facilitate their own simple layout later
this.setResizable(false); // Set non-scaling
init(); // Perform initialization functions (add components such as username and password to the panel)
this.setVisible(true); // Make the program visible
}
public void init()
{
// Assign a value to the width and height of the screen, 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();
// Sets the location of the program window to the center of the screen
this.setLocation(screenSizeWidth / 2 - windowWidth / 2,
screenSizeHeight / 2 - windowHeight / 2);
// Declare name, password label
JLabel username_label = new JLabel(" The name ");
JLabel password_label = new JLabel(" password ");
// Declare the name input field and password input field
final JTextField user_field = new JTextField();
final JPasswordField password_field = new JPasswordField();
// Declare login button
JButton login_btn = new JButton(" The 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(user_field);
this.add(password_field);
this.add(login_btn);
// The listener for the login button
login_btn.addActionListener(new ActionListener()
{
@SuppressWarnings("deprecation")
@Override
// This method is automatically activated when the button is clicked
public void actionPerformed(ActionEvent event)
{
// If the username and password are both 123 The pop-up dialog box shows that the login was successful and opens another 1 10 main frames (home page)
if (user_field.getText().equals(userName)
&& password_field.getText().equals(password))
{
JOptionPane.showMessageDialog(null, " Login successful ", "Login",
JOptionPane.INFORMATION_MESSAGE);
// Statement of the home page
JFrame home_page = new JFrame(" The home page ");
// Set the location for the home page
home_page.setLocation(screenSizeWidth / 2 - windowWidth / 2
+ 50, screenSizeHeight / 2 - windowHeight / 2 + 50);
// Set the size of the home page
home_page.setSize(windowWidth, windowHeight);
// Set the home page to close, and hide the login page after successful login
home_page.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
home_page.setVisible(true);
setVisible(false);// Login page hidden
} else // If the login is not successful, login again
{
JOptionPane.showMessageDialog(null, " Login failed, please login again ", "Login",
JOptionPane.INFORMATION_MESSAGE);
// Set the content of the input box to be empty and let the user reenter
user_field.setText("");
password_field.setText("");
}
}
});
}
}
1 student class has been added 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, the entrance to the 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";
// Declares the width and height of the screen and the width and height of the program window
private int windowWidth;
private int windowHeight;
private int screenSizeWidth;
private int screenSizeHeight;
// The constructor,
public begindemo(String title)
{
super(title); // Set the title
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Set to close the window
this.setSize(600, 600); // Set the size of the window
this.setLayout(null); // Set the default layout format of the program is empty, in order to facilitate their own simple layout later
this.setResizable(false); // Set non-scaling
init(); // Perform initialization functions (add components such as username and password to the panel)
this.setVisible(true); // Make the program visible
}
public void init()
{
// Assign a value to the width and height of the screen, 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();
// Sets the location of the program window to the center of the screen
this.setLocation(screenSizeWidth / 2 - windowWidth / 2,
screenSizeHeight / 2 - windowHeight / 2);
// Declare name, password label
JLabel username_label = new JLabel(" The name ");
JLabel password_label = new JLabel(" password ");
// Declare the name input field and password input field
final JTextField user_field = new JTextField();
final JPasswordField password_field = new JPasswordField();
// Declare login button
JButton login_btn = new JButton(" The 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(user_field);
this.add(password_field);
this.add(login_btn);
// The listener for the login button
login_btn.addActionListener(new ActionListener()
{
@SuppressWarnings("deprecation")
@Override
// This method is automatically activated when the button is clicked
public void actionPerformed(ActionEvent event)
{
// If the username and password are both 123 The pop-up dialog box shows that the login was successful and opens another 1 10 main frames (home page)
if (user_field.getText().equals(userName)
&& password_field.getText().equals(password))
{
JOptionPane.showMessageDialog(null, " Login successful ", "Login",
JOptionPane.INFORMATION_MESSAGE);
// Statement of the home page
JFrame home_page = new JFrame(" The home page ");
// Set the location for the home page
home_page.setLocation(screenSizeWidth / 2 - windowWidth / 2
+ 50, screenSizeHeight / 2 - windowHeight / 2 + 50);
// Set the size of the home page
home_page.setSize(windowWidth, windowHeight);
// Set the home page to close, and hide the login page after successful login
home_page.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
home_page.setVisible(true);
setVisible(false);// Login page hidden
} else // If the login is not successful, login again
{
JOptionPane.showMessageDialog(null, " Login failed, please login again ", "Login",
JOptionPane.INFORMATION_MESSAGE);
// Set the content of the input box to be empty and let the user reenter
user_field.setText("");
password_field.setText("");
}
}
});
}
}
// The statement 1 Student class, convenient to add student information in the future
class student
{
private String name;
private String sex;
private int number; // Student id
private String class_; // The class
private double grade;
// The default constructor, new1 Object will be called automatically
public student()
{
this.name = "";
this.number = 0;
this.class_ = "";
this.grade = 0;
System.out.println(" This is a 1 A student ");
}
// Overloaded constructors
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 the function of setting the name, gender, student number, etc., which will be called in the future when input student information is stored
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;
}
// Below are several functions to get the student's name, gender, etc., which are called later to display the student information in the window when displaying the student information.
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;
}
// The same thing as the function above 1 Set up under the 1 All personal information of three students
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;
}
//1 under 1 All the information about a student is not needed 1 a 1 a getName or getSex the
public String getAll()
{
String output="";
output+=getName()+" "+getSex()+" "+getNumber()+" "+getClass_()+" "+getGrade();
return output;
}
}
For more information, please pay attention to the topic management System Development.