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 }
标签:界面,String,frame,add,new,JLabel,制作,图形化,panel From: https://www.cnblogs.com/muzhaodi/p/17694281.html