菜鸟的博客

纵有疾风起,人生不言弃。

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

简单图形化界面的制作

复制代码
 1 package test;
 2 
 3 import javax.swing.*;
 4 import java.awt.*;
 5 import java.awt.event.ActionEvent;
 6 import java.awt.event.ActionListener;
 7 
 8 public class Test {
 9     public static void main(String[] args) {
10         // 创建窗口
11         JFrame frame = new JFrame("登录页面");
12 
13         // 设置窗口大小
14         frame.setSize(300, 150);
15 
16         // 设置关闭操作
17         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
18 
19         // 创建面板
20         JPanel panel = new JPanel();
21         frame.add(panel);
22 
23         // 添加用户名标签和文本框
24         JLabel userLabel = new JLabel("用户名:");
25         JTextField userTextField = new JTextField(20);
26         panel.add(userLabel);
27         panel.add(userTextField);
28 
29         // 添加密码标签和密码框
30         JLabel passwordLabel = new JLabel("密码:");
31         JPasswordField passwordField = new JPasswordField(20);
32         panel.add(passwordLabel);
33         panel.add(passwordField);
34 
35         // 添加登录按钮
36         JButton loginButton = new JButton("登录");
37 
38 
39         panel.add(loginButton);
40 
41         // 创建事件处理程序
42         loginButton.addActionListener(new ActionListener() {
43             @Override
44             public void actionPerformed(ActionEvent e) {
45                 String username = userTextField.getText();
46                 String password = new String(passwordField.getPassword());
47 
48                 // 这里可以添加验证逻辑,例如检查用户名和密码是否正确
49                 if (username.equals("your_username") && password.equals("your_password")) {
50                     JOptionPane.showMessageDialog(frame, "登录成功");
51                 } else {
52                     JOptionPane.showMessageDialog(frame, "登录失败,请重试", "错误", JOptionPane.ERROR_MESSAGE);
53                 }
54             }
55         });
56 
57         // 设置窗口可见
58         frame.setVisible(true);
59     }
60 }
复制代码

 

posted on   hhmzd233  阅读(13)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示