专栏首页Java帮帮-微信公众号-技术文章全总结Java基础-25(04)图形用户界面编程GUI

Java基础-25(04)图形用户界面编程GUI

package cn.itcast.view;(5) import cn.itcast.dao.UserDao; import cn.itcast.dao.impl.UserDaoImpl; import cn.itcast.util.UiUtil; import javax.swing.JOptionPane; public class LoginFrame extends javax.swing.JFrame { /** * Creates new form LoginFrame */ public LoginFrame() { initComponents(); init(); } private void init() { this.setTitle("登录界面"); this.setResizable(false); UiUtil.setFrameCenter(this); UiUtil.setFrameImage(this,"user.jpg"); } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); jtfUsername = new javax.swing.JTextField(); jpfPassword = new javax.swing.JPasswordField(); jButton1 = new javax.swing.JButton(); jButton2 = new javax.swing.JButton(); jButton3 = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jLabel1.setText("用户名:"); jLabel2.setText("密码:"); jButton1.setText("登录"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jButton2.setText("重置"); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton2ActionPerformed(evt); } }); jButton3.setText("注册"); jButton3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton3ActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(42, 42, 42) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel1) .addComponent(jLabel2)) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(jtfUsername, javax.swing.GroupLayout.DEFAULT_SIZE, 174, Short.MAX_VALUE) .addComponent(jpfPassword))) .addGroup(layout.createSequentialGroup() .addGap(64, 64, 64) .addComponent(jButton1) .addGap(18, 18, 18) .addComponent(jButton2) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jButton3))) .addContainerGap(61, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(47, 47, 47) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel1) .addComponent(jtfUsername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(31, 31, 31) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel2) .addComponent(jpfPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(54, 54, 54) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jButton1) .addComponent(jButton2) .addComponent(jButton3)) .addContainerGap(48, Short.MAX_VALUE)) ); pack(); }// </editor-fold>//GEN-END:initComponents private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed RegistFrame rf = new RegistFrame(); rf.setVisible(true); // this.setVisible(false); this.dispose(); }//GEN-LAST:event_jButton3ActionPerformed private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed this.jtfUsername.setText(""); this.jpfPassword.setText(""); }//GEN-LAST:event_jButton2ActionPerformed private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed /* 思路: A:获取用户名和密码 B:正则表达式校验用户名和密码 C:创建对象调用功能,返回一个boolean值 D:根据boolean值给出提示 */ //获取用户名和密码 String username = this.jtfUsername.getText().trim(); String password = this.jpfPassword.getText().trim(); //用正则表达式做数据校验 //定义规则 //用户名规则 String usernameRegex = "[a-zA-z]{5}"; //密码规则 String passwordRegex = "\\w{6,12}"; //校验 if(!username.matches(usernameRegex)) { JOptionPane.showMessageDialog(this, "用户名不满足条件(5个英文字母组成)"); this.jtfUsername.setText(""); this.jtfUsername.requestFocus(); return; } if(!password.matches(passwordRegex)) { JOptionPane.showMessageDialog(this, "密码不满足条件(6-12个任意单词字符)"); this.jpfPassword.setText(""); this.jpfPassword.requestFocus(); return; } //创建对象调用功能,返回一个boolean值 UserDao ud = new UserDaoImpl(); boolean flag = ud.login(username, password); if(flag){ JOptionPane.showMessageDialog(this, "恭喜你登录成功"); // NewJFrame njf = new NewJFrame(); NewJFrame njf = new NewJFrame(username); njf.setVisible(true); this.dispose(); }else { JOptionPane.showMessageDialog(this, "用户名或者密码有误"); this.jtfUsername.setText(""); this.jpfPassword.setText(""); this.jtfUsername.requestFocus(); } }//GEN-LAST:event_jButton1ActionPerformed /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(LoginFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(LoginFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(LoginFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(LoginFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new LoginFrame().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; private javax.swing.JButton jButton3; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JPasswordField jpfPassword; private javax.swing.JTextField jtfUsername; // End of variables declaration//GEN-END:variables }

本文分享自微信公众号 - Java帮帮(javahelp)

原文出处及转载信息见文内详细说明,如有侵权,请联系 yunjia_community@tencent.com 删除。

原始发表时间:2016-12-16

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • Java基础-25(02)图形用户界面编程GUI

    G:一级菜单 package cn.itcast_08; import java.awt.FlowLayout; import java.awt.Frame; ...

    Java帮帮
  • Java基础-25(06)图形用户界面编程GUI

    package cn.itcast.view;(7) import cn.itcast.util.UiUtil; import java.util.loggi...

    Java帮帮
  • Java基础-25(05)图形用户界面编程GUI

    package cn.itcast.view;(6) import cn.itcast.dao.UserDao; import cn.itcast.dao.i...

    Java帮帮
  • Java基础-25(02)图形用户界面编程GUI

    G:一级菜单 package cn.itcast_08; import java.awt.FlowLayout; import java.awt.Frame; ...

    Java帮帮
  • 【转】storm和zookeeper中的节点的关系

    【转】https://www.cnblogs.com/yuananyun/p/5186427.html

    yiduwangkai
  • jpeg编码学习笔记

    felix
  • 修改数据库主键长度导致GOLDENGATE复制进程无法启动提示OGG-01296&ABEND on error 0

    2019-01-11 17:56:34 ERROR OGG-01296 Error mapping from SOURCE.AA to TARGET....

    徐靖
  • 从哈夫曼编码再出发:原理和现实

    对于计算机科班出身的人来说,在大学阶段几乎都学过信息论和算法这两门课,信息论都会讲到香农三大定理以及哈夫曼编码,算法课上会学习二叉树,甚至哈弗曼树。在介绍哈夫曼...

    职场亮哥
  • 小甲鱼《零基础学习Python》课后笔记(十三):元组——戴上了枷锁的列表

    7.上节课我们通过课后作业的形式学习到了“列表推导式”,那请问如果我把中括号改为小括号,会不会得到“元组推导式”呢?

    小火柴棒
  • iOS ipa包进行重新签名安装好brew,先用brew安装ruby,然后用gem安装sigh。安装好后,就可以直接使用了

    honey缘木鱼

扫码关注云+社区

领取腾讯云代金券