zoukankan      html  css  js  c++  java
  • 黑马程序员JAVA基础GUI

    一.GUI 概述:

      图形用户界面(Graphics User Interface, GUI) 。在Java 操作图形界面的一个类库称为“抽象工具集合(Abstract Window Toolkit , AWT)”。它为Java应用程序提供了基本图形组件。

      所有和AWT 编程相关的类都放在 java.awt 包以及它的子包中。AWT 编程中有两个基类:Component 和 MenuComponent 。 

    二.操作图形化界面:

      创建图形化界面的一般步骤:

      1、创建frame窗体。

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

      3、定义组件。

      4、将组件添加到窗体中。

      5、添加事件监听机制。

      6、让窗体显示。

      上面步骤中最重要的便是5、添加事件监听机制。 

      事件监听:

      

      事件监听机制的特点:

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

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

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

      4、事件处理。 

     1 import java.awt.*;
     2 import java.awt.event.WindowAdapter;
     3 import java.awt.event.WindowEvent;
     4 import java.awt.event.WindowListener;
     5 public class FrameDemo {
     6     public static void main(String[] args) {
     7 //        创建一个不可见的窗体
     8         Frame frame = new Frame() ; 
     9 //        设置窗体大小。
    10         frame.setSize(500, 400) ; 
    11 //        设置窗体弹出位置:
    12         frame.setLocation(200, 300) ;
    13 //        设置布局。
    14         frame.setLayout(new FlowLayout()) ;
    15 //        创建菜单栏
    16         MenuBar mb = new MenuBar() ;
    17 //        创建菜单
    18         Menu m = new Menu("文件") ;
    19 //        创建子菜单
    20         MenuItem closeItem = new MenuItem("退出"); 
    21         m.add(closeItem) ; 
    22         mb.add(m) ; 
    23 //        创建按钮
    24         Button button = new Button("确定"); 
    25 //        添加菜单
    26         frame.setMenuBar(mb) ;
    27         frame.add(button) ;
    28 //        添加监听器
    29         frame.addWindowListener(new WindowAdapter() {
    30             public void windowClosing(WindowEvent e) {
    31                 System.exit(0) ;
    32             }
    33         });
    34 //        调用 Component 类的 setVisible(boolean) 进行显示。
    35         frame.setVisible(true) ;
    36     }
    37 } 

      代码FrameDemo,简单的演示了操作图形界面的步骤:

    三.AWT 常用组件:

      AWT组件需要调用运行平台的图形界面来创建和平台一致的对等体,所以AWT只能使用所有平台都支持的公共组件。

      AWT提供的基本组件:  

      > Button : 按钮。

      > Canvas : 用于绘画的画布。

      > Checkbox : 复选框组件(也可变为单选框组件)。

      > CheckboxGroup : 用于多个Checkbox 组件组合成一组,一组Checkbox组件将只有一个可以被选中,即全部变为单选框组件。

      > Choice : 下拉式选择框组。

      > Frame : 窗口。

      > Label : 标签类,可以放置提示性文本。

      > List : 列表框组件,可以添加多项条目。

      > Panel :不能单独存在的基本容器,必须放到其他容器中。

      > Scrollbar : 滑动条组件。

      > ScrollPane : 带水平及垂直滚动条的容器组件。

      > TextArea : 多行文本域 。

      > TextField : 单行文本框。

       

    四.图形操作练习:

      练习1:在文本框中输入目录,点击“转到”按钮,将该目录中的文件与文件夹名称列在下面的文本区域

      思路:

      1、画出GUI 窗口。

      2、添加监听事件。

      3、通过File操作文件。

     1 public class ReadFileGUI {
     2 //    根据题目定义需要的组件:
     3     private Frame frame ;
     4     private Button toFileButton ; 
     5     private TextField dirText ; 
     6     private TextArea fileAndDirArea ; 
     7     
     8     //错误提示对话框
     9     private Dialog errDialog ;
    10     private Label inforLabel ;
    11     private Button okButton ;
    12     ReadFileGUI() {
    13          init();
    14     }
    15     //初始化方法:
    16     public void init() {
    17         frame = new Frame("读取文件和文件目录") ;
    18         frame.setBounds(300,200,500,400) ; 
    19         //设置为不能更改窗体大小。
    20         frame.setResizable(false) ;
    21         frame.setLayout(new FlowLayout()) ;
    22         
    23         toFileButton = new Button("转到") ; 
    24         dirText = new TextField(40) ; 
    25         fileAndDirArea = new TextArea(null, 20, 45, TextArea.SCROLLBARS_BOTH) ;
    26         //设置为不可以编辑:
    27         fileAndDirArea.setEditable(false) ;
    28         
    29         //初始化错误提示框。
    30         errDialog = new Dialog(frame, "提示", true) ; 
    31         errDialog.setBounds(300,200,350,100) ; 
    32         errDialog.setResizable(false) ; 
    33         errDialog.setLayout(new FlowLayout()) ;
    34         inforLabel = new Label() ;
    35         okButton = new Button("确定");
    36         errDialog.add(inforLabel) ; 
    37         errDialog.add(okButton) ;
    38         frame.add(dirText) ; 
    39         frame.add(toFileButton) ;
    40         frame.add(fileAndDirArea) ; 
    41         
    42         addListenerEvent();
    43         frame.setVisible(true) ;
    44     }
    45     //添加监听事件
    46     public void addListenerEvent() {
    47         frame.addWindowListener(new WindowAdapter(){
    48             public void windowClosing(WindowEvent e){
    49                 System.exit(0) ;
    50             }
    51         });
    52         //添加 toFileButtion 监听事件
    53         toFileButton.addActionListener(new ActionListener() { 
    54             public void actionPerformed(ActionEvent e) { 
    55                 //按下“转到”按钮,则读取 dirText 中数据,并且打印在fileAdnDirArea 中。
    56                 ReadDirAndFile() ;
    57             }
    58         }) ;
    59         //添加 dirText 监听事件,按回车键便能执行 打印出目录文件
    60         dirText.addKeyListener(new KeyAdapter() {
    61             public void keyPressed(KeyEvent e) {
    62                 if (e.getKeyCode() == e.VK_ENTER)
    63                     ReadDirAndFile() ;
    64             }
    65         }) ;
    66         //添加okButton 监听事件。
    67         okButton.addActionListener(new ActionListener() {
    68             public void actionPerformed(ActionEvent e) {
    69                 errDialog.setVisible(false) ;
    70             }
    71         }) ;
    72     }
    73     public void ReadDirAndFile() {
    74         String str = dirText.getText() ;
    75         File dir = new File(str) ; 
    76         if (dir.exists() && dir.isDirectory()) {    
    77             fileAndDirArea.setText("") ;
    78             String[] fileNames = dir.list() ;  
    79             for (String name : fileNames) { 
    80                 fileAndDirArea.append(name+"\t\n") ;
    81             }
    82         }
    83         else {
    84             inforLabel.setText("路径:'"+str+"' 不正确,请重新输入。") ;
    85             errDialog.setVisible(true) ;
    86 //            fileAndDirArea.setText("路径格式错误!") ;
    87         }
    88     }
    89     
    90     public static void main(String[] args) {
    91         new ReadFileGUI() ;
    92     }
    93 }

      运行结果图:

      

  • 相关阅读:
    Codeforces Round #644 (Div. 3)(A~G)
    【】BZOJ3687: 简单题(dp+bitset)
    [LeetCode] 275. H-Index II
    [LeetCode] 1028. Recover a Tree From Preorder Traversal
    [LeetCode] 1014. Best Sightseeing Pair
    [LeetCode] 468. Validate IP Address
    [LeetCode] 701. Insert into a Binary Search Tree
    [LeetCode] 658. Find K Closest Elements
    [LeetCode] 787. Cheapest Flights Within K Stops
    [LeetCode] 1300. Sum of Mutated Array Closest to Target
  • 原文地址:https://www.cnblogs.com/jbelial/p/3074028.html
Copyright © 2011-2022 走看看