1 package javazuoye; 2 import javax.swing.*; 3 4 import java.awt.*; 5 import java.awt.event.*; 6 public class denglujiemian { 7 8 public static void main(String[] args) { 9 // TODO Auto-generated method stub 10 LoginDialog lo = new LoginDialog(); 11 lo.setVisible(true); 12 } 13 14 } 15 public class MainWindow extends JFrame{ 16 17 18 JLabel but1; 19 MainWindow(){ 20 21 setTitle("系统登录"); 22 setBounds(400,200,300,180); 23 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); 24 // setLayout(null); 25 26 27 addWindowListener(new WindowAdapter(){ 28 public void windowClosing(WindowEvent e){ 29 System.exit(0); 30 } 31 }); 32 but1 = new JLabel("登录成功!"); 33 this.add(but1); 34 35 } 36 37 38 } 39 40 public class LoginDialog extends JDialog { 41 JLabel name; 42 JLabel a2; 43 JComboBox a3; 44 JPasswordField password; 45 JButton shi; 46 JButton fou; 47 48 LoginDialog(){ 49 method1(); 50 } 51 52 private void method1() { 53 // TODO Auto-generated method stub 54 setTitle("登录系统"); 55 setBounds(500,250,220,140); 56 setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE); 57 setLayout(null); 58 setModal(true); 59 // this.setResizable(false); 60 61 name = new JLabel("用户名:"); 62 name.setBounds(20,10,50,20); 63 this.add(name); 64 a2 = new JLabel("密码:"); 65 a2.setBounds(20,32,50,20); 66 this.add(a2); 67 68 a3 = new JComboBox(); 69 a3.addItem("huakaihualuo"); 70 a3.addItem("hellow"); 71 a3.setBounds(75,10,105,20); 72 this.add(a3); 73 password = new JPasswordField("666666"); 74 password.setBounds(75,33,105,20); 75 password.setEchoChar(‘*‘); 76 this.add(password); 77 78 shi = new JButton("登录"); 79 shi.setBounds(25,60,60,20); 80 add(shi); 81 shi.addActionListener(new ActionListener(){ 82 83 @Override 84 public void actionPerformed(ActionEvent e) { 85 // TODO Auto-generated method stub 86 login(); 87 } 88 }); 89 fou =new JButton("退出"); 90 fou.setBounds(110,60,60,20); 91 add(fou); 92 fou.addActionListener(new ActionListener(){ 93 94 @Override 95 public void actionPerformed(ActionEvent e) { 96 // TODO Auto-generated method stub 97 logout(); 98 } 99 }); 100 101 } 102 void login(){ 103 String accout = a3.getSelectedItem().toString(); 104 String pwd = new String(password.getPassword()); 105 if(accout.equals("huakaihualuo")&&pwd.equals("666666")){ 106 setVisible(false); 107 showMainWindow(); 108 }else{ 109 JOptionPane.showMessageDialog(this, "用户名是"+accout + ",密码是" + pwd); 110 } 111 } 112 void logout(){ 113 int ret = JOptionPane.showConfirmDialog(this,"退出登录?","取消登录",JOptionPane.YES_NO_OPTION); 114 if(ret ==JOptionPane.YES_NO_OPTION){ 115 dispose(); 116 } 117 } 118 void showMainWindow(){ 119 MainWindow frm = new MainWindow(); 120 frm.setVisible(true); 121 } 122 }
原文:http://www.cnblogs.com/tianyahaijiao1/p/5292993.html