[Java] 登录与注册 →→→→→进入此内容的聊天室

来自 , 2020-10-21, 写在 Java, 查看 193 次.
URL http://www.code666.cn/view/97d01458
  1. package com.lzy;
  2. /*
  3.  * 登录窗口
  4.  */
  5. import java.awt.Color;
  6. import java.awt.GridLayout;
  7. import java.awt.event.ActionEvent;
  8. import java.awt.event.ActionListener;
  9.  
  10. import javax.swing.ButtonGroup;
  11. import javax.swing.JButton;
  12. import javax.swing.JFrame;
  13. import javax.swing.JLabel;
  14. import javax.swing.JOptionPane;
  15. import javax.swing.JPanel;
  16. import javax.swing.JPasswordField;
  17. import javax.swing.JRadioButton;
  18. import javax.swing.JTextField;
  19.  
  20. import com.lzy.adm.AdminView;
  21. import com.lzy.sql.GetSQL;
  22. import com.lzy.stuExam.Student;
  23.  
  24. public class LoginView extends JFrame implements ActionListener {
  25.        
  26.         JLabel jlb1,jlb2,jlb3;
  27.         JTextField jtf;
  28.         JPasswordField jpf;
  29.         JButton jb1,jb2;
  30.         JRadioButton jrb1,jrb2;
  31.         ButtonGroup bg;
  32.         JPanel jp1,jp2,jp3,jp4;
  33.        
  34.         Color color=new Color(100, 155,225);
  35.        
  36.         LoginView(){
  37.                
  38.                 jlb1=new JLabel("用户名:");
  39.                 jlb2=new JLabel("密    码:");
  40.                 jlb3=new JLabel("权    限:");
  41.                
  42.                 jtf=new JTextField(10);
  43.                 jpf=new JPasswordField(10);
  44.                
  45.                 jb1=new JButton("登录");
  46.                 jb2=new JButton("退出");
  47.                 //注册监听
  48.                 jb1.addActionListener(this);
  49.                 jb2.addActionListener(this);
  50.                
  51.                 jrb1=new JRadioButton("学生",true);
  52.                 jrb2=new JRadioButton("管理员");
  53.                 //透明按钮
  54.                 jrb1.setContentAreaFilled(false);
  55.                 jrb2.setContentAreaFilled(false);
  56.                 //按钮组
  57.                 bg=new ButtonGroup();
  58.                
  59.                 bg.add(jrb1);
  60.                 bg.add(jrb2);
  61.                
  62.                 //注册监听
  63.                 jrb1.addActionListener(this);
  64.                 jrb2.addActionListener(this);
  65.                
  66.                 jp1=new JPanel();
  67.                 jp2=new JPanel();
  68.                 jp3=new JPanel();
  69.                 jp4=new JPanel();
  70.                
  71.                 jp1.add(jlb1);
  72.                 jp1.add(jtf);
  73.                
  74.                 jp2.add(jlb2);
  75.                 jp2.add(jpf);
  76.                
  77.                 jp3.add(jlb3);
  78.                 jp3.add(jrb1);
  79.                 jp3.add(jrb2);
  80.                
  81.                 jp4.add(jb1);
  82.                 jp4.add(jb2);
  83.                
  84.                 jp1.setBackground(color);
  85.                 jp2.setBackground(color);
  86.                 jp3.setBackground(color);
  87.                 jp4.setBackground(color);
  88.                
  89.                 this.setLayout(new GridLayout(4,1));
  90.                 this.add(jp1);
  91.                 this.add(jp2);
  92.                 this.add(jp3);
  93.                 this.add(jp4);
  94.                 this.setTitle("在线考试系统");
  95.                 this.setSize(300,250);
  96.                 this.setLocationRelativeTo(null);
  97.                 this.setVisible(true);
  98.                 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  99.                
  100.                
  101.         }
  102.        
  103.         @Override
  104.         public void actionPerformed(ActionEvent e) {
  105.                 // TODO Auto-generated method stub
  106.                 if(e.getActionCommand()=="退出"){
  107.                         System.exit(0);
  108.                 }else {
  109.                         if(e.getActionCommand()=="登录"){
  110.                                 if(!(jtf.getText().isEmpty()) && !(jpf.getText().isEmpty())){
  111.                                         //连接数据库
  112.                                         GetSQL.connectSQL();
  113.                                         //权限选择
  114.                                         if(jrb1.isSelected()){
  115.                                                 //查找用户
  116.                                                 GetSQL.stu(jtf.getText());
  117.                                                 if(GetSQL.pwd==null){
  118.                                                         this.clear();
  119.                                                 }else {
  120.                                                         if(GetSQL.pwd.equals(jpf.getText().trim())){
  121.                                                                 JOptionPane.showMessageDialog(null, "登录成功!");
  122.                                                                 this.clear();
  123.                                                                 this.dispose();
  124.                                                                 new Student();
  125.                                                         }else {
  126.                                                                 JOptionPane.showMessageDialog(null, "密码不正确,请重新输入!");
  127.                                                                 this.clear();
  128.                                                         }
  129.                                                 }
  130.                                         }
  131.                                         //权限选择
  132.                                         if(jrb2.isSelected()){
  133.                                                 //查找用户
  134.                                                 GetSQL.adm(jtf.getText());
  135.                                                 if(GetSQL.pwd==null){
  136.                                                         this.clear();
  137.                                                 }else {
  138.                                                         if(GetSQL.pwd.equals(jpf.getText().trim())){
  139.                                                                 JOptionPane.showMessageDialog(null, "登录成功!");
  140.                                                                 this.clear();
  141.                                                                 this.dispose();
  142.                                                                 new AdminView();
  143.                                                         }else {
  144.                                                                 JOptionPane.showMessageDialog(null, "密码不正确,请重新输入!");
  145.                                                                 this.clear();
  146.                                                         }
  147.                                                 }
  148.                                         }
  149.                                 }else {
  150.                                         JOptionPane.showMessageDialog(null, "请输入所有信息");
  151.                                         this.clear();
  152.                                 }
  153.                         }
  154.                 }
  155.         }
  156.        
  157.         //清空
  158.         private void clear(){
  159.                 jtf.setText("");
  160.                 jpf.setText("");
  161.         }
  162.        
  163. //      public static void main (String args[]){
  164. //              new Login();
  165. //      }
  166.        
  167. }
  168.  
  169.  
  170. package com.lzy;
  171.  
  172. public class Main {
  173.         public static void main (String args[]){
  174.                 new LoginView();
  175.         }
  176. }
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  

回复 "登录与注册"

这儿你可以回复上面这条便签

captcha