Java实现用户登录界面设计,在取消登录处设置事件,点击取消按钮,将输出取消登录
时间: 2023-12-07 21:03:27 浏览: 41
好的,我可以为您提供Java实现用户登录界面设计的代码,并在其中添加取消按钮的事件处理逻辑,实现取消登录时输出提示信息的功能。以下是示例代码:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class LoginFrame extends JFrame {
private JLabel userLabel, passLabel;
private JTextField userText;
private JPasswordField passText;
private JButton loginButton, cancelButton;
public LoginFrame() {
super("用户登录");
// 用户名标签
userLabel = new JLabel("用户名:");
userLabel.setBounds(10, 10, 80, 25);
add(userLabel);
// 用户名文本框
userText = new JTextField(20);
userText.setBounds(100, 10, 160, 25);
add(userText);
// 密码标签
passLabel = new JLabel("密码:");
passLabel.setBounds(10, 40, 80, 25);
add(passLabel);
// 密码文本框
passText = new JPasswordField(20);
passText.setBounds(100, 40, 160, 25);
add(passText);
// 登录按钮
loginButton = new JButton("登录");
loginButton.setBounds(10, 80, 80, 25);
add(loginButton);
// 取消按钮
cancelButton = new JButton("取消");
cancelButton.setBounds(180, 80, 80, 25);
add(cancelButton);
// 取消按钮事件处理
cancelButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("取消登录");
}
});
// 设置窗口大小和布局方式
setSize(300, 150);
setLayout(null);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new LoginFrame();
}
}
```
在上述代码中,我创建了一个`LoginFrame`类,继承自`JFrame`,并在其中添加了用户名标签、用户名文本框、密码标签、密码文本框、登录按钮和取消按钮等组件。在取消按钮的事件处理中,我使用`System.out.println()`方法输出了“取消登录”的提示信息。您可以根据自己的需求,修改事件处理逻辑以实现更多功能。