Java的awt包的使用实例和Java的一些提示框

一、awt的一些组件

Label l1=new Label("姓名:");  //标签
        Label l2=new Label("密码:");
        TextField tf1=new TextField(20); //文本框,数字是设置其文本框长度
        TextField tf2=new TextField(20); 
        Button b1=new Button("确定");  //按钮
        Button b2=new Button("取消");
        Frame f1=new Frame("登录窗口");  //窗口

 

二、之后对awt的一些组件进行大小设置

f1.setSize(250, 300);        //设置窗口大小
        f1.setLocation(400, 400);
        f1.setLayout(null);
        Panel p=new Panel();
        p.setSize(250, 200);
        p.setLocation(0, 100);        
        p.add(l1);    //添加各种组件    
        p.add(tf1);
        p.add(l2);
        p.add(tf2);
        p.add(b1);
        p.add(b2);
        f1.add(p);
        f1.setVisible(true);  //只有加上这一句各种组件才会显示出来

 

三、上面程序运行之后会出现窗口,但是你点击这个窗口的关闭按钮没有作用,只能强行终止程序。所以我们给窗口的关闭按钮添加事件。下面讲两种方式

3.1、你可以通过实现WindowListener接口来完成这个任务

 1 package temp;
 2 import java.awt.Button;
 3 import java.awt.Frame;
 4 import java.awt.Label;
 5 import java.awt.Panel;
 6 import java.awt.TextField;
 7 import java.awt.event.WindowEvent;
 8 import java.awt.event.WindowListener;
 9 import java.io.BufferedReader;
10 import java.io.File;
11 import java.io.FileReader;
12 import java.io.IOException;
13 import java.util.Scanner;
14 
15 public class smile {
16     public static class DoEvent implements WindowListener{
17 
18         @Override
19         public void windowActivated(WindowEvent arg0) {
20             // TODO Auto-generated method stub
21             
22         }
23 
24         @Override
25         public void windowClosed(WindowEvent arg0) {
26             // TODO Auto-generated method stub
27             
28         }
29 
30         @Override
31         public void windowClosing(WindowEvent arg0) {
32             // TODO Auto-generated method stub
33             System.out.println("窗口关闭");
34             System.exit(0);
35         }
36 
37         @Override
38         public void windowDeactivated(WindowEvent arg0) {
39             // TODO Auto-generated method stub
40             
41         }
42 
43         @Override
44         public void windowDeiconified(WindowEvent arg0) {
45             // TODO Auto-generated method stub
46             
47         }
48 
49         @Override
50         public void windowIconified(WindowEvent arg0) {
51             // TODO Auto-generated method stub
52             
53         }
54 
55         @Override
56         public void windowOpened(WindowEvent arg0) {
57             // TODO Auto-generated method stub
58             
59         }
60         
61     }
62     public static void main(String[] args) {
63         // TODO Auto-generated method stub
64         Label l1=new Label("姓名:");  //标签
65         Label l2=new Label("密码:");
66         TextField tf1=new TextField(20); //文本框
67         TextField tf2=new TextField(20); 
68         Button b1=new Button("确定");  //按钮
69         Button b2=new Button("取消");
70         Frame f1=new Frame("登录窗口");  //窗口
71         
72         DoEvent do1 = new DoEvent();
73         f1.addWindowListener(do1); 
74         
75         f1.setSize(250, 300);        //设置窗口大小
76         f1.setLocation(400, 400);
77         f1.setLayout(null);
78         Panel p=new Panel();
79         p.setSize(250, 200);
80         p.setLocation(0, 100);        
81         p.add(l1);    //添加各种组件    
82         p.add(tf1);
83         p.add(l2);
84         p.add(tf2);
85         p.add(b1);
86         p.add(b2);
87         f1.add(p);
88         f1.setVisible(true);  //只有加上这一句各种组件才会显示出来
89     }        
90 }
91 
92 
93  
View Code

 

 

 

 

上面代码有好多没有方法体的代码,我们可以点击Add unimplemented methods就可以让IDE帮你写出来

 

3.2、你也可以通过WindowAdapter这个类(实现于WindowListener这个接口),这样的话你就可以直接以匿名内部类方式直接new对象

package temp;
import java.awt.Button;
import java.awt.Frame;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

public class smile {
    
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Label l1=new Label("姓名:");  //标签
        Label l2=new Label("密码:");
        TextField tf1=new TextField(20); //文本框
        TextField tf2=new TextField(20); 
        Button b1=new Button("确定");  //按钮
        Button b2=new Button("取消");
        Frame f1=new Frame("登录窗口");  //窗口
        
        f1.addWindowListener(new WindowAdapter(){

            @Override
            public void windowClosing(WindowEvent e) {
                // TODO Auto-generated method stub
                //super.windowClosing(e);
                System.out.println("用户关闭窗口");
                System.exit(0);
            }  //window已经实现了WindowListener的方法
            
        });
        
        f1.setSize(250, 300);        //设置窗口大小
        f1.setLocation(400, 400);
        f1.setLayout(null);
        Panel p=new Panel();
        p.setSize(250, 200);
        p.setLocation(0, 100);        
        p.add(l1);    //添加各种组件    
        p.add(tf1);
        p.add(l2);
        p.add(tf2);
        p.add(b1);
        p.add(b2);
        f1.add(p);
        f1.setVisible(true);  //只有加上这一句各种组件才会显示出来
    }        
}


 

 

或者你可以继承这个WindowAdapter这个适配器类来写

package temp;
import java.awt.Button;
import java.awt.Frame;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

public class smile {
    
    public static void main(String[] args) {
        class DoEvent2 extends WindowAdapter{

            @Override
            public void windowClosing(WindowEvent e) {
                // TODO Auto-generated method stub
                //super.windowClosing(e);
                System.out.println("用户关闭窗口");
                System.exit(0);
            }  //window已经实现了WindowListener的方法
            
        }
        // TODO Auto-generated method stub
        Label l1=new Label("姓名:");  //标签
        Label l2=new Label("密码:");
        TextField tf1=new TextField(20); //文本框
        TextField tf2=new TextField(20); 
        Button b1=new Button("确定");  //按钮
        Button b2=new Button("取消");
        Frame f1=new Frame("登录窗口");  //窗口
        
        f1.addWindowListener(new DoEvent2());
        
        f1.setSize(250, 300);        //设置窗口大小
        f1.setLocation(400, 400);
        f1.setLayout(null);
        Panel p=new Panel();
        p.setSize(250, 200);
        p.setLocation(0, 100);        
        p.add(l1);    //添加各种组件    
        p.add(tf1);
        p.add(l2);
        p.add(tf2);
        p.add(b1);
        p.add(b2);
        f1.add(p);
        f1.setVisible(true);  //只有加上这一句各种组件才会显示出来
    }        
}


 

 

总的来说WindowAdapter是一个类,而WindowListener这一个接口,我们实现接口必须实现其所有抽象方法,但是继承类就不需要写这么多

 

四、我们需要判断输入的登陆用户名和密码是否正确,我们设定用户名和密码来自文件,且它们在一行用空格分开

package temp;
import java.awt.Button;
import java.awt.Frame;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

public class smile {
    public static String name,password;
    public static void main(String[] args) {
        /*------------------------------------------*/
        Scanner scan = new Scanner(System.in);
        String file_road=scan.nextLine();
        File file = new File(file_road);
        BufferedReader reader = null;
        StringBuffer sbf = new StringBuffer();
        try {
            reader = new BufferedReader(new FileReader(file));
            String tempStr;
            while ((tempStr = reader.readLine()) != null) {
                sbf.append(tempStr);
            }
            reader.close();
            String[] s = sbf.toString().split(" ");
            name = s[0];
            password=s[1];
            System.out.println(name);   //输出从文件里面读取的信息
            System.out.println(password);
            //return sbf.toString();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
        /*------------------------------------------*/
    }        
}


 

 

运行的时候输入txt文件的路径,程序就会读取信息并输出

 

五、对按钮添加事件

5.1、当点击确定按钮就要判断用户名和密码是否正确,不正确就清空输入框,并跳出用户名密码错误提示框

5.2、当点击取消按钮,就清空输入框

实现ActionListener接口的actionPerformed方法就可以

  1 package temp;
  2 import java.awt.Button;
  3 import java.awt.Frame;
  4 import java.awt.Label;
  5 import java.awt.Panel;
  6 import java.awt.TextField;
  7 import java.awt.event.ActionEvent;
  8 import java.awt.event.ActionListener;
  9 import java.awt.event.WindowAdapter;
 10 import java.awt.event.WindowEvent;
 11 import java.awt.event.WindowListener;
 12 import java.io.BufferedReader;
 13 import java.io.File;
 14 import java.io.FileReader;
 15 import java.io.IOException;
 16 import java.util.Scanner;
 17 
 18 import javax.swing.JOptionPane;
 19 
 20 public class smile {
 21     public static String name,password;
 22     public static void main(String[] args) {
 23         class DoEvent2 extends WindowAdapter{
 24 
 25             @Override
 26             public void windowClosing(WindowEvent e) {
 27                 // TODO Auto-generated method stub
 28                 //super.windowClosing(e);
 29                 System.out.println("用户关闭窗口");
 30                 System.exit(0);
 31             }  //window已经实现了WindowListener的方法
 32             
 33         }
 34         /*------------------------------------------*/
 35         Scanner scan = new Scanner(System.in);
 36         String file_road=scan.nextLine();
 37         File file = new File(file_road);
 38         BufferedReader reader = null;
 39         StringBuffer sbf = new StringBuffer();
 40         try {
 41             reader = new BufferedReader(new FileReader(file));
 42             String tempStr;
 43             while ((tempStr = reader.readLine()) != null) {
 44                 sbf.append(tempStr);
 45             }
 46             reader.close();
 47             String[] s = sbf.toString().split(" ");
 48             name = s[0];
 49             password=s[1];
 50             System.out.println(name);   //输出从文件里面读取的信息
 51             System.out.println(password);
 52             //return sbf.toString();
 53         } catch (IOException e) {
 54             e.printStackTrace();
 55         } finally {
 56             if (reader != null) {
 57                 try {
 58                     reader.close();
 59                 } catch (IOException e1) {
 60                     e1.printStackTrace();
 61                 }
 62             }
 63         }
 64         /*------------------------------------------*/
 65         
 66         
 67         
 68         // TODO Auto-generated method stub
 69         Label l1=new Label("姓名:");  //标签
 70         Label l2=new Label("密码:");
 71         TextField tf1=new TextField(20); //文本框
 72         TextField tf2=new TextField(20); 
 73         Button b1=new Button("确定");  //按钮
 74         Button b2=new Button("取消");
 75         Frame f1=new Frame("登录窗口");  //窗口
 76         
 77         /*-----------------------------------------*/
 78         class Dobutton implements ActionListener{
 79 
 80             @Override
 81             public void actionPerformed(ActionEvent a) {
 82                 // TODO Auto-generated method stub
 83                 if(a.getSource()==b1)
 84                 {
 85                     if(name.equals(tf1.getText()) && password.equals(tf2.getText()))
 86                     {
 87                         System.out.println("登陆成功!");
 88                         System.out.println("用户名为:"+tf1.getText());
 89                         System.out.println("用户密码为:"+tf2.getText());
 90                         System.exit(0);
 91                     }else {
 92                         //System.out.println("用户名或密码错误!");
 93                         JOptionPane.showMessageDialog(null, "用户名或密码错误!", "Title",JOptionPane.ERROR_MESSAGE);
 94                         tf1.setText(" ");  //设置文本框内容,必须要有一个空格
 95                         tf2.setText(" ");
 96                     }
 97                     
 98                 }
 99                 else if(a.getSource()==b2){
100                     tf1.setText(" ");
101                     tf2.setText(" ");
102                 }
103             }
104             
105         }
106         b1.addActionListener(new Dobutton());
107         b2.addActionListener(new Dobutton());
108         /*-------------------------------------------*/
109         
110         f1.addWindowListener(new DoEvent2());
111         
112         f1.setSize(250, 300);        //设置窗口大小
113         f1.setLocation(400, 400);
114         f1.setLayout(null);
115         Panel p=new Panel();
116         p.setSize(250, 200);
117         p.setLocation(0, 100);        
118         p.add(l1);    //添加各种组件    
119         p.add(tf1);
120         p.add(l2);
121         p.add(tf2);
122         p.add(b1);
123         p.add(b2);
124         f1.add(p);
125         f1.setVisible(true);  //只有加上这一句各种组件才会显示出来
126         
127     }        
128 }
129 
130 
131  
View Code

 

上面程序运行后需要先输入保存着用户名和密码的文件位置

 

六、一些对话框

 

import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

public class qmh {
    public static void main(String[] args) {
        JOptionPane.showMessageDialog(null, "普通对话框");
        JOptionPane.showMessageDialog(null, "警告提示框", "Title",JOptionPane.WARNING_MESSAGE);
        JOptionPane.showMessageDialog(null, "错误提示框", "Title",JOptionPane.ERROR_MESSAGE);
        JOptionPane.showMessageDialog(null, "最基本提示框", "Title",JOptionPane.PLAIN_MESSAGE);
        int n = JOptionPane.showConfirmDialog(null, "你是否喜欢Java?", "Title",JOptionPane.YES_NO_OPTION); 
        JOptionPane.showInputDialog(null," Please input:\n","title",JOptionPane.PLAIN_MESSAGE);
        
        //下拉框Java代码
        Object[] options ={ "苹果", "橘子", "香蕉" };  
        String s = (String) JOptionPane.showInputDialog(null,"请选择你喜欢吃的水果:\n", "水果", JOptionPane.PLAIN_MESSAGE, new ImageIcon("xx.png"), options, "xx");
        
        //自定义提示对话框
        Object[] optionss ={ "喜欢", "不喜欢" };  //自定义按钮上的文字
        int m = JOptionPane.showOptionDialog(null, "你喜欢这篇博客吗?", "Title",JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, optionss, optionss[0]); 
    }
}

 

 

 

 

 

之前未整理的代码:

  1 import java.awt.Button;
  2 import java.awt.FlowLayout;
  3 import java.awt.Frame;
  4 import java.awt.Label;
  5 import java.awt.Panel;
  6 import java.awt.TextField;
  7 import java.awt.event.ActionEvent;
  8 import java.awt.event.ActionListener;
  9 import java.awt.event.WindowAdapter;
 10 import java.awt.event.WindowEvent;
 11 import java.awt.event.WindowListener;
 12 import java.io.BufferedReader;
 13 import java.io.File;
 14 import java.io.FileReader;
 15 import java.io.IOException;
 16 import java.util.Scanner;
 17 
 18 import javax.swing.JOptionPane;
 19 
 20 public class qmh {
 21     public static String name,password;
 22     public static class DoEvent implements WindowListener{
 23 
 24         @Override
 25         public void windowActivated(WindowEvent arg0) {
 26             // TODO Auto-generated method stub
 27             
 28         }
 29 
 30         @Override
 31         public void windowClosed(WindowEvent arg0) {
 32             // TODO Auto-generated method stub
 33             
 34         }
 35 
 36         @Override
 37         public void windowClosing(WindowEvent arg0) {
 38             // TODO Auto-generated method stub
 39             System.out.println("窗口关闭");
 40             System.exit(0);
 41         }
 42 
 43         @Override
 44         public void windowDeactivated(WindowEvent arg0) {
 45             // TODO Auto-generated method stub
 46             
 47         }
 48 
 49         @Override
 50         public void windowDeiconified(WindowEvent arg0) {
 51             // TODO Auto-generated method stub
 52             
 53         }
 54 
 55         @Override
 56         public void windowIconified(WindowEvent arg0) {
 57             // TODO Auto-generated method stub
 58             
 59         }
 60 
 61         @Override
 62         public void windowOpened(WindowEvent arg0) {
 63             // TODO Auto-generated method stub
 64             
 65         }
 66         
 67     }
 68 
 69     public static void main(String[] args) {
 70         
 71         class DoEvent2 extends WindowAdapter{
 72 
 73             @Override
 74             public void windowClosing(WindowEvent e) {
 75                 // TODO Auto-generated method stub
 76                 //super.windowClosing(e);
 77                 System.out.println("用户关闭窗口");
 78                 System.exit(0);
 79             }  //window已经实现了WindowListener的方法
 80             
 81         }
 82         // TODO Auto-generated method stub
 83         //MVC  V视图
 84         //DoEvent do1 = new DoEvent();  //两个使用一个就可以
 85         DoEvent2 do1 = new DoEvent2();
 86         
 87         
 88         Label l1=new Label("姓名:");  //标签
 89         Label l2=new Label("密码:");
 90         TextField tf1=new TextField(20); //文本框
 91         TextField tf2=new TextField(20); 
 92         Button b1=new Button("确定");  //按钮
 93         Button b2=new Button("取消");
 94         Frame f1=new Frame("登录窗口");  //窗口
 95         /*-------------------------------------------------*/
 96         Scanner scan = new Scanner(System.in);
 97         String file_road=scan.nextLine();
 98         File file = new File(file_road);
 99         BufferedReader reader = null;
100         StringBuffer sbf = new StringBuffer();
101         try {
102             reader = new BufferedReader(new FileReader(file));
103             String tempStr;
104             while ((tempStr = reader.readLine()) != null) {
105                 sbf.append(tempStr);
106             }
107             reader.close();
108             String[] s = sbf.toString().split(" ");
109             name = s[0];
110             password=s[1];
111             //return sbf.toString();
112         } catch (IOException e) {
113             e.printStackTrace();
114         } finally {
115             if (reader != null) {
116                 try {
117                     reader.close();
118                 } catch (IOException e1) {
119                     e1.printStackTrace();
120                 }
121             }
122         }
123 
124         
125         
126         
127         
128         /*-------------------------------------------------*/
129         
130         /*-------------------------------------------------*/
131         //f1.addWindowListener(do1);  //注册事件 ,这样注册和下面匿名类注册一样的结果
132         
133         f1.addWindowListener(new WindowAdapter(){
134 
135             @Override
136             public void windowClosing(WindowEvent e) {
137                 // TODO Auto-generated method stub
138                 //super.windowClosing(e);
139                 System.out.println("用户关闭窗口");
140                 System.exit(0);
141             }  //window已经实现了WindowListener的方法
142             
143         });
144         /*-------------------------------------------------*/
145         
146         
147         /*-------------------------------------------------*/
148         //因为tf1在main里面,所以定义类也需要在tf1变量定义的下面
149         class Dobutton implements ActionListener{
150 
151             @Override
152             public void actionPerformed(ActionEvent a) {
153                 // TODO Auto-generated method stub
154                 if(a.getSource()==b1)
155                 {
156                     if(name.equals(tf1.getText()) && password.equals(tf2.getText()))
157                     {
158                         System.out.println("登陆成功!");
159                         System.out.println("用户名为:"+tf1.getText());
160                         System.out.println("用户密码为:"+tf2.getText());
161                         System.exit(0);
162                     }else {
163                         //System.out.println("用户名或密码错误!");
164                         JOptionPane.showMessageDialog(null, "用户名或密码错误!", "Title",JOptionPane.ERROR_MESSAGE);
165                         tf1.setText(" ");  //设置文本框内容,必须要有一个空格
166                         tf2.setText(" ");
167                     }
168                     
169                 }
170                 else if(a.getSource()==b2){
171                     tf1.setText(" ");
172                     tf2.setText(" ");
173                 }
174             }
175             
176         }
177         b1.addActionListener(new Dobutton());
178         b2.addActionListener(new Dobutton());
179         /*-------------------------------------------------*/
180         
181         /*-------------------------------------------------*/
182 //        b2.addActionListener(new ActionListener(){
183 //
184 //            @Override
185 //            public void actionPerformed(ActionEvent arg0) {
186 //                // TODO Auto-generated method stub
187 //                tf1.setText("qwe");
188 //                tf2.setText("qwe");
189 //            }
190 //            
191 //        });
192         
193         /*-------------------------------------------------*/
194         f1.setSize(250, 300);        //设置窗口大小
195         f1.setLocation(400, 400);
196         f1.setLayout(null);
197         Panel p=new Panel();
198         p.setSize(250, 200);
199         p.setLocation(0, 100);        
200         p.add(l1);    //添加各种组件    
201         p.add(tf1);
202         p.add(l2);
203         p.add(tf2);
204         p.add(b1);
205         p.add(b2);
206         f1.add(p);
207         f1.setVisible(true);  //只有加上这一句各种组件才会显示出来
208     }
209 }
View Code

 

posted @ 2020-11-19 17:00  kongbursi  阅读(772)  评论(0编辑  收藏  举报