GUI编程之布局

  • AWT(Abstract Windows Toolkit)包含很多类和接口,用于Java Application的GUI编程

  • GUI的各种元素(窗口、按钮、文本框)由java类来实现

  • 使用AWT所涉及的类一般在java.awt包及其子包中

  • container和component是AWT中的两个核心类

  • AWT的体系结构

GUI编程之布局_第1张图片

  • component和container
    • java的图形用户界面的最基本组成部分是component,component类及其子类的对象用来描述一图形化的方式显示在屏幕上并能够与用户进行交互的GUI元素
    • 一般的component对象不能单独显示出来,必须放在一个container对象中才可以显示
    • container是component的子类,container子类对象可以容纳别的component对象
    • container是component对象的子类,container子类对象也可以被当做component对象添加到其他的container对象中
    • 两种常见的container
      • window:其对象表示自由停泊的顶级窗口
      • panel:其对象可以容纳其他component对象,但是不能单独存在,必须被添加到其他container中

Frame

  • Frame是window的子类,有Frame或其子类创建的对象为一个窗口
  • Frame常见的构造方法:
    • Frame()
    • Frame(String s) 创建题目栏为字符串s的窗口
  • 常用方法
setBounds(int x,int y,int width,int height) 设置窗口位置和大小,x,y是左上角坐标,width,height是宽度和高度

setSize(int width,int height) 设置窗口的大小

setLocation(int x,int y) 设置窗口的位置

setBackground(Color c)  设置窗口颜色

setVisible(Boolean b)  设置是否可见

setTitle(String name)  设置窗口名字

setResizable(Boolean b)  设置是否窗口可调大小

实例一

package GUI编程.frame;

import java.awt.*;

public class test01 {
    public static void main(String[] args) {
        Frame frame = new Frame("my first test");
        frame.setLocation(10,20);
        frame.setSize(100,100);
        frame.setBackground(Color.red);
        frame.setVisible(true);  // 默认是false
    }
}

GUI编程之布局_第2张图片

Panel

  • Panel对象可以看作可以容纳component的空间

  • Panel对象可以拥有自己的布局管理器

  • Panel类拥有从其父类继承来的方法

    • setBounds(int x,int y,int width,int height)
    • setSize(int width,int height)
    • setLocation(int x,int y)
    • setBackground(Color c)
    • setLayout(LayoutManager mgr)
  • Panel的构造方法

    • Panel()使用默认的FlowLayout类布局管理器初始化
    • Panel(LayoutManager layout)使用指定的布局管理器初始化
package GUI编程.Panel;

import java.awt.*;

public class test01 {
    public static void main(String[] args) {
        Frame frame = new Frame("test");
        Panel panel = new Panel(null);
        frame.setLayout(null);
        frame.setBounds(300,300,500,500);
        frame.setBackground(new Color(0,0,102));
        panel.setBounds(50,50,400,400);
        panel.setBackground(new Color(204,204,255));
        frame.add(panel);
        frame.setVisible(true);
    }
}

GUI编程之布局_第3张图片

布局管理器

  • java语言中,提供了布局管理器类的对象可以管理
    • component在container中的布局,不必直接设置component的位置与大小
    • 每一个container都有一个布局管理器对象,当容器需要对某个组件进行定位或判断其尺寸大小时,就会调用其对应的布局管理器,即调用container的setLayout方法改变其布局管理器对象
  • AWT提供了5种布局管理器对象
    • FlowLayout
    • BorderLayout
    • GirdLayout
    • CardLayout
    • GridBagLayout
FlowLayout布局管理对象
  • FlowLayout是panel类的默认布局管理器
    • FlowLayout布局管理器对组件逐行定位,行内从左到右,一行排满后换行
    • 不改变组件的大小,按组件原有尺寸显示组件,可设置不同的组件间距、行距以及对齐方式
  • FlowLayout布局管理器的默认对齐方式是居中
  • FlowLayout的构造方法
    new FlowLayout(FlowLayout.RIGHT,20,40)  右对齐,组件之间水平间距20像素,垂直间距40像素
    new FlowLayout(FlowLayout.LEFT)  左对齐,水平和垂直间距为缺省值5
    new FlowLayout() 使用缺省的居中对齐方式,水平和垂直间距为缺省值5

实例:

package GUI编程.layout布局管理器;

import java.awt.*;

public class test01 {
    public static void main(String[] args) {
        Frame frame = new Frame("fdsf");
        Button button = new Button("one");
        Button button1 = new Button("two");
        Button button2 = new Button("three");

        // 采用缺省值,居中,水平、垂直间距为5
        frame.setLayout(new FlowLayout());

        frame.add(button);
        frame.add(button1);
        frame.add(button2);

        frame.setLocation(500,500);
        frame.setSize(200,100);
        frame.setVisible(true);
    }
}

GUI编程之布局_第4张图片

实例二

package GUI编程.layout布局管理器;

import java.awt.*;

public class test02 {
    public static void main(String[] args) {
        Frame frame = new Frame("test");

        FlowLayout flowLayout = new FlowLayout(FlowLayout.CENTER,20,40);

        frame.setLayout(flowLayout);
        frame.setLocation(300,400);;
        frame.setSize(300,200);;
        frame.setBackground(Color.red);

        for (int i=0;i<8;i++){
            frame.add(new Button("btn" + i));
        }

        frame.setVisible(true);
    }
}

GUI编程之布局_第5张图片

BorderLayout布局管理器
  • BorderLayout是Frame类的默认布局管理器
  • BorderLayout将整个容器的布局划分为
    • 东(EAST)
    • 西(WEST)
    • 南(SOUTH)
    • 北(NORTH)
    • 中(CENTER)
      五个区域,组件只能被添加到指定的区域
  • 如果不指定组件的加入部位,则默认加入到CENTER区
  • 每个区只能添加一个组件,如果加入多个,则先加入的背覆盖

实例一(未指定位置,默认添加在center,导致覆盖

package GUI编程.layout布局管理器;

import java.awt.*;

public class test03 {
    public static void main(String[] args) {
        Frame frame = new Frame("test");

        Button button = new Button("east");
        Button button1 = new Button("west");
        Button button2 = new Button("north");
        Button button3 = new Button("south");
        Button button4 = new Button("center");

        frame.add(button);
        frame.add(button1);
        frame.add(button2);
        frame.add(button3);
        frame.add(button4);

        frame.setSize(200,200);;
        frame.setVisible(true);
    }
}

GUI编程之布局_第6张图片
实例二

package GUI编程.layout布局管理器;

import java.awt.*;

public class test03 {
    public static void main(String[] args) {
        Frame frame = new Frame("test");

        Button button = new Button("east");
        Button button1 = new Button("west");
        Button button2 = new Button("north");
        Button button3 = new Button("south");
        Button button4 = new Button("center");

        frame.add(button,BorderLayout.EAST);
        frame.add(button1,BorderLayout.WEST);
        frame.add(button2,BorderLayout.NORTH);
        frame.add(button3,BorderLayout.SOUTH);
        frame.add(button4,BorderLayout.CENTER);

        frame.setSize(200,200);;
        frame.setVisible(true);
    }
}

GUI编程之布局_第7张图片

GridLayout布局管理器
  • GirdLayout型布局管理器将空间划分成规则的矩形网格,每个单元格区域大小相等。组件被添加到每个单元格中,先从左自右填满一行后换行,再从上到下
  • GridLayout构造方法指定分割的行数和列数
    GirdLayout(3,4)   将区域划分为3行4列
    

实例一

package GUI编程.layout布局管理器;

import java.awt.*;

public class test04 {
    public static void main(String[] args) {
        Frame frame = new Frame("test");
        frame.setLayout(new GridLayout(3,2));
        for (int i=0;i<6;i++){
            frame.add(new Button("btn" + i));
        }

        /**
         * 这里没有设置窗口的大小,
         * 通过pack()方法来使其自动适应
         */
        frame.pack();
        frame.setVisible(true);
    }
}

GUI编程之布局_第8张图片

布局管理器总结

  • Frame是一个顶级窗口,Frame的缺省布局管理器为BorderLayout
  • Panel无法独立显示,必须添加到某个容器中
    • Panel的缺省布局管理器为FlowLayout
  • 当把Panel作为一个组件添加到每个容器中后,该Panel仍然可以有自己的布局管理器
  • 使用布局管理器时,布局管理器负责各个组件的大小和位置,因而用户无法在这种情况下设置组件的大小和位置属性,如果试图使用java语言提供的setLocation(),setSize(),setBounds()等方法,则都会被布局管理器覆盖。
  • 如果用户确实需要亲自设置组件的大小或位置,则应取消该容器的布局管理器,方法为 setLayout(null)

练习:

package GUI编程.layout布局管理器;

import javafx.scene.layout.Pane;

import javax.swing.plaf.PanelUI;
import java.awt.*;

public class test05 {
    public static void main(String[] args) {

        Frame frame = new Frame("test");
        frame.setSize(300,200);
        frame.setLocation(500,500);
        frame.setLayout(new GridLayout(2,1));

        Panel panelUp = new Panel(new BorderLayout());
        Panel panelDown = new Panel(new BorderLayout());

        panelUp.add(new Button("btn"),BorderLayout.EAST);
        panelUp.add(new Button("btn"),BorderLayout.WEST);
        Panel panelUp_1 = new Panel(new GridLayout(2,1));
        for (int i=0;i<2;i++){
            panelUp_1.add(new Button("btn"));
        }
        panelUp.add(panelUp_1,BorderLayout.CENTER);

        panelDown.add(new Button("btn"),BorderLayout.EAST);
        panelDown.add(new Button("btn"),BorderLayout.WEST);
        Panel panelDown_1 = new Panel(new GridLayout(2,4));
        for (int i=0;i<2;i++){
            for (int j=0;j<2;j++){
                panelDown_1.add(new Button("btn"));
            }
        }
        panelDown.add(panelDown_1,BorderLayout.CENTER);

        frame.add(panelUp);
        frame.add(panelDown);

        frame.setVisible(true);
    }
}

GUI编程之布局_第9张图片

思路:先用GridLayout将整体分为上下两层,然后每一层使用BorderLayout分为左、中、右三块,中间在使用GridLayout分为上下两层,然后进行填充
GUI编程之布局_第10张图片

你可能感兴趣的:(java学习)