【黑马程序员】图形化界面GUI知识总结

---------------------- ASP.Net+Unity开发.Net培训、期待与您交流! --------------------

图形化界面 GUI

1、GUI概述

GUI(图形用户界面)

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

CLI:Command Line User Interface(命令行用户接口),就是常见的dos命令行操作。需要记忆一些常用的命令,操作不直观。

Java为GUI提供的对象都存在java.awt和javax.swing两个包中。

Awt与Swing

       java.Awt:Abstract Window ToolKit(抽象窗口工具包),需要调用本地系统方法实现功能,属于重量级控件。

       javax.Swing:在AWT的基础上,简历的一套图形界面系统,其中提供了更多的组件,而且完全有Java实现。增强了移植性,属轻量级控件。

轻量级与重量级的区别?

主要是与系统组件的关联大不大,需要调用本地系统方法去实现功能的属于重量级控件;直接调用自身功能的组件属于轻量级控件,与系统平台依耐性不大,增强了可移植性。

继承关系图

Component:构件、组件

       --|Button:按钮

       --|Label:标签

       --|Checkbox:复选框

       --|TextComponent:文本组件

              --|TextArea:单行文本

              --|TextField:多行文本

 

       --|Container:容器

              --|Window:窗口

                     --|Frame:框架

                     --|Dialog:对话框

                            --|FileDialog:文件对话框

              --|Panel:面板

 

Container为容器,是一个特殊的组件,该组件中可以通过add方法添加其他组件进来。

 

2、GUI-布局

布局管理器

       容器中组件的排放方式,就是布局。

常见的布局管理器:

       FlowLayout(流式布局管理器):从左到右的顺序排列,Panel默认的布局管理器。

       BorderLayout(边界式布局管理器):东,南,西,北,中,Frame默认的布局管理器。

       GridLayout(网格布局管理器):规则的矩阵

       CardLayout(卡片布局管理器):选项卡

       GridBagLayout(网格包布局管理器):非规则的矩阵。

 

3、GUI-Frame

事件监听机制组成

事件源(组件)、时间(Event)、监听器(Listener)、时间处理(引发事件后处理方式)

WindowAdapter:接收窗口事件的抽象适配器类,此类中的方法为空,此类存在的目的是方便创建监听器对象。

创建图形化界面:

1.创建frame窗体。

2.对窗体进行基本设置,比如大小,位置,布局等。

3.定义组件。

4.将组件通过窗体的add方法添加到窗体中。

5.让窗体显示,通过setVisible(true)实现

 

4、GUI-事件监听机制

事件监听机制的特点:

1.事件源;

2.事件;

3.监听器;

4.事件处理。

 

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

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

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

以上三者,在java中已经定义好了,直接获取其对象来用就可以了。我们要做得事情就是对产生的动作进行处理。

 

WindowAdapter 接收窗口事件的抽象适配器类,此类中的方法为空,此类存在的目的是方便创建监听器对象。

[java]  view plain copy
  1. import java.awt.*;  
  2. //必须导入事件包  
  3. import java.awt.event.*;  
  4. class AwtDemo1 {  
  5.        public static void main(String[] args) {  
  6.               Frame f = new Frame("测试窗体");//创建窗体  
  7.               f.setSize(500,400);//设置窗体大小  
  8.               f.setLocation(300,200);//设置窗体位置  
  9.               f.setLayout(new FlowLayout());//设置窗体布局  
  10.    
  11.               Button b = new Button("提交");  
  12.               f.add(b);  
  13.               //添加监听器  
  14.               f.addWindowListener(newWindowAdapter(){  
  15.                      public void windowClosing(WindowEvent e){  
  16.                             System.exit(0);  
  17.                      }  
  18.                      public void windowActivated(WindowEvent e){  
  19.                             System.out.println("我活了");  
  20.                      }  
  21.                      public void windowOpened(WindowEvent e){  
  22.                             System.out.println("我被打开了,哈哈");  
  23.                      }  
  24.               });  
  25.               f.setVisible(true);  
  26.        }  
  27. }  
  28. /* 
  29. class MyWinimplements WindowListener 
  30. { 
  31.        //覆盖7个方法,可是只需用到关闭动作,其他动作用不到,但却必须复写 
  32. } 
  33. */  
  34. //因为windowListener的子类WindowAdapter已经实现了WindowListener接口。  
  35. //并覆盖了其中的所有方法,那么我只要继承自windowAdapter覆盖我需要的方法即可。  
  36. class MyWin extends WindowAdapter{ //继承WindowAdapter  
  37.        public void windowClosing(WindowEvent e){  
  38.               //System.out.println("windowclosing-----"+e.toString());  
  39.               System.exit(0);//退出程序  
  40.        }  
  41. }  


5、GUI—Action事件

将事件与图形化界面分离出来

void setBounds(intx, int y, int width, int height)      移动组件并调整其大小

[java]  view plain copy
  1. import java.awt.*;  
  2. importjava.awt.event.*;  
  3. class  FrameDemo{  
  4.        //定义该图形中所需的组件的引用,方便整个作用域  
  5.        private Frame f;  
  6.        private Button but;  
  7.        FrameDemo(){  
  8.               init();  
  9.        }  
  10.        public void init(){  
  11.               f = new Frame("my frame");  
  12.               //对frame进行基本设置。  
  13.               f.setBounds(300,100,600,500);//x,y,w,l  
  14.               f.setLayout(new FlowLayout());//设置窗体布局方式  
  15.    
  16.               but = new Button("mybutton");  
  17.    
  18.               //将组件添加到frame中  
  19.               f.add(but);  
  20.    
  21.               //加载窗体上的事件  
  22.               myEvent();  
  23.               //显示窗体  
  24.               f.setVisible(true);  
  25.        }  
  26.        private void myEvent(){  
  27.               f.addWindowListener(new WindowAdapter(){  
  28.                      public void windowClosing(WindowEvent e){  
  29.                             System.exit(0);  
  30.                      }  
  31.               });  
  32.               /* 
  33.               让按钮具备退出程序的功能 
  34.               按钮就是事件源,那么选择哪个监听器呢? 
  35.               通过关闭窗体事例了解到,想要知道哪个组件具备什么样的特有监听器,需要 
  36.               查看该组件对象的功能。 
  37.               通过查阅button的描述,发现按钮支持一个特有监听addActionListener。 
  38.               */  
  39.               but.addActionListener(new ActionListener(){  
  40.                      public void actionPerformed(ActionEvent e){  
  41.                             System.out.println("退出,我干的");  
  42.                             System.exit(0);  
  43.                      }  
  44.               });  
  45.        }  
  46.        public static void main(String[] args) {  
  47.               new FrameDemo();  
  48.        }  
  49. }  


6、GUI—鼠标键盘事件

consume:使用此事件,以便不会按照默认的方式由产生此事件的源代码来处理此事件。

booleanisControlDown() :返回Control修饰符在此事件上时为down,即ctrl+组合键,判断ctrl键是否被按下

代码演示:

[java]  view plain copy
  1. import java.awt.*;  
  2. importjava.awt.event.*;  
  3. classMouseAndKeyEvent {  
  4.        private Frame f;  
  5.        private Button but;  
  6.        private TextField tf;  
  7.        MouseAndKeyEvent(){  
  8.               init();  
  9.        }  
  10.        public void init(){  
  11.               f = new Frame("myframe");  
  12.    
  13.               f.setBounds(300,100,400,300);  
  14.               f.setLayout(new FlowLayout());  
  15.    
  16.               tf = new TextField(20);  
  17.               but = new Button("mybutton");  
  18.               f.add(tf);  
  19.    
  20.               f.add(but);  
  21.               myEvent();  
  22.               f.setVisible(true);  
  23.        }  
  24.        private void myEvent(){  
  25.               f.addWindowListener(new WindowAdapter(){  
  26.                      public void windowClosing(WindowEvent e){  
  27.                             System.exit(0);  
  28.                      }  
  29.               });  
  30.               //只允许输入数字的键盘监听  
  31.               tf.addKeyListener(new KeyAdapter(){  
  32.                      public void keyPressed(KeyEvente){  
  33.                             int code =e.getKeyCode();  
  34.                             if(!(code>=KeyEvent.VK_0&& code<=KeyEvent.VK_9)){  
  35.                                    System.out.println(KeyEvent.getKeyText(e.getKeyCode())+"..."+"是非法字符");  
  36.                                    e.consume();  
  37.                             }  
  38.                      }  
  39.               });  
  40.               //给but添加一个键盘监听  
  41.               but.addKeyListener(new KeyAdapter(){  
  42.                      public void keyPressed(KeyEvent e){  
  43.                             if(e.isControlDown()&& e.getKeyCode()==KeyEvent.VK_ENTER)  
  44.                                    //System.exit(0);  
  45.                                    System.out.println("ctrl+ enter is run");  
  46.                             //System.out.println(KeyEvent.getKeyText(e.getKeyCode())+"......"+e.getKeyCode());  
  47.                      }  
  48.               });  
  49.               /* 
  50.               //按钮只要是活动的就能执行 
  51.               but.addActionListener(new ActionListener(){ 
  52.                      public void actionPerformed(ActionEvent e){ 
  53.                             System.out.println("actionok"); 
  54.                      } 
  55.               }); 
  56.               but.addMouseListener(new MouseAdapter(){ 
  57.                      private int count =1; 
  58.                      private int clickCount =1; 
  59.                      public void mouseEntered(MouseEvent e){ 
  60.                             System.out.println("鼠标进入该组件---"+count++); 
  61.                      } 
  62.                      public void mouseClicked(MouseEvent e){ 
  63.                             if(e.getClickCount()==2) 
  64.                                    System.out.println("点击动作"+clickCount++); 
  65.                      } 
  66.               }); 
  67.               */  
  68.        }  
  69.    
  70.        public static void main(String[] args) {  
  71.               new MouseAndKeyEvent();  
  72.        }  
  73. }  


7、练习:列出制定目录内容

void append(Stringstr) :将给定文本追加到文本区的当前文本。

Dialog(Dialogowner, String title, boolean modal): 构造一个最初不可见的Dialog,它带有指定的所有者Dialog、标题和模式。

代码演示:

[java]  view plain copy
  1. import java.awt.*;  
  2. import java.awt.event.*;  
  3. import java.io.*;  
  4. class  MyWindowDemo{  
  5.        private Frame f;  
  6.        private TextField tf;  
  7.        private Button but;  
  8.        private TextArea ta;//文本区域rows:行,columns:列  
  9.    
  10.        private Dialog d;  
  11.        private Label lab;//对话框里面不能直接创建文本  
  12.        private Button okBut;  
  13.    
  14.        MyWindowDemo(){  
  15.               init();  
  16.        }  
  17.        public void init(){  
  18.               f = new Frame("mywindow");  
  19.               f.setBounds(300,100,600,500);  
  20.               f.setLayout(new FlowLayout());  
  21.    
  22.               tf = new TextField(60);  
  23.    
  24.               but = new Button("转到");  
  25.               ta = new TextArea(25,70);  
  26.               d = new Dialog(f,"提示信息-self",true);//如果模式为true,则只能操作当前窗口;反之则均可操作。  
  27.               d.setBounds(400,200,300,150);  
  28.               d.setLayout(new FlowLayout());  
  29.    
  30.               lab = new Label();  
  31.               okBut = new Button("确定");  
  32.    
  33.               d.add(lab);  
  34.               d.add(okBut);  
  35.    
  36.               f.add(tf);  
  37.               f.add(but);  
  38.               f.add(ta);  
  39.    
  40.               myEvent();//添加事件  
  41.               f.setVisible(true);  
  42.        }  
  43.        private void myEvent(){  
  44.               okBut.addActionListener(new ActionListener(){  
  45.                      public void actionPerformed(ActionEvent e){  
  46.                             d.setVisible(false);//隐藏窗口  
  47.                      }  
  48.               });  
  49.               d.addWindowListener(new WindowAdapter(){  
  50.                      public void windowClosing(WindowEvent e){  
  51.                             d.setVisible(false);  
  52.                      }  
  53.               });  
  54. <span style="white-space:pre">      </span>//发现一点不足的是,在弹出的对话框中可以再次使用键盘监听机制使其直接利用键盘让对话框消失。  
  55. <span style="white-space:pre">  </span>      okBut.addKeyListener(new KeyAdapter(){  
  56.                      public void keyPressed(KeyEvent e){  
  57.                             if(e.getKeyCode()==KeyEvent.VK_ENTER)  
  58.                                    d.setVisible(false);  
  59.                      }  
  60.               });  
  61.               tf.addKeyListener(new KeyAdapter(){  
  62.                      public void keyPressed(KeyEvent e){  
  63.                             if(e.getKeyCode()==KeyEvent.VK_ENTER)  
  64.                                    showDir();  
  65.                      }  
  66.               });  
  67.               but.addActionListener(new ActionListener(){  
  68.                      public void actionPerformed(ActionEvent e){  
  69.                             /* 
  70.                             String text =tf.getText(); 
  71.                             ta.setText(text);//将输入框中(TextField)的文字添加到输入文本框中(TextArea) 
  72.                             //System.out.println(text); 
  73.                             tf.setText("");//清空输入框中(TextField)的内容 
  74.                             */  
  75.                             showDir();  
  76.                              
  77.                      }  
  78.               });  
  79.               f.addWindowListener(new WindowAdapter(){  
  80.                      public void windowClosing(WindowEvente){  
  81.                             System.exit(0);  
  82.                      }  
  83.               });  
  84.        }  
  85.        private void showDir(){  
  86.               String dirPath =  tf.getText();  
  87.               File dir = new File(dirPath);  
  88.    
  89.               if(dir.exists() &&dir.isDirectory()){  
  90.                      ta.setText("");//将之前内容清空  
  91.                      String[] names =dir.list();  
  92.                      for(String name: names){  
  93.                             ta.append(name+"\r\n");//追加文本  
  94.                      }  
  95.               }else{  
  96.                      String info = "您输入的信息:"+dirPath+"有误,请重新输入";  
  97. <span style="white-space:pre">          </span>//将文本添加到对话框中  
  98.                      lab.setText(info);  
  99.                      d.setVisible(true);  
  100.               }  
  101.        }  
  102.    
  103.        public static void main(String[] args) {  
  104.               new MyWindowDemo();  
  105.        }  
  106. }  


8、GUI—菜单

public classFileDailog extends Dialog :FileDialog类显示一个对话框窗口,用户可以从中选择文件。由于它是一个模式对话框,当应用程序调用其show方法来显示对话框时,它将阻塞其余应用程序,知道用户选择一个文件。

publicFileDialog(Frame parent,String title,int node) :创建一个具有指定标题的文件对话框窗口,用于加载或保存文件。如果node的值为LOAD,那么文件对话框将查找药读取的文件,所显示的文件时当前目录中的文件。如果node的值为SAVE,则文件对话框将查找药写入文件的位置。

代码演示:

[java]  view plain copy
  1. import java.awt.*;  
  2. importjava.awt.event.*;  
  3. import java.io.*;  
  4.    
  5. class  MyMenuDemo1{  
  6.        private Frame f;  
  7.        private MenuBar bar;  
  8.        private TextArea ta;  
  9.        private Menu fileMenu;  
  10.        private MenuItem openItem,saveItem,closeItem;  
  11.    
  12.        private FileDialog openDia,saveDia;  
  13.        private File file;  
  14.    
  15.        MyMenuDemo1(){  
  16.               init();  
  17.        }  
  18.        public void init(){  
  19.               f = new Frame("mywindow");  
  20.               f.setBounds(300,100,500,500);  
  21.               //f.setLayout(new FlowLayout());  
  22.    
  23.               bar = new MenuBar();  
  24.               ta = new TextArea();  
  25.               fileMenu = new Menu("文件");  
  26.                
  27.               openItem = new MenuItem("打开");  
  28.               saveItem = new MenuItem("保存");  
  29.               closeItem = new MenuItem("退出");  
  30.    
  31.               fileMenu.add(openItem);  
  32.               fileMenu.add(saveItem);  
  33.               fileMenu.add(closeItem);  
  34.               bar.add(fileMenu);  
  35.               f.setMenuBar(bar);  
  36.    
  37.               openDia = new FileDialog(f,"我要打开",FileDialog.LOAD);  
  38.               saveDia = new FileDialog(f,"我要保存",FileDialog.SAVE);  
  39.    
  40.               f.add(ta);  
  41.               myEvent();  
  42.               f.setVisible(true);  
  43.    
  44.        }  
  45.        private void myEvent(){  
  46.               //保存文件  
  47.               saveItem.addActionListener(new ActionListener(){  
  48.                      public void actionPerformed(ActionEvent ex){  
  49.                             if(file==null){  
  50.                                    saveDia.setVisible(true);  
  51.    
  52.                                    StringdirPath = saveDia.getDirectory();  
  53.                                    StringfileName = saveDia.getFile();  
  54.                                    if(dirPath==null|| fileName==null)  
  55.                                           return;  
  56.                                    file = new File(dirPath,fileName);  
  57.                             }  
  58.                             try{  
  59.                                    BufferedWriter bufw = new BufferedWriter(new FileWriter(file));  
  60.                                    String text =ta.getText();  
  61.                                    bufw.write(text);  
  62.                                    bufw.close();  
  63.                             }catch (IOException e){  
  64.                                    throw new RuntimeException("写入文件失败");  
  65.                             }  
  66.                      }  
  67.                
  68.               });  
  69.               //打开文件  
  70.               openItem.addActionListener(new ActionListener(){  
  71.                      public void actionPerformed(ActionEvent e){  
  72.                             openDia.setVisible(true);  
  73.                             String dirPath =openDia.getDirectory();  
  74.                             String fileName =openDia.getFile();  
  75.                             //System.out.println(dirPath+"..."+fileName);  
  76.                             if(dirPath==null ||fileName==null)  
  77.                                    return;  
  78.                             ta.setText("");  
  79.                             file = new File(dirPath,fileName);  
  80.                             try{  
  81.                                    BufferedReader bufr = new BufferedReader(new FileReader(file));  
  82.                                    String line =null;  
  83.                                    while((line=bufr.readLine())!=null){  
  84.                                           ta.append(line+"\r\n");  
  85.                                    }  
  86.                                    bufr.close();  
  87.                             }catch (IOException ex){  
  88.                                    throw new RuntimeException("文件读取失败");  
  89.                             }  
  90.    
  91.                      }  
  92.               });  
  93.               //退出程序  
  94.               closeItem.addActionListener(new ActionListener(){  
  95.                      public void actionPerformed(ActionEvent e){  
  96.                             System.exit(0);  
  97.                      }  
  98.               });  
  99.               f.addWindowListener(new WindowAdapter(){  
  100.                      public void windowClosing(WindowEvent e){  
  101.                             System.exit(0);  
  102.                      }  
  103.               });  
  104.        }  
  105.        public static void main(String[] args) {  
  106.               new MyMenuDemo1();  
  107.        }  
  108. }  


9、如何制作jar包

1、javac -d C:\myclass MyMenu.java

javac -d 路径程序名.java

 

2、进入路径myclass后

jar -cvfmymenu.jar mymenu

jar -cvf 需要命名的jar包 包名

 

3、如何给jar包写入相关信息?

1.任意创建一个文本文件,然后写入Main-Class: mymenu.MyMenuTest

大写Main-Class: (换行)包名.程序名(换行)

2.在DOS状态下输入

jar -cvfm my.jar1.txt mymenu

jar -cvfm jar文件需要操作的文件 包名

 

jar在DOS命令下的操作:

-c  创建新的归档文件

-t  列出归档目录

-x  从档案中提取指定的 (或所有) 文件

-u  更新现有的归档文件

-v  在标准输出中生成详细输出

-f  指定归档文件名

-m  包含指定清单文件中的清单信息

-e  为捆绑到可执行 jar文件的独立应用程序指定应用程序入口点

-0  仅存储; 不使用情况任何 ZIP 压缩

-M  不创建条目的清单文件

-i  为指定的 jar 文件生成索引信息

-C  更改为指定的目录并包含其中的文件

 

小结:在给jar包添加配置信息的时候,Main-Class:后一定需要加空格,其次就是包名.类名后一定记得换行,否则程序无法识别


---------------------- ASP.Net+Unity开发.Net培训、期待与您交流! --------------------

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值