1 import javax.swing.*; 2 import java.awt.event.*; 3 import java.awt.*; 4 5 public class ShowDIalog extends JFrame{ 6 JButton button=new JButton("显示"); 7 public ShowDIalog(){ 8 setLayout(new FlowLayout()); 9 add(button); 10 button.addActionListener(new ActionListener(){ 11 public void actionPerformed(ActionEvent e){ 12 JOptionPane.showMessageDialog(null,"Wath a fucking day!"); 13 } 14 }); 15 setVisible(true); 16 setSize(100,100); 17 } 18 public static void main(String[] args){ 19 ShowDIalog s=new ShowDIalog(); 20 } 21 }
Add a listener for the button
---------------------------------------
When the program is running a pop-up prompt box, show extreme stripe running.
The initial user ID and password are set to 123. After the correct password is login prompt box, etc. after a successful landing, you close the prompt, exit the program.
1 import java.awt.*; 2 import javax.swing.*; 3 import java.awt.event.*; 4 5 public class Test extends JFrame implements ActionListener,Runnable { 6 7 JLabel jLabel1 = new JLabel(); 8 9 JLabel jLabel2 = new JLabel(); 10 11 JTextField jtUserID = new JTextField(); 12 13 JLabel jLabel3 = new JLabel(); 14 15 the JPasswordField jpUsePwd = new new the JPasswordField (); 16 . 17 the JButton jbEnter = new new the JButton (); 18 is . 19 the JButton jbExit = new new the JButton (); 20 is 21 is // set login user name and password 22 is 23 is String userID = "123" ; 24 UserPwd = String "123" ; 25 26 is public the Test () { 27 Super ( "user login interface" ); 28 the try { 29 // set the form of the size, position, visibility 30 31 is jbInit(); 32 this.setVisible(true); 33 this.setSize(410, 300); 34 this.addWindowListener(new WindowAdapter() { // 清空内存 35 public void windowClosing(WindowEvent e) { 36 System.exit(0); 37 } 38 }); 39 } catch (Exception exception) { 40 exception.printStackTrace(); 41 } 42 is } 43 is 44 is Private void the jbInit () throws Exception { 45 // initialization of the control, set the position control, to add the control panel 46 is the getContentPane () setLayout (. Null ); 47 jtUserID.setText ( "" ); 48 jtUserID .setBounds ( new new the Rectangle (182, 50, 141 is, 22 is )); 49 jLabel2.setText ( "user name:" ); 50 jLabel2.setBounds ( new new the Rectangle (83, 50, 78, 24 )); 51 is jLabel3.setText ( "password:" ); 52 jLabel3.setBounds(new Rectangle(81, 91, 78, 24)); 53 jpUsePwd.setBounds(new Rectangle(182, 92, 140, 27)); 54 jbEnter.setBounds(new Rectangle(122, 197, 90, 25)); 55 jbEnter.setText("登陆"); 56 jbExit.setBounds(new Rectangle(217, 197, 90, 25)); 57 jbExit.setText("退出"); 58 this.getContentPane().add(jLabel2); 59 this.getContentPane().add(jLabel3); 60 this.getContentPane().add(jLabel1); 61 this.getContentPane().add(jtUserID); 62 this.getContentPane().add(jpUsePwd); 63 this.getContentPane().add(jbEnter); 64 this.getContentPane().add(jbExit); 65 66 jbEnter.addActionListener(this); 67 jbExit.addActionListener(this); 68 69 } 70 71 72 //多线程控制登陆信息框 73 public void run(){ 74 try{ 75 this.setVisible ( to false ); 76 JOptionPane.showMessageDialog ( null , "is landed in ...." ); 77 the Thread.sleep (3000 ); 78 the this .Dispose (); 79 } the catch (Exception E) { 80 the System. Out.println (E); 81 } 82 83 } 84 85 // when the button is triggered when the following method of 86 public void the actionPerformed (the ActionEvent E) { 87 the try { 88 String Command =e.getActionCommand (); 89 IF (command.equals ( "exit" )) { 90 System.exit (0 ); 91 is } the else { 92 93 // administrator login functions implemented 94 95 IF (jtUserID.getText (). the equals ( "" ) 96 || new new String (jpUsePwd.getPassword ()) the equals. ( "" )) { 97 JOptionPane.showMessageDialog ( null , "enter the full data" ); 98 } the else { 99 IF (jtUserID.getText().equals(userID) 100 && new String(jpUsePwd.getPassword()) 101 .equals(userPwd)) { 102 Thread t=new Thread(this); 103 t.start(); 104 105 106 107 } 108 else { 109 JOptionPane.showMessageDialog(null, "帐号或者密码错误"); 110 jtUserID.setText(""); 111 jpUsePwd.setText(""); 112 } 113 } 114 } 115 }catch(Exception ex){ 116 System.out.println(ex); 117 } 118 } 119 public static void main(String[] args) { 120 new Test(); 121 } 122 }
Reproduced in: https: //www.cnblogs.com/JoannaQ/archive/2012/08/08/2628010.html