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 }
在按钮上添加一个监听器
---------------------------------------
当程序运行时,弹出一个提示框来显示极端条。
初始用户ID和密码设置为123。正确的密码后,正在登录的提示框出现。登录成功后,关闭提示并退出程序。
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 JPasswordField jpUsePwd = new JPasswordField(); 16 17 JButton jbEnter = new JButton(); 18 19 JButton jbExit = new JButton(); 20 21 ///设置登录用户名和密码 22 23 String userID="123"; 24 String userPwd="123"; 25 26 public Test() { 27 super(“用户登录界面”); 28 try { 29 // 设置窗体的大小、位置和可见性 30 31 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 } 43 44 private void jbInit() throws Exception { 45 // 各种控件的初始化,在面板上设置控件位置,并在面板上添加控件 46 getContentPane().setLayout(null); 47 jtUserID.setText(""); 48 jtUserID.setBounds(new Rectangle(182, 50, 141, 22)); 49 jlabel2.setText(用户名:; 50 jlabel2.setBounds(new Rectangle(83, 50, 78, 24)); 51 jlabel3.setText("密 码:"); 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(false); 76 JOptionPane.showMessageDialog(null, “正在登陆..."); 77 Thread.sleep(3000); 78 this.dispose(); 79 }catch(Exception e){ 80 System.out.println(e); 81 } 82 83 } 84 85 ///点击按钮时触发以下方法 86 public void actionPerformed(ActionEvent e) { 87 try{ 88 String command=e.getActionCommand(); 89 if (command.equals(“退出”) { 90 System.exit(0); 91 } else { 92 93 // 实现管理员登录功能 94 95 if (jtUserID.getText().equals("") 96 || new String(jpUsePwd.getPassword()).equals("")) { 97 JOptionPane.showMessageDialog(null, “请输入完整数据”; 98 } 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 }
本文是转载内容,我们尊重原作者对文章的权利。如有内容错误或侵权行为,请联系我们更正或删除文章。