java在面板中点击按钮后弹出对话框
生活随笔
收集整理的這篇文章主要介紹了
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 }轉載于:https://www.cnblogs.com/JoannaQ/archive/2012/08/08/2628010.html
總結
以上是生活随笔為你收集整理的java在面板中点击按钮后弹出对话框的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 宝可梦剑盾龙洞在哪
- 下一篇: 番茄todoapp苹果版怎么打开(好看的