zoukankan      html  css  js  c++  java
  • 黑马程序员图形用户界面GUI 22天(1)

    GUI(图形用户界面)

    GUI
    Graphical User Interface(图形用户接口)。
    用图形的方式,来显示计算机操作的界面,这样更方便更直观。
    CLI
    Command line User Interface (命令行用户接口)
    就是常见的Dos命令行操作。 
    需要记忆一些常用的命令,操作不直观。
    举例:  
    比如:创建文件夹,或者删除文件夹等

    Java为GUI提供的对象都存在java.Awt和javax.Swing两个包中

    Awt与 Swing
    java.Awt:Abstract Window ToolKit (抽象窗口工具包),需要调用本地系统方法实现功能。属重量级控件。
    javax.Swing:在AWT的基础上,建立的一套图形界面系统,其中提供了更多的组件,而且完全由Java实现。增强了移植性,属轻量级控件。

     继承关系图


     布局管理器
     容器中的组件的排放方式,就是布局。
    常见的布局管理器:
    FlowLayout(流式布局管理器)
    从左到右的顺序排列。
    Panel默认的布局管理器。
    BorderLayout(边界布局管理器)
    东,南,西,北,中
    Frame默认的布局管理器。
    GridLayout(网格布局管理器)
    规则的矩阵
    CardLayout(卡片布局管理器)
    选项卡
    GridBagLayout(网格包布局管理器)
    非规则的矩阵

     布局管理器
     容器中的组件的排放方式,就是布局。
    常见的布局管理器:
    FlowLayout(流式布局管理器)
    从左到右的顺序排列。
    Panel默认的布局管理器。
    BorderLayout(边界布局管理器)
    东,南,西,北,中
    Frame默认的布局管理器。
    GridLayout(网格布局管理器)
    规则的矩阵
    CardLayout(卡片布局管理器)
    选项卡
    GridBagLayout(网格包布局管理器)
    非规则的矩阵
    建立一个简单的窗体
    Container常用子类:Window   Panel(面板,不能单独存在。)
    Window常用子类:Frame  Dialog
    简单的窗体创建过程:
    Frame  f = new Frame(“my window”);
    f.setLayout(new FlowLayout());
    f.setSize(500,400);//设置窗体大小
    f.setLocation(300,200);//设置窗体出现在屏幕的位置
    f.setVisible(true);

    示例

    1. import java.awt.*;  
    2. import java.awt.event.*;  
    3. class  MyWinowdemo  
    4. {  
    5.     private Frame f;  
    6.     private TextField tf;  
    7.     private Button but;  
    8.     private TextArea ta;  
    9.   
    10.     MyWindowDemo()  
    11.     {  
    12.      init();  
    13.     }  
    14.   
    15.     public void init()  
    16.     {  
    17.         f= new Frame("my window");  
    18.         f.setBounds(300,100,600,500);//窗口大小  
    19.         f.setLayout(new FlowLayout());// 不设置布局的话是边界布局  
    20.   
    21.         tf = new TextField(30);  
    22.   
    23.         but = new Button("转到");  
    24.   
    25.         ta = new TextArea(15,40);  
    26.   
    27.         f.add(tf);  
    28.         f.add(but);  
    29.         f.add(ta);  
    30.   
    31.         myEvent();  
    32.         f.setVisible(true);  
    33.   
    34.     }  
    35.     private void myEvent()  
    36.     {  
    37.         f.addWindowListener(new WindowAdapter()  
    38.         {  
    39.             public void WindowClosing(WindowEvent e)  
    40.             {  
    41.                 System.exit(0);  
    42.             }  
    43.         }     
    44.         );  
    45.     }  
    46.     public static void main(String[] args)   
    47.     {  
    48.         System.out.println("Hello World!");  
    49.     }  
    50. }  

    进一步完善的窗口 带点击事件

    1. import java.awt.*;  
    2. import java.awt.event.*;  
    3.   
    4.   
    5. class MyMenuDemo   
    6. {  
    7.   
    8.     private Frame f;  
    9.     private MenuBar mb;  
    10.     private Menu m,subMenu;         //初始化一个子条目一个主菜单  
    11.     private MenuItem closeItem,subItem;  
    12.   
    13.     MyMenuDemo()  
    14.     {  
    15.         init();  
    16.     }  
    17.     public void init()  
    18.     {  
    19.         f = new Frame("my window");  
    20.         f.setBounds(300,100,500,600);  
    21.         f.setLayout(new FlowLayout());  
    22.   
    23.         mb = new MenuBar();         //做个菜单条  
    24.   
    25.         m = new Menu("文件");     //初始化一个菜单  
    26.   
    27.         subMenu = new Menu("子菜单"); //  
    28.   
    29.   
    30.         subItem = new MenuItem("子条目"); //  
    31.         closeItem = new MenuItem("退出");  
    32.           
    33.         subMenu.add(subItem);       //子条目添加子菜单  
    34.   
    35.         m.add(subMenu);         //加子条目  
    36.         m.add(closeItem);       //菜单添加菜单项  
    37.         mb.add(m);              //菜单栏+菜单  
    38.   
    39.         f.setMenuBar(mb);    //菜单条加到窗口里面  
    40.   
    41.         myEvent();  
    42.   
    43.         f.setVisible(true); //显示   
    44.   
    45.   
    46.     }  
    47.     private void myEvent()  
    48.     {  
    49.         //关闭窗口方法  
    50.         closeItem.addActionListener(new ActionListener()  
    51.         {  
    52.             public void actionPerformed(ActionEvent e)  
    53.             {  
    54.                 System.exit(0);  
    55.             }  
    56.         });  
    57.         //窗口点叉退出  
    58.         f.addWindowListener(new WindowAdapter()  
    59.         {  
    60.             public void windowClosing(WindowEvent e)  
    61.             {  
    62.                 System.exit(0);   
    63.             }  
    64.         });  
    65.     }  
    66.       
    67.     public static void main(String[] args)   
    68.     {  
    69.         new MyMenuDemo();  
    70.     }  
    71. }  
    1. import java.awt.*;  
    2. import java.awt.event.*;  
    3.   
    4. class  FrameDemo  
    5. {  
    6.     //定义该图形中所需的组建的引用  
    7.     private Frame f;  
    8.     private Button but;  
    9.     FrameDemo()  
    10.     {  
    11.         init();  
    12.     }  
    13.     public void init()  
    14.     {  
    15.         f= new Frame("my frame");  
    16.         //对frame进行基本的设置  
    17.         f.setBounds(300,100,600,500);  
    18.         f.setLayout(new FlowLayout());  
    19.         but = new Button("mt button");  
    20.         //将组建添加到frame中  
    21.         f.add(but);  
    22.         //加载一下窗体上的事件  
    23.         myEvent();  
    24.   
    25.         //显示  
    26.         f.setVisible(true);  
    27.   
    28.           
    29.     }  
    30.     private void myEvent()  
    31.     {  
    32.         f.addWindowListener(new WindowAdapter()  
    33.         {  
    34.             public void windowClosing(WindowEvent e)  
    35.             {  
    36.                 System.exit(0);  
    37.             }  
    38.         }     
    39.         );  
    40.         //让按钮具备退出程序的功能  
    41.         /* 按钮就是事件源 
    42.          那么选择哪个监听器呢  
    43.          通过关闭窗体示例了解到,想要知道哪个组件具备什么样的特有监听器。 
    44.          需要查看该组件对象的功能, 
    45.            查API button的 发先按钮支持一个活动监听   
    46.            只要方法超过三个的都有适配器 
    47.         */  
    48.         but.addActionListener(new ActionListener()  
    49.         {  
    50.             public void actionPerformed(ActionEvent e)  
    51.             {  
    52.                 System.out.println(" 点了按钮退出");  
    53.                 System.exit(0);  
    54.             }  
    55.         }     
    56.         );  
    57.     }  
    58.     public static void main(String[] args)   
    59.     {  
    60.         new FrameDemo();  
    61.         //System.out.println("Hello World!");  
    62.     }  
    63. }  


    事件监听机制的特点:
    1,事件源。
    2,事件。
    3,监听器。
    4,事件处理。
    事件源:就是awt包或者swing包中的那些图形界面组件。
    事件:每一个事件源都有自己特有的对应事件和共性事件。
    监听器:将可以触发某一个事件的动作(不只一个动作)都已经封装到了监听器中。
    以上三者,在java中都已经定义好了。
    直接获取其对象来用就可以了。
    我们要做的事情是,就是对产生的动作进行处理。

    确定事件源(容器或组件)
    通过事件源对象的addXXXListener()方法将侦听器注册到该事件源上。
    该方法中接收XXXListener的子类对象,或者XXXListener的子类XXXAdapter的子类对象。
    一般用匿名内部类来表示。
    在覆盖方法的时候,方法的参数一般是XXXEvent类型的变量接收。
    事件触发后会把事件打包成对象传递给该变量。(其中包括事件源对象。通过getSource()或者,getComponent()获取。)

    1. import java.awt.*;  
    2. import java.awt.event.*;  
    3.   
    4. class  FrameDemo  
    5. {  
    6.     //定义该图形中所需的组建的引用  
    7.     private Frame f;  
    8.     private Button but;  
    9.     FrameDemo()  
    10.     {  
    11.         init();  
    12.     }  
    13.     public void init()  
    14.     {  
    15.         f= new Frame("my frame");  
    16.         //对frame进行基本的设置  
    17.         f.setBounds(300,100,600,500);  
    18.         f.setLayout(new FlowLayout());  
    19.         but = new Button("mt button");  
    20.         //将组建添加到frame中  
    21.         f.add(but);  
    22.         //加载一下窗体上的事件  
    23.         myEvent();  
    24.   
    25.         //显示  
    26.         f.setVisible(true);  
    27.   
    28.           
    29.     }  
    30.     private void myEvent()  
    31.     {  
    32.         f.addWindowListener(new WindowAdapter()  
    33.         {  
    34.             public void windowClosing(WindowEvent e)  
    35.             {  
    36.                 System.exit(0);  
    37.             }  
    38.         }     
    39.         );  
    40.         //让按钮具备退出程序的功能  
    41.         /* 按钮就是事件源 
    42.          那么选择哪个监听器呢  
    43.          通过关闭窗体示例了解到,想要知道哪个组件具备什么样的特有监听器。 
    44.          需要查看该组件对象的功能, 
    45.            查API button的 发先按钮支持一个活动监听   
    46.            只要方法超过三个的都有适配器 
    47.         */  
    48.         but.addActionListener(new ActionListener()  
    49.         {  
    50.             public void actionPerformed(ActionEvent e)  
    51.             {  
    52.                 System.out.println(" 点了按钮退出");  
    53.                 System.exit(0);  
    54.             }  
    55.         }     
    56.         );  
    57.     }  
    58.     public static void main(String[] args)   
    59.     {  
    60.         new FrameDemo();  
    61.         //System.out.println("Hello World!");  
    62.     }  
    63. }  


    菜单

    MenuBar,Menu,MenuItem
    先创建菜单条,再创建菜单,每一个菜单中建立菜单项。
    也可以菜单添加到菜单中,作为子菜单。
    通过setMenuBar()方法,将菜单添加到Frame中。




    菜单示例

      1. package mymenu; // 编译的时候前面加-d  
      2. import java.awt.*;  
      3. import java.awt.event.*;  
      4. import java.io.*;  
      5.   
      6. public class MyMenuTest  
      7. {  
      8.   
      9.     private Frame f;  
      10.     private MenuBar bar;  
      11.     private TextArea ta;  
      12.     private Menu fileMenu;  
      13.     private MenuItem openItem,saveItem,closeItem;  
      14.   
      15.   
      16.     private FileDialog openDia,saveDia;  
      17.   
      18.     private File file;  
      19.     MyMenuTest()  
      20.     {  
      21.         init();  
      22.     }  
      23.     public void init()  
      24.     {  
      25.         f = new Frame("my window");  
      26.         f.setBounds(300,100,650,600);  
      27.   
      28.         bar = new MenuBar();  
      29.   
      30.         ta = new TextArea(); //设置文件区  
      31.   
      32.         fileMenu = new Menu("文件");  
      33.           
      34.         openItem = new MenuItem("打开");  
      35.         saveItem = new MenuItem("保存");  
      36.         closeItem = new MenuItem("退出");  
      37.           
      38.         fileMenu.add(openItem);  
      39.         fileMenu.add(saveItem);  
      40.         fileMenu.add(closeItem);  
      41.         bar.add(fileMenu);  
      42.   
      43.         f.setMenuBar(bar);  
      44.   
      45.   
      46.         openDia = new FileDialog(f,"我要打开",FileDialog.LOAD);  
      47.         saveDia = new FileDialog(f,"我要保存",FileDialog.SAVE);  
      48.   
      49.   
      50.         f.add(ta);  
      51.         myEvent();  
      52.   
      53.         f.setVisible(true);  
      54.   
      55.   
      56.     }  
      57.     private void myEvent()  
      58.     {  
      59.   
      60.         saveItem.addActionListener(new ActionListener()  
      61.         {  
      62.           
      63.             public void actionPerformed(ActionEvent e)  
      64.             {  
      65.                 if(file==null)  
      66.                 {//判断  
      67.                     saveDia.setVisible(true);  
      68.   
      69.                     String dirPath = saveDia.getDirectory();  
      70.                     String fileName = saveDia.getFile();  
      71.                     if(dirPath==null || fileName==null)  
      72.                         return ;  
      73.                     file = new File(dirPath,fileName);  
      74.                 }  
      75.   
      76.                 try  
      77.                 { //写入  
      78.                     BufferedWriter bufw  = new BufferedWriter(new FileWriter(file));  
      79.   
      80.                     String text = ta.getText();  
      81.   
      82.                     bufw.write(text);  
      83.                       
      84.                     bufw.close();  
      85.                 }  
      86.                 catch (IOException ex)  
      87.                 {  
      88.                     throw new RuntimeException();  
      89.                 }  
      90.                   
      91.             }  
      92.         });  
      93.   
      94.   
      95.         openItem.addActionListener(new ActionListener()  
      96.         {  
      97.             public void actionPerformed(ActionEvent e)  
      98.             {  
      99.                 openDia.setVisible(true);  
      100.                 String dirPath = openDia.getDirectory();//找文件路径  
      101.                 String fileName = openDia.getFile();  //找文件名  
      102. //              System.out.println(dirPath+"..."+fileName);  
      103.                 if(dirPath==null || fileName==null)     //点取消返回空       
      104.                     return ;  
      105.   
      106.                 ta.setText(""); //选的的是有效文件的时候清空以前内容  
      107.                 file = new File(dirPath,fileName);//名字和目录都有了就给封装成对象  
      108.   
      109.                 try  
      110.                 {  
      111.                     BufferedReader bufr = new BufferedReader(new FileReader(file));  
      112.   
      113.                     String line = null;  
      114.   
      115.                     while((line=bufr.readLine())!=null)  
      116.                     {  
      117.                         ta.append(line+"\r\n");  
      118.                     }  
      119.   
      120.                     bufr.close();  
      121.                 }  
      122.                 catch (IOException ex)  
      123.                 {  
      124.                     throw new RuntimeException("读取失败");  
      125.                 }  
      126.   
      127.   
      128.             }  
      129.         });  
      130.   
      131.   
      132.   
      133.   
      134.   
      135.         closeItem.addActionListener(new ActionListener()  
      136.         {  
      137.             public void actionPerformed(ActionEvent e)  
      138.             {  
      139.                 System.exit(0);  
      140.             }  
      141.         });  
      142.         f.addWindowListener(new WindowAdapter()  
      143.         {  
      144.             public void windowClosing(WindowEvent e)  
      145.             {  
      146.                 System.exit(0);   
      147.             }  
      148.         });  
      149.     }  
      150.       
      151.     public static void main(String[] args)   
      152.     {  
      153.         new MyMenuTest();  
      154.     }  

  • 相关阅读:
    Lucene in action 笔记 case study
    关于Restful Web Service的一些理解
    Lucene in action 笔记 analysis篇
    Lucene in action 笔记 index篇
    Lucene in action 笔记 term vector
    Lucene in action 笔记 search篇
    博客园开博记录
    数论(算法概述)
    DIV, IFRAME, Select, Span标签入门
    记一个较困难的SharePoint性能问题的分析和解决
  • 原文地址:https://www.cnblogs.com/guwenren/p/3010963.html
Copyright © 2011-2022 走看看