GUI
- GUI是图形用户界面,也就是应用程序给用户操作的图形界面,包括AWT和Swing
文章目录
一、AWT概述
-
AWT是用于创建用户界面的一个工具包
-
分类
1.Component:组件,分为基本组件类(按钮、文本框、单选)和容器类Container(特殊组件、可以容纳其他组件)
- 容器类又分为window和panel
2.MenuComponent:所有与菜单相关组件的父类
Window
-
不依赖其他容器而独立存在的容器
-
Frame:创建有标题栏的框架窗口
-
Dialog:创建一个对话框,实现和用户信息的交互
Panel
-
只能存在于其他容器当中(window或其他子类)
import java.awt.*; public class Demo1 { public static void main(String[] args) { //建立新的窗口 Frame f=new Frame("窗体"); //设置窗体的长宽 f.setSize(400,400); //设置窗体在屏幕中的位置 f.setLocation(300,200); //设置颜色 f.setBackground(Color.lightGray); //设置窗体可见 f.setVisible(true); } }
二、布局管理器
- 组件不能单独存在,必须放置在容器中,而组件在容器中的位置和尺寸是由布局管理器来决定的
- 有布局管理器才能有组件
- java.awt包中提供了5种布局管理器,每个容器创建时都会使用一种默认的布局管理器
- 程序中可以通过调用容器对象的
1.FlowLayout流式布局管理器
-
容器会将组件按照从左到右放置
-
当到达容器边界时,会自动将组件放到下一行的开始位置。
-
组件可以按照左对齐,居中对齐(默认)、右对齐的方式排列
-
构造方法
方法声明 功能描述 FlowLayout() 组件默认居中对齐,水平、垂直间距默认为5个单位 FlowLayout(int align) 指定组件相对于容器的对齐方式,水平、垂直间距默认为5个单位 FlowLayout(int align,int hgap,int vgap) 指定组件的对齐方式和水平、垂直间距 -
添加按钮案例
import java.awt.*; public class Demo2 { public static void main(String[] args) { final Frame f=new Frame("标题"); //设置可见 f.setVisible(true); //设置坐标和大小 f.setBounds(300,300,300,200); //设置背景颜色 f.setBackground(Color.lightGray); //设置布局管理器为流式 f.setLayout(new FlowLayout(FlowLayout.LEFT,20,30)); //添加按钮 f.add(new Button("first button")); f.add(new Button("second button")); f.add(new Button("third button")); f.add(new Button("fourth button")); f.add(new Button("fifth button")); f.add(new Button("sixth button")); } }
2.BoederLayout边界布局管理器
-
边界布局管理器将容器划分为东EAST南SOUTH西WEST北NORTH中CENTER五个区域,组件可以被放在5个区域中任意一个
-
不指定添加哪个区域默认添加到CENTER
-
每个区域只能放一个组件,后放入的组件会覆盖先放入的组件
-
添加组件案例
import java.awt.*; public class Border { public static void main(String[] args) { final Frame f=new Frame(); f.setBounds(200,200,200,200); f.setVisible(true); f.setBackground(Color.lightGray); f.setLayout(new BorderLayout()); //创建5个按钮 Button but1=new Button("01"); Button but2=new Button("02"); Button but3=new Button("03"); Button but4=new Button("04"); Button but5=new Button("05"); //将创建好的按钮添加到窗体中,并设置按钮所在的区域 f.add(but1,BorderLayout.EAST); f.add(but2,BorderLayout.WEST); f.add(but3,BorderLayout.NORTH); f.add(but4,BorderLayout.SOUTH); f.add(but5,BorderLayout.CENTER); } }
3.GridLayout网络布局管理器
-
网络布局管理器使用纵横线将容器分成n行m列大小相等的网格,每个网格上放置一个组件
-
添加到容器中的组件首先放置在第一行第一列的网格中,然后在第一行第一列从左到右依次放置其他组件
-
放满一行再放下一行
-
放置在GridLayout的布局管理器中的组件将自动沾满网格的整个区域
-
构造方法
方法声明 功能描述 GirdLayout() 默认只有一行,每个组件占一列 GirdLayout(int rows,int cols) 指定容器的行数和列数 GirdLayout(int rows,int cols,int hgap,int vgap) 指定容器的行数和列数以及组件之间的水平、垂直距离 -
案例
import java.awt.*; public class Grid { public static void main(String[] args) { Frame f=new Frame("窗口"); f.setVisible(true); f.setBounds(200,200,200,200); f.setLayout(new GridLayout(3,3,1,1)); for(int i=0;i<9;i++) { f.add(new Button("按钮"+i)); } } }
4.GridBagLayout网络包布局管理器
-
网络布局包管理器是最灵活、最复杂的布局管理器。
-
允许网格中的组件大小各不相同
-
允许一个组件跨越一个或者多个网格
使用步骤
import java.awt.*; public class GridBag { public static void main(String[] args) { //创建GridBagLayout布局管理器,并使容器采用该布局管理器 Frame container=new Frame("窗口"); Button bt=new Button(); container.setVisible(true); container.setBounds(100,100,1000,1000); GridBagLayout layout = new GridBagLayout(); container.setLayout(layout); //创建GridBagConstraints对象(布局约束条件),并设置该对象的相关属性 GridBagConstraints constraints=new GridBagConstraints(); constraints.gridx=1;//设置网格左上角横向索引 constraints.gridy=1;//设置网格左上角纵向索引 constraints.gridwidth=1;//设置组件横向跨越的网格 constraints.gridheight=1;//设置组件纵向跨越的网格 //调用GridBagLayout对象的setConstraints()方法建立GridBagConstraints对象和受控组件之间的关联 layout.setConstraints(bt,constraints); //向容器中添加组件 container.add(bt); } }
GridBagConstrains常用属性
属性 常用属性 gridx和gridy 设置组件的左上角所在网格的行和列,默认值为当前组件跟在上一个组件后面 gridwigth和gridheight 设置组件横向纵向跨越几个网格,两个属性的默认值都是1,表示当前组件在其行或在其列上为最后一个组件 fill none(不改变组件大小) HORIZONTAL(使组件水平方向足够长以填充显示区域,但高度不变)VERTICAL(使组件垂直方向足够高以填充显示区域,但长度不变) BOTH(使组件足够大,以填充整个显示区域) weightx和weighty 设置组件占领容器中多余的水平方向和垂直方向的空白比例 import java.awt.*; public class Layout extends Frame { public Layout(String title) { GridBagLayout layout=new GridBagLayout(); GridBagConstraints c=new GridBagConstraints(); this.setLayout(layout); c.fill=GridBagConstraints.BOTH;//设置组件可以纵向横向拉伸 c.weightx=1;//设置横向权重为1 c.weighty=1;//设置纵向权重为1 this.addComponent("bt1",layout,c); this.addComponent("bt2",layout,c); this.addComponent("bt3",layout,c); //添加组件是本行的最后一个组件 c.gridwidth=GridBagConstraints.REMAINDER; this.addComponent("bt4",layout,c); c.weightx=0;//设置横向权重为0 c.weighty=0;//设置纵向权重为0 this.addComponent("bt5",layout,c); c.gridwidth=1;//设置组件横跨一个网格 this.addComponent("bt6",layout,c); //添加组件是本行的最后一个组件 c.gridwidth=GridBagConstraints.REMAINDER; this.addComponent("bt7",layout,c); c.weightx=2;//设置横向权重为2 c.weighty=2;//设置纵向权重为2 c.gridheight=3;//纵跨3个网格 c.gridwidth=1;//横跨1个网格 this.addComponent("bt8",layout,c); //添加组件是本行的最后一个组件 c.gridwidth=GridBagConstraints.REMAINDER; c.gridheight=1;//设置组件横跨一个网格 this.addComponent("bt9",layout,c); this<