- 1 import javax.swing.JFrame;
- 2 import javax.swing.JPanel;
- 3 import javax.swing.border.EmptyBorder;
- 4 import javax.swing.JLabel;
- 5 import javax.swing.JOptionPane;
- 6
- 7 import java.awt.Font;
- 8 import javax.swing.JTextField;
- 9 import javax.swing.SwingConstants;
- 10 import javax.swing.JButton;
- 11 import javax.swing.JPasswordField;
- 12
- 13 import java.awt.event.ActionEvent;
- 14 import java.awt.event.ActionListener;
- 15 import javax.swing.ImageIcon;
- 16 import java.awt.Color;
- 17
- 18 public class Login extends JFrame {
- 19 private JPanel contentPane; //面板
- 20 private JTextField tName;//账号输入框
- 21 private JPasswordField passwordField; //密码输入框
- 22 private JLabel lblBanner; //QQ登录图片
- 23 public static void main(String[] args) {
- 24 // TODO Auto-generated method stub
- 25 Login frame = new Login(); //实例化Login
- 26 frame.setVisible(true); //使窗体显示
- 27 }
- 28 public Login() { //无参构造方法
- 29 setResizable(false); //窗口边界不可拉伸
- 30 setTitle("QQ登录窗口");//设置窗口标题
- 31 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //关闭并退出
- 32 setBounds(100, 100, 406, 289);//初始位置及大小
- 33
- 34 contentPane = new JPanel();//实例化面板
- 35 contentPane.setBackground(Color.white);//背景颜色
- 36 contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));//外边距
- 37 setContentPane(contentPane);//将面板放进窗口
- 38 contentPane.setLayout(null);//布局方式
- 39
- 40 JLabel lblName = new JLabel("用户名:"); //账号标签
- 41 lblName.setFont(new Font("幼圆", Font.PLAIN, 16));//标签字体 类型 大小
- 42 lblName.setBounds(115, 144, 64, 18);//初始位置及大小
- 43 contentPane.add(lblName); //将标签添加进面板
- 44
- 45 tName = new JTextField();//实例化账号输入框
- 46 tName.setBounds(180, 143, 156, 21);//初始位置及大小
- 47 contentPane.add(tName);//添加进面板
- 48 tName.setColumns(10);//最大输入列数
- 49
- 50 tName.addActionListener(new ActionListener() { // 添加动作监听的事件
- 51 @Override
- 52 public void actionPerformed(ActionEvent e) { // 发生操作时
- 53 passwordField.requestFocus(); //当写完账号后回车,自动跳转到密码框
- 54 }
- 55 });
- 56
- 57 JLabel lblPwd = new JLabel("密码:");//密码标签
- 58 lblPwd.setHorizontalAlignment(SwingConstants.RIGHT);//水平居中
- 59 lblPwd.setFont(new Font("幼圆", Font.PLAIN, 16));//字体
- 60 lblPwd.setBounds(125, 175, 54, 15);//位置大小
- 61 contentPane.add(lblPwd);//添加
- 62
- 63 passwordField = new JPasswordField();//实例化
- 64 passwordField.setBounds(180, 172, 156, 21);//位置大小
- 65 contentPane.add(passwordField);//添加
- 66 passwordField.addActionListener(new ActionListener() { // 添加动作监听的事件,输入完密码后回车,作用和登录按钮相同
- 67 @Override
- 68 public void actionPerformed(ActionEvent e) { // 发生操作时
- 69 if (tName.getText().equals("yujie") && passwordField.getText().equals("yujie")) { // “登陆成功”的条件
- 70 JOptionPane.showMessageDialog(null, "登录成功!", "Success", JOptionPane.INFORMATION_MESSAGE); // 弹出框:“登陆成功!”
- 71 } else if (tName.getText().equals("") || passwordField.getText().equals("")) { // 文本框为空时
- 72 JOptionPane.showMessageDialog(null, "用户名或密码不能为空!", "Warning", JOptionPane.WARNING_MESSAGE); // 弹出框:“用户名或密码不能为空!”
- 73 } else { // 以上条件都不满足的时候
- 74 JOptionPane.showMessageDialog(null, "用户名或密码错误!", "Error", JOptionPane.ERROR_MESSAGE); // 弹出框:“用户名或密码错误!”
- 75 }
- 76 }
- 77 });
- 78
- 79 JButton btnLogin = new JButton("登 录"); //登录按钮
- 80 btnLogin.addActionListener(new ActionListener() { // 添加动作监听的事件
- 81 @Override
- 82 public void actionPerformed(ActionEvent e) { // 发生操作时
- 83 if (tName.getText().equals("yujie") && passwordField.getText().equals("yujie")) { // “登陆成功”的条件
- 84 JOptionPane.showMessageDialog(null, "登录成功!", "Success", JOptionPane.INFORMATION_MESSAGE); // 弹出框:“登陆成功!”
- 85 } else if (tName.getText().equals("") || passwordField.getText().equals("")) { // 文本框为空时
- 86 JOptionPane.showMessageDialog(null, "用户名或密码不能为空!", "Warning", JOptionPane.WARNING_MESSAGE); // 弹出框:“用户名或密码不能为空!”
- 87 } else { // 以上条件都不满足的时候
- 88 JOptionPane.showMessageDialog(null, "用户名或密码错误!", "Error", JOptionPane.ERROR_MESSAGE); // 弹出框:“用户名或密码错误!”
- 89 }
- 90 }
- 91 });
- 92 btnLogin.setFont(new Font("幼圆", Font.PLAIN, 16));//按钮字体
- 93 btnLogin.setBounds(125, 216, 156, 23);//位置大小
- 94 contentPane.add(btnLogin);//添加
- 95
- 96
- 97 lblBanner = new JLabel("");//用于添加QQ界面图片
- 98 lblBanner.setIcon(new ImageIcon(Login.class.getResource("QQLogin.png")));//图标
- 99 lblBanner.setBounds(0, 0, 400, 129);//位置大小
- 100 contentPane.add(lblBanner);//添加
- 101
- 102 JLabel lblIcon = new JLabel("");//用于添加头像
- 103 lblIcon.setIcon(new ImageIcon(Login.class.getResource("yujie.png")));//图标
- 104 lblIcon.setBounds(56, 140, 55, 55);//位置大小
- 105 contentPane.add(lblIcon);//添加
- 106 }
- 107 }
感谢阅读(谢谢、Thank you、Salamat Do(撒拉玛特朵)、あリがCám o*n(嘉蒙)とゥ(阿里嘎都)、????? (勘三哈咪瘩)、terima Kasih(