用Eclipse写一个Swing图书馆学生登录查询信息的代码
时间: 2024-02-27 17:58:12 浏览: 17
很高兴回答您的问题!以下是一个简单的Swing图书馆学生登录查询信息的代码,您可以在Eclipse中使用它进行开发:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class LibrarySystem extends JFrame implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
private JLabel nameLabel, idLabel, passwordLabel;
private JTextField nameField, idField;
private JPasswordField passwordField;
private JButton loginButton;
private JPanel loginPanel, infoPanel;
public LibrarySystem() {
// 设置窗口标题和大小
setTitle("图书馆学生登录查询系统");
setSize(400, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 创建登录面板
loginPanel = new JPanel(new GridLayout(4, 2));
nameLabel = new JLabel("姓名:");
idLabel = new JLabel("学号:");
passwordLabel = new JLabel("密码:");
nameField = new JTextField(10);
idField = new JTextField(10);
passwordField = new JPasswordField(10);
loginButton = new JButton("登录");
// 添加组件到登录面板
loginPanel.add(nameLabel);
loginPanel.add(nameField);
loginPanel.add(idLabel);
loginPanel.add(idField);
loginPanel.add(passwordLabel);
loginPanel.add(passwordField);
loginPanel.add(loginButton);
loginButton.addActionListener(this);
// 创建信息面板
infoPanel = new JPanel();
infoPanel.setLayout(new BorderLayout());
infoPanel.add(new JLabel("欢迎登录图书馆学生查询系统!"), BorderLayout.CENTER);
// 添加面板到窗口
add(loginPanel, BorderLayout.CENTER);
add(infoPanel, BorderLayout.SOUTH);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// 获取用户输入的姓名、学号和密码
String name = nameField.getText();
String id = idField.getText();
String password = new String(passwordField.getPassword());
// 判断用户名和密码是否正确
if (name.equals("admin") && id.equals("123") && password.equals("123456")) {
// 登录成功,显示查询信息面板
infoPanel.removeAll();
infoPanel.add(new JLabel("您好," + name + ",欢迎登录图书馆学生查询系统!"), BorderLayout.CENTER);
infoPanel.revalidate();
infoPanel.repaint();
} else {
// 登录失败,弹出错误提示框
JOptionPane.showMessageDialog(this, "登录失败,用户名或密码错误!", "错误提示", JOptionPane.ERROR_MESSAGE);
}
}
public static void main(String[] args) {
new LibrarySystem();
}
}
```
这个程序包含一个登录面板和一个信息面板。当用户输入正确的用户名和密码后,会显示一个欢迎信息。当用户输入错误的用户名和密码时,会弹出一个错误提示框。
希望这个代码能帮到您!