如何用java做一个学生管理系统平台的登录界面模块和添加界面模块!

如题所述

下面是一个使用 Java 编写的学生管理系统的登录界面模块和添加界面模块的示例代码:
登录界面模块:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Login extends JFrame implements ActionListener {
JLabel lb1, lb2;
JTextField tf1;
JPasswordField pf1;
JButton btn1, btn2;
public Login() {
setTitle("学生管理系统-登录");
setSize(300, 200);
setLocationRelativeTo(null);
lb1 = new JLabel("用户名:");
lb2 = new JLabel("密码:");
tf1 = new JTextField();
pf1 = new JPasswordField();
btn1 = new JButton("登录");
btn2 = new JButton("取消");
JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(2, 2));
p1.add(lb1);
p1.add(tf1);
p1.add(lb2);
p1.add(pf1);
JPanel p2 = new JPanel();
p2.setLayout(new FlowLayout());
p2.add(btn1);
p2.add(btn2);
setLayout(new BorderLayout());
add(p1, BorderLayout.CENTER);
add(p2, BorderLayout.SOUTH);
btn1.addActionListener(this);
btn2.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btn1) {
// 登录按钮被点击
String username = tf1.getText();
String password = new String(pf1.getPassword());
if (username.equals("admin") && password.equals("123456")) {
// 登录成功,打开主界面
MainFrame frame = new MainFrame();
frame.setVisible(true);
dispose();
} else {
// 登录失败,弹出提示框
JOptionPane.showMessageDialog(this, "用户名或密码错误!");
}
} else if (e.getSource() == btn2) {
// 取消按钮被点击,关闭窗口
dispose();
}
}
public static void main(String[] args) {
Login frame = new Login();
frame.setVisible(true);
}
}
在这个程序中,我们定义了一个 Login 类,继承了 JFrame 类,实现了登录界面的布局和逻辑。在登录界面中,我们使用了 JLabel、JTextField、JPasswordField、JButton 等控件来实现用户名和密码的输入和登录操作。当用户点击登录按钮时,我们将输入的用户名和密码与预设的账号和密码进行比较,如果匹配成功,则打开主界面;否则,弹出提示框告知用户登录失败。添加界面模块:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class AddFrame extends JFrame implements ActionListener {
JLabel lb1, lb2, lb3, lb4, lb5, lb6;
JTextField tf1, tf2, tf3, tf4, tf5;
JButton btn1, btn2;
public AddFrame() {
setTitle("学生管理系统-添加");
setSize(400, 300);
setLocationRelativeTo(null);
lb1 = new JLabel("学号:");
lb2 = new JLabel("姓名:");
lb3 = new JLabel("性别:");
lb4 =new JLabel("出生日期:");
lb5 = new JLabel("籍贯:");
lb6 = new JLabel("所在院系:");
tf1 = new JTextField();
tf2 = new JTextField();
tf3 = new JTextField();
tf4 = new JTextField();
tf5 = new JTextField();
btn1 = new JButton("添加");
btn2 = new JButton("取消");
JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(6, 2));
p1.add(lb1);
p1.add(tf1);
p1.add(lb2);
p1.add(tf2);
p1.add(lb3);
p1.add(tf3);
p1.add(lb4);
p1.add(tf4);
p1.add(lb5);
p1.add(tf5);
p1.add(lb6);
String[] deptList = {"计算机科学与技术", "信息工程", "物联网工程"};
JComboBox<String> comboBox = new JComboBox<>(deptList);
p1.add(comboBox);
JPanel p2 = new JPanel();
p2.setLayout(new FlowLayout());
p2.add(btn1);
p2.add(btn2);
setLayout(new BorderLayout());
add(p1, BorderLayout.CENTER);
add(p2, BorderLayout.SOUTH);
btn1.addActionListener(this);
btn2.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btn1) {
// 添加按钮被点击
String id = tf1.getText();
String name = tf2.getText();
String sex = tf3.getText();
String birth = tf4.getText();
String hometown = tf5.getText();
String dept = comboBox.getSelectedItem().toString();
// TODO: 将学生信息添加到数据库中
JOptionPane.showMessageDialog(this, "添加成功!");
} else if (e.getSource() == btn2) {
// 取消按钮被点击,关闭窗口
dispose();
}
}
public static void main(String[] args) {
AddFrame frame = new AddFrame();
frame.setVisible(true);
}
在这个程序中,我们定义了一个 `AddFrame` 类,继承了 `JFrame` 类,实现了添加界面的布局和逻辑。在添加界面中,我们使用了 `JLabel`、`JTextField`、`JComboBox`、`JButton` 等控件来实现学生信息的输入和添加操作。当用户点击添加按钮时,我们将输入的学生信息添加到数据库中,并弹出提示框告知用户添加成功;否则,直接关闭窗口。需要注意的是,在实际的开发中,我们需要将学生信息添加到数据库中,这里只是简单地用一个 TODO 注释标记了这个操作。
温馨提示:答案为网友推荐,仅供参考
相似回答
大家正在搜