用Eclipse写一个Swing学生登录查询成绩的代码
时间: 2024-02-27 09:57:18 浏览: 13
好的,下面是一个简单的Swing学生登录查询成绩的代码,你可以在Eclipse中创建一个Java项目,然后将以下代码复制到一个新的Java类文件中:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class StudentGradeQuery extends JFrame implements ActionListener {
private JTextField usernameField;
private JPasswordField passwordField;
private JButton loginButton;
private JLabel resultLabel;
public StudentGradeQuery() {
setTitle("学生成绩查询系统");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 200);
setLocationRelativeTo(null);
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
JLabel usernameLabel = new JLabel("用户名:");
usernameLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
panel.add(usernameLabel);
usernameField = new JTextField();
usernameField.setMaximumSize(new Dimension(200, 30));
usernameField.setAlignmentX(Component.CENTER_ALIGNMENT);
panel.add(usernameField);
JLabel passwordLabel = new JLabel("密码:");
passwordLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
panel.add(passwordLabel);
passwordField = new JPasswordField();
passwordField.setMaximumSize(new Dimension(200, 30));
passwordField.setAlignmentX(Component.CENTER_ALIGNMENT);
panel.add(passwordField);
loginButton = new JButton("登录");
loginButton.setAlignmentX(Component.CENTER_ALIGNMENT);
loginButton.addActionListener(this);
panel.add(loginButton);
resultLabel = new JLabel("");
resultLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
panel.add(resultLabel);
add(panel);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String username = usernameField.getText();
String password = String.valueOf(passwordField.getPassword());
if (username.equals("admin") && password.equals("123456")) {
resultLabel.setText("登录成功,您的成绩为:数学:90,英语:85,化学:92");
} else {
resultLabel.setText("用户名或密码错误,请重新输入");
}
}
public static void main(String[] args) {
new StudentGradeQuery();
}
}
```
代码中实现了一个简单的登录和成绩查询功能,用户名为"admin",密码为"123456",如果输入正确,则会显示学生的成绩信息。你可以根据自己的需求进行修改和完善。