Java Swing实现登陆功能

使用swing实现用户登录功能

构建用户登录界面


/**
 * @author FeianLing
 * @date 2019/9/9
 */
@Data
@Slf4j
public class LoginV extends JFrame {

  private static final long serialVersionUID = 5475179439752076273L;
  private Container container = getContentPane();
  private JLabel userLabel = new JLabel("用 户:");
  private JTextField usernameField = new JTextField();
  private JLabel passLabel = new JLabel("密 码:");
  private JPasswordField passwordField = new JPasswordField();
  private JButton okBtn = new JButton("确定");
  private JButton cancelBtn = new JButton("清空");

  public LoginV() {
    setTitle("**系统登陆");
    // 设计窗体大小
    setBounds(600, 200, 400, 200);
    // 添加一块桌布
    container.setLayout(new BorderLayout());
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    // 初始化窗口
    init();
    // 设计窗口可见
    setVisible(true);
  }

  private void init() {
    /*输入部分--Center*/
    JPanel fieldPanel = new JPanel();
    fieldPanel.setLayout(null);
    userLabel.setBounds(50, 20, 50, 20);
    passLabel.setBounds(50, 60, 50, 20);
    fieldPanel.add(userLabel);
    fieldPanel.add(passLabel);
    usernameField.setBounds(110, 20, 160, 20);
    passwordField.setBounds(110, 60, 160, 20);
    fieldPanel.add(usernameField);
    fieldPanel.add(passwordField);
    container.add(fieldPanel, "Center");

    /*按钮部分--South*/
    JPanel buttonPanel = new JPanel();
    buttonPanel.setLayout(new FlowLayout());
    buttonPanel.add(okBtn);
    buttonPanel.add(cancelBtn);
    container.add(buttonPanel, "South");
    listerner();
  }
  /**
   * @author FeianLing
   * @date 2019/9/9
   * @desc 添加按钮的监听
   * @param
   * @return void
   */
  public void listerner() {
    /** 登录系统 */
    okBtn.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            String username = usernameField.getText();
            String password = String.valueOf(passwordField.getPassword());
            if (null == username
                || password == null
                || username.trim().length() == 0
                || password.trim().length() == 0) {
              JOptionPane.showMessageDialog(null, "用户名或密码不能为空");
            }
            //简单校验用户密码
           	if("root".equal(username)&& "admin123".equal(password)){
				JOptionPane.showMessageDialog(null, "登录成功");
			}else{
				JOptionPane.showMessageDialog(null, "登录失败");
			}
              
            
            // 登录操作
          }
        });
    /** 清空输入信息 */
    cancelBtn.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            usernameField.setText("");
            passwordField.setText("");
          }
        });
  }
}

程序入口

/**
 * app程序入口
 *
 * @author FeianLing
 * @date 2019/9/9
 */
public class Main {
  public static void main(String[] args) {
    new LoginV();
  }
}

执行main方法弹出登录串口
在这里插入图片描述
输入登录账号密码即可完成登录功能

  • 7
    点赞
  • 42
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值