用java语言写以下代码用户界面大小为200*160像素,居中显示,如下所示: 用户类型包括学生用户和教师用户,缺省为学生用户。 “确定”按钮功能:如果用户名为空,则打印"用户名不可为空!";如果密码为空,则打印"密码不可为空!";如果是学生用户,用户名和密码都是s,登陆成功则打印"学生用户登陆成功";如果是教师用户,用户名和密码都是t;登陆成功则打印"教师用户登陆成功";如果登陆不成功,则打印"用户名不存在或者密码不正确!"。 “取消”按钮功能:将用户名和密码的输入框清空。 “退出”按钮功能:退出程序 ,窗口事件不处理,即单击关闭按钮不能退出程序。
时间: 2024-03-26 10:36:06 浏览: 55
使用Java AWT和Swing实现一个简单的图形用户界面.txt
以下是一个Java Swing代码实现:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class LoginGUI {
private JFrame frame;
private JRadioButton studentButton;
private JRadioButton teacherButton;
private JTextField usernameField;
private JPasswordField passwordField;
public LoginGUI() {
frame = new JFrame("登录");
frame.setSize(200, 160);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
// 居中显示
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int x = (screenSize.width - frame.getWidth()) / 2;
int y = (screenSize.height - frame.getHeight()) / 2;
frame.setLocation(x, y);
// 用户类型
JPanel userTypePanel = new JPanel();
ButtonGroup userTypeGroup = new ButtonGroup();
studentButton = new JRadioButton("学生用户", true);
teacherButton = new JRadioButton("教师用户");
userTypeGroup.add(studentButton);
userTypeGroup.add(teacherButton);
userTypePanel.add(studentButton);
userTypePanel.add(teacherButton);
// 用户名和密码
JPanel usernamePanel = new JPanel();
JLabel usernameLabel = new JLabel("用户名:");
usernameField = new JTextField(10);
usernamePanel.add(usernameLabel);
usernamePanel.add(usernameField);
JPanel passwordPanel = new JPanel();
JLabel passwordLabel = new JLabel("密码:");
passwordField = new JPasswordField(10);
passwordPanel.add(passwordLabel);
passwordPanel.add(passwordField);
// 按钮
JPanel buttonPanel = new JPanel();
JButton okButton = new JButton("确定");
JButton cancelButton = new JButton("取消");
JButton exitButton = new JButton("退出");
okButton.addActionListener(new LoginListener());
cancelButton.addActionListener(new ClearListener());
exitButton.addActionListener(new ExitListener());
buttonPanel.add(okButton);
buttonPanel.add(cancelButton);
buttonPanel.add(exitButton);
// 布局
frame.setLayout(new GridLayout(4, 1));
frame.add(userTypePanel);
frame.add(usernamePanel);
frame.add(passwordPanel);
frame.add(buttonPanel);
}
public void show() {
frame.setVisible(true);
}
private class LoginListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String username = usernameField.getText();
String password = new String(passwordField.getPassword());
if (username.isEmpty()) {
JOptionPane.showMessageDialog(frame, "用户名不可为空!");
} else if (password.isEmpty()) {
JOptionPane.showMessageDialog(frame, "密码不可为空!");
} else if (studentButton.isSelected() && username.equals("s") && password.equals("s")) {
JOptionPane.showMessageDialog(frame, "学生用户登陆成功");
} else if (teacherButton.isSelected() && username.equals("t") && password.equals("t")) {
JOptionPane.showMessageDialog(frame, "教师用户登陆成功");
} else {
JOptionPane.showMessageDialog(frame, "用户名不存在或者密码不正确!");
}
}
}
private class ClearListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
usernameField.setText("");
passwordField.setText("");
}
}
private class ExitListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}
public static void main(String[] args) {
LoginGUI loginGUI = new LoginGUI();
loginGUI.show();
}
}
```
运行代码后,将会得到一个简单的登录界面,用户可以输入用户名和密码,选择用户类型,然后单击“确定”按钮进行登录,单击“取消”按钮清空输入框,单击“退出”按钮退出程序。登录的结果将会以弹窗的方式显示。
阅读全文