java在面板中点击按钮后弹出对话框

 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 }

你可能感兴趣的:(java)