AWT初步—Frame和 Panel

  1. 初识 AWT

      GUI 和 AWT

GUI:Graphics User Interface  图形用户界面

AWT:Abstract Window Toolkit  抽象窗口工具集

之前的程序输出结果均在控制台上显示,现在学习AWT后,可以编程显示图形用户界面。

抽象窗口工具包AWT (Abstract Window Toolkit) 是 API为Java 程序提供的建立图形用户界面GUI (Graphics User Interface)工具集,之所以叫抽象窗口工具集,是因为该图形用户界面可以与系统无关,可以跨平台AWT可用于Java的applet和applications中,它支持图形用户界面编程。

  • 功能包括:

用户界面组件;

事件处理模型;

图形和图像工具,包括形状、颜色和字体类;

布局管理器,可以进行灵活的窗口布局而与特定窗口的尺寸和屏幕分辨率无关;

数据传送类,可以通过本地平台的剪贴板来进行剪切和粘贴。

  • java.awt包

java.awt包中提供了GUI设计所使用的类和接口。
java.awt包提供了基本的java程序的GUI设计工具。主要包括下述三个概念:

组件--Component
容器--Container
布局管理器--LayoutManager

  • 组件、容器和布局管理器

 组件    

      Java的图形用户界面的最基本组成部分是组件(Component),组件是一个可以以图形化的方式显示在屏幕上并能与用户进行交互的对象,例如一个按钮,一个标签等。组件不能独立地显示出来,必须将组件放在一定的容器中才可以显示出来。

常用组件:

      Button 按钮:

      label 标签 :显示文字内容

      TextField 文本框 :接受用户输入

      这些组件都被封装成类,编程时进行实例化被直接调用。

  类java.awt.Component是许多组件类的父类,Component类中封装了组件通用的方法和属性,如图形的组件对象、大小、显示位置、前景色和背景色、边界、可见性等,因此许多组件类也就继承了Component类的成员方法和成员变量,相应的成员方法包括:

        getComponentAt(int x, int y)
   getFont()
   getForeground()
   getName()
   getSize()
   paint(Graphics g)
   repaint()
   update()
   setVisible(boolean b)
   setSize(Dimension d)
   setName(String name)等

容器

 

      容器(Container)也是一个类,实际上是Component的子类,因此容器本身也是一个组件,具有组件的所有性质,但是它的主要功能是容纳其它组件和容器。

常用容器

  容器java.awt.Container是Component的子类,一个容器可以容纳多个组件,并使它们成为一个整体。容器可以简化图形化界面的设计,以整体结构来布置界面。所有的容器都可以通过add()方法向容器中添加组件。

  有三种类型的容器:Window、Panel、ScrollPane,常用的有Panel, Frame, Applet。

 1.Frame 窗口
     继承关系


  


  • 程序实现
public class Demo01 {

    public static void main(String[] args) {
        Frame frame = new Frame("我的第一个窗口");
        frame.setBounds(50, 50, 300, 200);//设置Frame 的大小,距离windows界面上左均为50,宽和高分别为300 200
        frame.setBackground(Color.PINK);
        frame.setVisible(true);//设置Frame为可见

    }

}

Output:

 

      一般我们要生成一个窗口,通常是用Window的子类Frame来进行实例化,而不是直接用到Window类。Frame的外观就像我们平常在windows系统下见到的窗口,有标题、边框、菜单、大小等等。每个Frame的对象实例化以后,都是没有大小和不可见的,因此必须调用setSize( )来设置大小,调用setVisible(true)来设置该窗口为可见的。

  另外,AWT在实际的运行过程中是调用所在平台的图形系统,因此同样一段AWT程序在不同的操作系统平台下运行所看到的图形系统是不一样的。例如在windows下运行,则显示的窗口是windows风格的窗口;而在UNIX下运行时,则显示的是UNIX风格的窗口。

2. Panel 面板

 
 

 

  • 程序实现
public class Demo02 {

    public static void main(String[] args) {
        Frame frame = new Frame("我的panel 界面");
        frame.setBounds(50, 50, 500, 300);
        Panel panel = new Panel();
        panel.setBackground(Color.pink);
        //panel.setBounds(0, 0, 200, 100); 如果要用 panel将整个窗口分块,则要用到布局管理器,不可直接调用实现
        
        frame.add(panel);
        frame.setVisible(true);
        
    }

}

Output:

 

Panel 可以作为容器容纳其他组件,但不可以像Frame 一样独立存在,必须将其添加到其他容器其中。

  • 简单的用户登陆界面的代码实现
public class Demo03 {

    public static void main(String[] args) {
        Frame frame = new Frame("用户登入窗口");
        frame.setBounds(50, 50, 400, 100);
        
        Panel panel = new Panel();
        panel.setBackground(Color.pink);
        frame.add(panel);
        
        Label lable = new Label("用户名");
        TextField textField = new TextField("请输入用户名",20);//20为文本框长度
        Button loginbtn = new Button("确定");
        
        panel.add(lable);
        panel.add(textField);
        panel.add(loginbtn);
        
        frame.setVisible(true);
    }

}

Output:

 

      需要注意的是,Lebel 、TextField、 Button这些组件的添加是有顺序的,以上是一个简单的界面,比较复杂的情况则要使用布局管理器来实现。 并且以上的代码比较简单,实现的只是最基本的显示功能,并不能进行实际的登陆与关闭操作。


  布局管理器(LayoutManager):每个容器都有一个布局管理器,当容器需要对某个组件进行定位或判断其大小尺寸时,就会调用其对应的布局管理器。

为了使我们生成的图形用户界面具有良好的平台无关性,Java语言中,提供了布局管理器这个工具来管理组件在容器中的布局,而不使用直接设置组件位置和大小的方式。

      布局管理器主要包括:FlowLayout,BorderLayout,GridLayout,CardLayout,GridBagLayout

1. FlowLayout(流布局)

  FlowLayout 是Panel,Applet的缺省布局管理器(默认布局管理器)。其组件的放置规律是从上到下、从左到右进行放置,如果容器足够宽,第一个组件先添加到容器中第一行的最左边,后续的组件依次添加到上一个组件的右边,如果当前行已放置不下该组件,则放置到下一行的最左边。

      FlowLayout(FlowLayout.RIGHT,20,40);
  /*第一个参数表示组件的对齐方式,指组件在这一行中的位置是居中对齐、居右对齐还是居左对齐,第二个参数是组件之间的横向间隔,第三个参数是组件之间的纵向间隔,单位是象素。*/
  FlowLayout(FlowLayout.LEFT);
  //居左对齐,横向间隔和纵向间隔都是缺省值5个象素

  FlowLayout();
  //缺省的对齐方式居中对齐,横向间隔和纵向间隔都是缺省值5个象素

  • 代码实现
    public static void main(String[] args) {
        Frame frame = new Frame("FlowLayout");
        frame.setBounds(100, 100, 400, 300);
        frame.setLayout(new FlowLayout());
        
        Button but1 = new Button("button1");
        Button but2 = new Button("button2");
        Button but3 = new Button("button3");
        Button but4 = new Button("button4");
        Button but5 = new Button("button5");
        
        but1.setBackground(Color.blue);
        but2.setBackground(Color.yellow);
        but3.setBackground(Color.red);
        but4.setBackground(Color.green);
        but5.setBackground(Color.pink);
        
        frame.add(but1);
        frame.add(but2);
        frame.add(but3);
        frame.add(but4);
        frame.add(but5);
        
        frame.setVisible(true);
        
    }

}

Output:

      流布局与麻将布局的不同在于麻将布局要自己设置东西南北中的位置布局,而流布局则是按照按钮的调用先后顺序依次排列。默认居中排列,如果要设置为居左或居右排列,只需实例化FlowLayout ,用其实例对象 fl 调用设置队列的方法,将其设置为居左。

即FlowLayout fl = new FlowLayout();
   fl.setAlignment(FlowLayout.LEFT);
   frame.setLayout(fl);

上图为居左放置。

2. BorderLayout(麻将布局)

  BorderLayout 是Window,Frame和Dialog的缺省(默认)布局管理器。BorderLayout布局管理器把容器分成5个区域:North,South,East,West和Center,每个区域只能放置一个组件。各个区域的位置及大小如下图所示:

    

  • 代码实现
public class Demo04 {

    public static void main(String[] args) {
        Frame frame = new Frame("BorderLayt");
        frame.setBounds(100, 100, 400, 300);
        //设置 frame 的布局为BorderLayout
        frame.setLayout(new BorderLayout());
        
        Button btn1 = new Button("button1");
        Button btn2 = new Button("button2");
        Button btn3 = new Button("button3");
        Button btn4 = new Button("button4");    
        Button btn5 = new Button("button5");
        
        btn1.setBackground(Color.blue);
        btn2.setBackground(Color.yellow);
        btn3.setBackground(Color.pink);
        btn4.setBackground(Color.green);
        btn5.setBackground(Color.red);
        
        frame.add(btn1,BorderLayout.EAST);
        frame.add(btn2,BorderLayout.NORTH);
        frame.add(btn3,BorderLayout.SOUTH);
        frame.add(btn4,BorderLayout.WEST);
        frame.add(btn5);
        
        frame.setVisible(true);
    }

}

Output:

 

3. GridLayout(表格布局)

  使容器中各个组件呈网格状布局,平均占据容器的空间。

  在程序中安排组件的位置和大小时,应该注意以下两点:

  • 容器中的布局管理器负责各个组件的大小和位置,因此用户无法在这种情况下设置组件的这些属性。如果试图使用Java 语言提供的setLocation(),setSize(),setBounds() 等方法,则都会被布局管理器覆盖。
  • 如果用户确实需要亲自设置组件大小或位置,则应取消该容器的布局管理器,方法为:setLayout(null);

 

  •  代码实现
public class Demo06 {

    public static void main(String[] args) {
        Frame frame = new Frame("FlowLayout");
        frame.setBounds(100, 100, 400, 300);
        
        GridLayout gl = new GridLayout(3,2,5,5); //设置表格为3行两列排列,表格横向间距为5个像素,纵向间距为5个像素
        frame.setLayout(gl);
        
        
        Button but1 = new Button("button1");
        Button but2 = new Button("button2");
        Button but3 = new Button("button3");
        Button but4 = new Button("button4");
        Button but5 = new Button("button5");
        
        but1.setBackground(Color.blue);
        but2.setBackground(Color.yellow);
        but3.setBackground(Color.red);
        but4.setBackground(Color.green);
        but5.setBackground(Color.pink);
        
        frame.add(but1);
        frame.add(but2);
        frame.add(but3);
        frame.add(but4);
        frame.add(but5);
        
        frame.setVisible(true);
        
    }

}

Output:

  • 综合运用—— 实现一个简单的4*4 计算器界面
public class Demo07 {

    public static void main(String[] args) {
        Frame frame = new Frame("计算器");
        frame.setBounds(100, 100, 300, 420);
        /*因为Frame 的默认布局为BorderLayout 所以这两句代码可以省略。
        BorderLayout bl = new BorderLayout();
        frame.setLayout(bl);
        */
        
        //创建一个文本输入框,然后将其添加到 North 位置
        TextField tf = new TextField();
        frame.add(tf, BorderLayout.NORTH);
        
        //创建一个Panel 面板,并设置布局为 GridLayout
        Panel panel = new Panel();
        GridLayout gl = new GridLayout(4,4,1,1);//创建Panel 面板,大小为4*4,行距与列距均为1 个像素点
        panel.setLayout(gl);
        
        //将 panel 添加到 frame 的 Center 位置
        frame.add(panel, BorderLayout.CENTER);
        Button btn1 = new Button (" 7");
        Button btn2 = new Button (" 8");
        Button btn3 = new Button (" 9");
        Button btn4 = new Button (" +");
        Button btn5 = new Button (" 4");
        Button btn6 = new Button (" 5");
        Button btn7 = new Button (" 6");
        Button btn8 = new Button (" -");
        Button btn9 = new Button (" 1");
        Button btn10 = new Button ("2");
        Button btn11 = new Button ("3 ");
        Button btn12 = new Button ("*");
        Button btn13 = new Button ("0 ");
        Button btn14 = new Button (" .");
        Button btn15 = new Button (" =");
        Button btn16 = new Button (" /");
        
        //将按钮添加到 Panel 面板
        panel.add(btn1);
        panel.add(btn2);
        panel.add(btn3);
        panel.add(btn4);
        panel.add(btn5);
        panel.add(btn6);
        panel.add(btn7);
        panel.add(btn8);
        panel.add(btn9);
        panel.add(btn10);
        panel.add(btn11);
        panel.add(btn12);
        panel.add(btn13);
        panel.add(btn14);
        panel.add(btn15);
        panel.add(btn16);
        
        frame.setVisible( true);
        
    }

}

Output:

  • 总结


   1.Frame是一个顶级窗口。Frame的缺省布局管理器为BorderLayout。

   2.Panel 无法单独显示,必须添加到某个容器中。 Panel 的缺省布局管理器为FlowLayout。

   3.当把Panel 作为一个组件添加到某个容器中后,该Panel 仍然可以有自己的布局管理器。因此,可以利用Panel 使得BorderLayout 中某个区域显示多个组件,达到设计复杂用户界面的目的 。

   4.如果采用无布局管理器 setLayout(null),则必须使用setLocation(),setSize(),setBounds()等方法手工设置组件的大小和位置,此方法会导致平台相关,不鼓励使用。

 

posted @ 2017-01-17 16:08  BU_LIN  阅读(2717)  评论(0编辑  收藏  举报