黑马程序员---java基础-Java之GUI

一、概念

1, Graphical User Interface(图形用户接口)用图形的方式,来显示计算机操作的界面,这样更方便更直观。

2,  用户与计算机交互方式有俩种:除了GUI之外还有CLI(命令行用户接口),例如Dos命令行操作,操作不直观看。

3,  javaGUI提供的对象都存在java.Awtjavax.Swing俩个包中。

 

java.Awt: Abstract Window Toolkit(抽象窗口工具包),需要调用本地系统方法

实现功能,属于重量级控件。

 

javax.Swing:在AWT的基础上,建立的一套图形界面系统,其中提供了更多的组件,而且

完全由java实现。增强了移植性,属轻量级控件。

 

二、GUI的应用

1、基本设置

设置窗体大小:setSize(长,宽);

设置窗体位置:setLocation(距离左,距离上);setBounds(长,宽,距离左,距离上);

设置布局:setLayoutnew FlowLayout());

使窗体可见:setVisibletrue);

2、事件监听机制的特点

<1,事件源。

<2,事件。

<3,监听器。

<4,事件处理。

事件源:就是awt包或者swing包中的那些图形界面组件。

事件:每一个事件源都有自己特有的对应事件和共性事件。

监听器:将可以触发某一个事件的动作(不只一个动作)都已经封装到了监听器中。

以上三者,在java中都已经定义好了。

直接获取其对象来用就可以了。 

我们要做的事情是,就是对产生的动作进行处理

例:

 1 import java.awt.Button;
 2 import java.awt.Frame;
 3 import java.awt.event.ActionEvent;
 4 import java.awt.event.ActionListener;
 5 import java.awt.event.WindowAdapter;
 6 import java.awt.event.WindowEvent;
 7 
 8 public class AwtEventDemo {
 9     public static void main(String[]args)
10     {
11         new AwtEventDemo();
12         //System.out.println();
13     }
14     private Frame f=null;
15     private Button b =null;
16     AwtEventDemo()
17     {
18         init();
19     }
20     public  void init()
21     {
22          f = new Frame("我的窗体");//创建一个名叫我的窗体的窗体
23         f.setSize(500,400);//设置窗体大小
24         f.setLocation(300,200);//设置窗体位置
25         //f.setBounds(500,400,300,200);//综合上面俩项
26         b = new Button("我的按钮");//创建一个名叫我的按钮的按钮
27         f.add(b);//添加按钮
28         f.addWindowListener(new WindowAdapter()//在窗体f中添加一个监听器,
29         {
30             public void windowClosing(WindowEvent e)//复写WindowAdapter的方法windowClosing,将事件打包成对象传递进去。
31             {
32                 System.exit(0);//事件发生即会自动调用这个方法。
33             }
34         });
35         b.addActionListener(new ActionListener()//在按钮b上添加监听器, 
36         {
37             public void actionPerformed(ActionEvent e)//复写掉点击方法  并传入被监听器打包成的对象
38             {
39                 System.exit(0);//处理方式
40             }
41         });
42         f.setVisible(true);//通过加载boolean值来确定显示窗体f 
43     }
44 }

3、对话框:Dialog

此对象在需要时进行调用,如:在错误操作时提示的提示对话框。

例:

  1 /* 
  2 列出指定目录下的内容,当输入的路径不正确时,给出错误提示信息。 
  3 */ 
  4 import java.io.*;  
  5 import java.awt.*;  
  6 import java.awt.event.*;  
  7 class MyWindowDemo   
  8 {  
  9     //定义所需组件引用  
 10     private Frame f;  
 11     private Button but,bok;  
 12     private TextField tf;  
 13     private TextArea ta;  
 14     private Dialog d;  
 15     private Label lab;  
 16     //构造函数  
 17     MyWindowDemo()  
 18     {  
 19         init();  
 20     }  
 21     //窗体基本设置于功能实现  
 22     public void init()  
 23     {  
 24         //组件实例化  
 25         f=new Frame("我的Window");  
 26         but=new Button("跳转");  
 27         tf=new TextField(50);  
 28         ta=new TextArea(30,60);  
 29         //基本设置  
 30         f.setBounds(300,150,500,500);  
 31         f.setLayout(new FlowLayout());  
 32         //添加组件  
 33         f.add(tf);  
 34         f.add(but);  
 35         f.add(ta);  
 36         //窗体事件  
 37         myEvent();  
 38         //窗体显示  
 39         f.setVisible(true);  
 40     }
 41     //注册事件  
 42     public void myEvent()  
 43     {  
 44         //窗体关闭功能  
 45         f.addWindowListener(new WindowAdapter()  
 46         {  
 47             public void windowClosing(WindowEvent e)  
 48             {  
 49                 System.exit(0);  
 50             }  
 51         });  
 52         //“跳转”按钮事件  
 53         but.addActionListener(new ActionListener()  
 54         {  
 55             public void actionPerformed(ActionEvent e)  
 56             {  
 57                 showFile();//列出目录内容在文本区中  
 58             }  
 59         });  
 60         //文本框键盘事件  
 61         tf.addKeyListener(new KeyAdapter()  
 62         {  
 63             public void keyPressed(KeyEvent e)  
 64             {  
 65                 //如果键盘按下Enter键,就将目录内容显示在文本区中  
 66                 if(e.getKeyCode()==KeyEvent.VK_ENTER)  
 67                     showFile();  
 68             }  
 69         });  
 70     }  
 71   
 72     //目录内容显示在文本区中方法  
 73         private void showFile()  
 74         {  
 75             String path=tf.getText();//获取输入的路径  
 76             File dir=new File(path);//将路径封装成对象  
 77             //判断输入的路径是否存在,且是否是文件夹  
 78             if (dir.exists()&&dir.isDirectory())  
 79             {  
 80                 ta.setText("");//清空文本区中的内容---------  
 81                 String names[]=dir.list();//列出目录下的内容  
 82                 //遍历  
 83                 for (String name : names )  
 84                 {  
 85                     ta.append(name+"
");//添加进文本区中  
 86                 }  
 87             }  
 88             else  
 89             {  
 90                 //对话框基本设置  
 91                 d=new Dialog(f,"错误提示",true);  
 92                 d.setBounds(400,200,280,150);  
 93                 d.setLayout(new FlowLayout());  
 94   
 95                 bok=new Button("确定");  
 96                 lab=new Label();  
 97   
 98                 //添加按钮和文本  
 99                 d.add(bok);  
100                 d.add(lab);  
101                 //对话框关闭事件  
102                 d.addWindowListener(new WindowAdapter()  
103                 {  
104                     public void windowClosing(WindowEvent e)  
105                     {  
106                         d.setVisible(false);//退出对话框  
107                     }  
108                 });  
109                 //“确定”按钮事件  
110                 bok.addActionListener(new ActionListener()  
111                 {  
112                     public void actionPerformed(ActionEvent e)  
113                     {  
114                         d.setVisible(false);//按确认键,退出对话框  
115                     }  
116                 });  
117                 String info="您输入的路径:"+path+"是错误的,请重输!";  
118                 lab.setText(info);//设置标签文本内容  
119                 d.setVisible(true);//显示对话框  
120             }  
121         }  
122     public static void main(String[] args)   
123     {  
124         //运行窗体  
125         new MyWindowDemo();  
126     }  
127 }  

4.菜单:menu

aMenu:菜单,继承MenuITem;有右三角的图标存在,可添加MenuMenuItem

bMenuBar:菜单栏,可添加菜单和菜单条目,一般先创建菜单栏,再创建菜单

cMenuItem:菜单条目,无右三角的图标存在,最终菜单项

d,菜单的时间处理和组件相同,可以对类型为MenuIteMenu的对象这个事件源添 加活动监听

     ActionListener,并进行相关处理。

e,通过setMenuBar()方法,将菜单添加到Frame中。

  1 import java.awt.Button;
  2 import java.awt.FlowLayout;
  3 import java.awt.Frame;
  4 import java.awt.TextArea;
  5 import java.awt.TextField;
  6 import java.awt.event.KeyAdapter;
  7 import java.awt.event.KeyEvent;
  8 import java.awt.event.MouseAdapter;
  9 import java.awt.event.MouseEvent;
 10 import java.awt.event.WindowAdapter;
 11 import java.awt.event.WindowEvent;
 12 import java.io.File;
 13 
 14 
 15 public class KeyAndMouseEvent {
 16     
 17     //创建组件引用(全局) 
 18     private Frame f;  //窗体
 19     private Button but;  //按钮
 20     private TextField tf;  //文本框
 21     private TextArea ta;  //文本
 22   
 23     //构造函数  
 24     KeyAndMouseEvent()  
 25     {  
 26         init();  
 27     }  
 28   
 29     //窗体创建与功能实现  
 30     public void init()  
 31     {  
 32         //组件实例化  
 33         f=new Frame("my fame");  
 34         but=new Button("跳转");  
 35         tf=new TextField(50);  
 36         ta=new TextArea(30,58);  
 37   
 38         //设置窗体  
 39         f.setBounds(300,150,500,500); //大小位置 
 40         f.setLayout(new FlowLayout()); // 流适布局
 41   
 42         //将组件添加到窗体中  
 43         f.add(tf);  
 44         f.add(but);  
 45         f.add(ta);  
 46   
 47         //窗体事件  
 48         myEvent();  
 49           
 50         //窗体显示  
 51         f.setVisible(true);  
 52     }  
 53   
 54     //将所有的窗体事件封装到一个方法中 
 55     public void myEvent()  
 56     {  
 57           
 58         f.addWindowListener(new WindowAdapter()  //窗体关闭功能
 59         {  
 60             public void windowClosing(WindowEvent e)  
 61             {  
 62                 System.exit(0);  
 63             }  
 64         });  
 65   
 66         /* 
 67          
 68         but.addActionListener(new ActionListener() //按钮监听
 69         { 
 70             public void actionPerformed(ActionEvent e) 
 71             { 
 72                 System.out.println("按钮在动");  
 73             } 
 74         }); 
 75         */  
 76   
 77        
 78         but.addMouseListener(new MouseAdapter()  //鼠标监听
 79         {  
 80             //int count=0;  
 81             public void mouseClicked(MouseEvent e)  //
 82             {  
 83             //  if(e.getClickCount()==2)  //getClickCount,获取鼠标点击次数
 84             //      System.out.println("鼠标双击按钮");  
 85   
 86             //  System.out.println("鼠标单击按钮");  
 87                 //System.exit(0);  
 88   
 89                 showFile();//显示到文本区中  
 90             }  
 91             /* 
 92             public void mouseEntered(MouseEvent e) //鼠标放入按钮去监听
 93             { 
 94                 System.out.println("鼠标放进入按钮范围"+(++count)+"次"); 
 95             } 
 96             */  
 97         });  
 98   
 99         /* 
100         //按钮键盘事件 
101         but.addKeyListener(new KeyAdapter() //键盘监听事件,
102         { 
103             public void keyPressed(KeyEvent e) 
104             { 
105                 //捕获同时按下ctrl+entry 
106                 if(e.isControlDown()&&e.getKeyCode()==KeyEvent.VK_ENTER) 
107                     System.out.println("Ctrl+Enter.....is Down"); 
108                  
109                 //System.out.println(e.getKeyText(e.getKeyCode())+"---"+e.getKeyCode());     
110             } 
111         }); 
112         */  
113   
114         
115         tf.addKeyListener(new KeyAdapter()  //文本框监听事件
116         {  
117             public void keyPressed(KeyEvent e)  
118             {  
119                 /* 
120                 //判断输入字符是否是数字 
121                 if(e.getKeyCode()<KeyEvent.VK_0||e.getKeyCode()>KeyEvent.VK_9) 
122                 { 
123                     System.out.println("你输入的数字非法,请重数"); 
124                     e.consume();//不显示输入的字符 
125                 } 
126                 */  
127   
128                 if(e.getKeyCode()==KeyEvent.VK_ENTER)  
129                     showFile();//将目录下的内容显示到文本区中  
130             }  
131         });  
132     }  
133   
134     //将路径下的目录或文件列出  
135     private void showFile()  
136     {  
137         String path=tf.getText();//获取文本框内容  
138         File file=new File(path);//将路径封装成文件对象  
139         //判断是否存在  
140         if(file.exists())  
141         {  
142             ta.setText("");//清空文本区中的内容  
143               
144             if(file.isDirectory())  //如果是目录
145             {  
146                 String[] dir=file.list();//列出目录下的文件和目录  
147   
148                 for (String name : dir) //遍历
149                 {  
150                     ta.append(name+"
");//添加到文本区中  
151                 }  
152             }  
153             else  
154                 ta.append(file.getName());//如果是文件,则只显示该文件的名字  
155         }  
156         else  
157             System.out.println("输入路径错误");  
158     }  
159       
160   
161     public static void main(String[] args)   
162     {  
163         //运行窗体  
164         new KeyAndMouseEvent();  
165     }  
166 
167 }

jar包的过程:

1、 编译包 jar cvf jar名 包名

2、 写一个文件main-class: 包名.类名【加个回车】

3、 jar cvfm jar名 文件名 包名

配置jar执行的过程:

工具---文件夹选项---文件类型---新建---文件扩展名jar

然后确定----高级----修改图标-----open------javaw的路径—jar即可!

原文地址:https://www.cnblogs.com/ZkSnug/p/4457617.html