Java--AWT

容器

Java图形用户界面最基本组成部分是组件(Component),组件不能独立显示出来,必须将组件放在一定的容器才能显示出来。容器java.awt.Container是Component的子类,在一个容器中可以容纳多个组件。所有容器可以通过方法add()向容器中添加组件。

类Component有四个常用方法设置组件的大小、位置和可见性:

setLocation(int x,int y):设置组件位置。

setSize(int width,int height):设置组件大小。

setBounds(int x,int y,int width,int height):同时设置组件的位置、大小。

setVisible(Boolean b):设置该组件的可见性。

AWT中提供两种主要的容器类型:

Window: 可独立存在的顶级类型;

Panel: 可作为容器容纳其他组件,但不能独立存在,必须被添加到其他容器中(如Window、Panel或者Applet等)。

AWT常用的组件:Frame、Panel、ScrollPane.

1、Frame

Window类的子类

特征:

1、Frame对象有标题,允许通过拖拉来改变窗口的位置、大小;

2、初始化时为不可见,可用setVisible(true)使其显示出来;

3、默认使用BorderLayout作为其布局管理器;

例:创建一个窗口

import   java.awt.*;
public class youngFrame {
public static void main(String[] args) {
Frame f=new Frame("测试窗口");
//设置窗口大小、位置
f.setBounds(30,30,250,200);
//将窗口显示出来(Frame对象默认处于隐藏状态)
f.setVisible(true);
}

}

2、Panel

Panel容器存在的意义在于为其它组件提供空间。

特点:

1、可作为容器来盛装其他组件,为放置组件提供空间;

2、不能单独存在,必须放到其他容器中;

3、默认使用FlowLayout作为其布局管理器

例:使用Panel作为容器盛装了一个文本框和一个按钮

import java.awt.*;
public class youngPanel {
public static void main(String[] args) {
Frame f=new Frame("测试窗口");
//创建一个Panel对象
Panel p=new Panel();
//向Panel中添加两个组件
p.add(new TextField(20));
p.add(new Button("单击我"));
f.add(p);
//设置窗口大小、位置
f.setBounds(30,30,250,120);
//将窗口显示出来(Frame对象默认处于隐藏状态)
f.setVisible(true);
}

}

3、ScrollPane

ScrollPane是一个带滚动条的容器,它也不能独立存在,必须被添加到其他容器中。

特点:

1、可作为容器来盛装其他组件,当组件占用空间过大时ScrollPane会自动产生滚动条。当然也可以通过指定特定的构造器参数来指定默认具有滚动条。

2、不能单独存在,必须放置到其他容器中。

3、默认使用BorderLayout作为其布局管理器。ScrollPane通常用于盛装其他容器,所以通常不允许改变ScrollPane的布局管理器。

例:使用ScrollPane容器代替了本节前面的Panel容器

import java.awt.*;
public class youngScrollPane {
public static void main(String[] args) {
Frame f=new Frame("测试窗口");
//创建一个ScrollPane容器,指定总是具有滚动条
ScrollPane sp=new ScrollPane(ScrollPane.SCROLLBARS_ALWAYS);
//向ScrollPane容器中添加两个组件
sp.add(new TextField(20));
sp.add(new Button("单击我"));
f.add(sp);
//设置窗口大小、位置
f.setBounds(30,30,250,120);
//将窗口显示出来(Frame对象默认处于隐藏状态)
f.setVisible(true);
}

}

布局管理器

布局利器FlowLayout

默认情况下,AWT的布局管理器是FlowLayout,这个管理器将组件从上到下顺序摆放,它将所有的组件摆放在居中位置。

例:使用FlowLayout布局

import java.awt.*;
import java.awt.event.*;
public class Wintwo1 extends Frame
{
//定义三个按钮组件
Button b1=new Button("提交");
Button b2=new Button("取消");
Button b3=new Button("重置");
Wintwo1()
{
//设置窗口名称
this.setTitle("使用FlowLayout布局");
//设置布局管理器为FlowLayout
this.setLayout(new FlowLayout());
//将按钮组件放入窗口中
this.add(b1);
this.add(b2);
this.add(b3);
//设置窗口大小、位置
this.setBounds(100,100,450,350);
//将窗口显示出来(Frame对象默认处于隐藏状态)
this.setVisible(true);
}
public static void main(String[] args) {
new Wintwo1();
}

}

布局利器BorderLayout

将窗口划分成上、下、左、右、中五个区域

注意以下两点:

1、当向使用BorderLayout布局管理器的容器中添加组件时,需要指定要添加到哪个区域里。如果没有指定,默认添加到中间区域。

2、如果向同一个区域添加多个组件,后放入的组件会覆盖前面的组件。

Frame、Dialog、ScrollPane默认使用BorderLayout布局管理器。

两个构造器:

BorderLayout():默认水平,垂直间距创建

BorderLayout(int hgap,int vgap):使用指定的水平、垂直间距创建。

几个用来指定添加组件到哪个区域的静态常量:EAST、NORTH、WEST、SOUTH、CENTER。

例:使用BorderLayout布局

import java.awt.*;
import java.awt.event.*;
public class Wintwo1 extends Frame
{
//定义5个按钮组件
Button b1=new Button("上边");
Button b2=new Button("下边");
Button b3=new Button("左边");
Button b4=new Button("右边");
Button b5=new Button("中间");
Wintwo1(){
//设置窗口名称
this.setTitle("五个按钮随意摆");
//设置布局管理器为BorderLayout
this.setLayout(new BorderLayout());
//将按钮组件放入窗口规定位置中
this.add(b1,BorderLayout.NORTH);
this.add(b2,BorderLayout.SOUTH);
this.add(b3,BorderLayout.WEST);
this.add(b4,BorderLayout.EAST);
this.add(b5,BorderLayout.CENTER);
//设置窗口大小、位置
this.setBounds(100,100,450,450);
//将窗口显示出来(Frame对象默认处于隐藏状态)
this.setVisible(true);

}
public static void main(String[] args) {
new Wintwo1();

}

}

注意:BorderLayout最多只能放5个组件,要想放多个组件,需要先将部分组件放在Panel中,然后再把Panel添加到BorderLayout中。如果组件小于5个,没有放置组件的地方,将被相邻的组件占用。

布局利器GridLayout

矩形网络,网格的大小是由容器和创建网格的多少来确定。添加组件时,默认从左到右、从上到下。与FlowLayout不同的是,GridLayout中各组件的大小,由组件所处的区域来决定(每个组件将自动涨大到占满整个区域)。

两个构造器:

GridLayout(int rows,int cols):采用指定行、列数,默认间距。

GridLayout(int rows,int cols,int hgap,int vgap):采用指定行、列、横向、纵向间距。

import java.awt.*;
import java.awt.event.*;
public class Wintwo1 extends Frame implements ActionListener{
int i=5;
//定义9个按钮组件
Button b1=new Button("按钮A");
Button b2=new Button("按钮B");
Button b3=new Button("按钮C");
Button b4=new Button("按钮D");
Button b5=new Button("按钮E");
Button b6=new Button("按钮F");
Button b7=new Button("按钮G");
Button b8=new Button("按钮H");
Button b9=new Button("按钮I");
Wintwo1(){
//设置窗口名称
    this.setTitle("布局利器GridLayout");
  //设置布局管理器为3行3列GridLayout
  this.setLayout(new GridLayout(3,3));
  //将按钮组件放入窗口
  this.add(b1);
  this.add(b2);
  this.add(b3);
  this.add(b4);
  this.add(b5);
  this.add(b6);
  this.add(b7);
  this.add(b8);
this.add(b9);
//为每个按钮组件添加监听
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
//设置窗口大小、位置
this.setBounds(100,100,450,450);
//将窗口显示出来(Frame对象默认处于隐藏状态)
this.setVisible(true);
}
//实现ActionListener接口中的actionPerformed方法
public void actionPerformed(ActionEvent e) {
i++;
Button bi=new Button("按钮"+i);
this.add(bi);
bi.addActionListener(this);
this.show(true);
}
public static void main(String[] args) {
new Wintwo1();

}

布局利器CardLayout

CardLayout布局管理器可以设置在一组组件中只显示某一个组件,用户可以根据需要选择需要显示的某一个组件。

两个构造器:

CardLayout():创建默认的CardLayout布局管理器

CardLayout(int hgap,int vgap):通过指定卡片与容器左右边界的间距,上下边界的间距来创建CardLayout布局管理器。

import java.awt.*;
import java.awt.event.*;
public class Wintwo1 extends Frame implements ActionListener{
//定义一个面板
Panel p=new Panel();
//定义5个按钮
Button bf=new Button("第一个");
Button bl=new Button("最后一个");
Button bn=new Button("下一个");
Button bp=new Button("上一个");
Button bg=new Button("搜索一个");
//定义一个单行文本框
TextField tf=new TextField();
//设置CardLayout布局管理器
CardLayout cl=new CardLayout();
Wintwo1(){
//设置窗口名称
this.setTitle("利用CardLayout布局组件");
this.setLayout(null);
this.add(p);
//定义P面板为CardLayout布局管理器
p.setLayout(cl);
//为CardLayout布局管理器添加按钮
for(int i=1;i<=3;i++) {
Button btemp=new Button("布局按钮"+i);
p.add(btemp, ""+i);
}
//为每个组件设置大小位置并添加到容器中
p.setBounds(10,40,100,100);
this.add(bf);
bf.addActionListener(this);
bf.setBounds(120,40,60,20);
this.add(bl);
bl.addActionListener(this);
bl.setBounds(120,70,60,20);
this.add(bn);
bn.addActionListener(this);
bn.setBounds(120,100,60,20);
this.add(bp);
bp.addActionListener(this);
bp.setBounds(120,130,60,20);
this.add(bg);
bg.addActionListener(this);
bg.setBounds(60,160,40,20);
this.add(tf);
tf.setBounds(20,160,40,20);
//设置窗口的位置和大小
this.setBounds(200,200,400,380);
//设置窗口的可见性
this.setVisible(true);
}
//实现ActionListener接口中的actionPerformed方法
public void actionPerformed(java.awt.event.ActionEvent e)
{
//next方法
if(e.getSource()==bn)
cl.next(p);
//previous方法
if(e.getSource()==bp)
cl.previous(p);
//first
if(e.getSource()==bf)
cl.first(p);
//last方法
if(e.getSource()==bl)
cl.last(p);
//show方法
if(e.getSource()==bg)
{
cl.show(p, tf.getText().trim());
tf.setText("");
}
}
public static void main(String[] args) {
new Wintwo1();
}  

}

布局利器Null

import java.awt.*;
import java.awt.event.*;
public class Wintwo1 extends Frame{
//定义3个按钮
Button b1=new Button("甲按钮");
Button b2=new Button("乙按钮");
Button b3=new Button("丙按钮");
Wintwo1(){
//设置窗口名称
this.setTitle("Null布局");
//设置布局管理器
this.setLayout(null);
//将组件添加到容器
this.add(b1);
this.add(b2);
this.add(b3);
//设置组件的大小和位置
b1.setBounds(50,50,100,50);
b2.setBounds(50,120,100,50);
b3.setBounds(50,190,100,50);
//设置窗口的大小和位置 
this.setBounds(100,100,450,400);
//设置窗口的可见性
this.setVisible(true);
     }
public static void main(String[] args) {
new Wintwo1();

}

}

GridBagLayout布局管理器

GridBagLayout是Java中最富有弹性但也是最复杂的一种版面管理器,它只有一种构造函数,但必须配合GridBagConstraints才能达到设置的效果。

因为GridBagLayout里的各种设置都必须通过GridBagConstraints,因此当设置好GridBagConstraints的参数后,必须新建一个GridBagConstraints的对象以便GridBagLayout使用。

BoxLayout布局管理器

Swing引入,BoxLayout保留了GridBagLayout的优点,使用简单。BoxLayout可以在水平和垂直两个方向上摆放GUI组件

Java应用中,BoxLayout通常和Box容器结合使用,Box是一个和Panel容器类似的特殊容器。Box容器默认使用BoxLayout布局管理器。

AWT常用组件

基本组件:

Button:按钮,可单击

Canvas:用于绘图的画布

Checkbox:复选框组件(也可变成单选框组件)

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

Choice:下拉式选择框组件。

Frame:窗口,在GUI程序里通过该类创建窗口。

Label:标签类,用于放置提示性文本。

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

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

Scrollbar:滑动条组件。

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

TextArea:多行文本域

TextField:单行文本框

AWT中的对话框

对话框是Window类中的一个子类,也是一个容器类。对话框是可以独立存在的顶级窗口,其用法和普通窗口用法几乎完全一样。

使用对话框时要注意两点:

1、对话框通常依赖于其他窗口,也就是通常有一个parent窗口。

2、对话框有非模式、模式两种,当某个模式对话框被打开之后,该模式对话框总是位于它依赖的窗口之上;在模式对话框被关闭之前,它依赖的窗口无法获得焦点。

对话框有多个重载的构造器,构造器中可能包含如下3个参数:

owner:指定该对话框所依赖的窗口,既可以是窗口,也可以是对话框。

title:指定该对话框的窗口标题。

modal:指定该对话框是否是模式的,可以是true或false。

类Dialog还有一个名为FileDialog的子类,此类代表了一个文件对话框,用于打开或保存文件。

例:演示模式对话框和非模式对话框的用法

import java.awt.*;
import java.awt.event.*;
public class youngDialog {
Frame f=new Frame("测试");
Dialog d1=new Dialog(f,"模式对话框",true);
Dialog d2=new Dialog(f,"模式对话框",true);
Button b1=new Button("打开模式对话框");
Button b2=new Button("打开非模式对话框");
public void init() {
d1.setBounds(20,30,300,400);
d2.setBounds(20,30,300,400);
b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
d1.setVisible(true);
}
});
b2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
d2.setVisible(true);
}
});
f.add(b1);
f.add(b2,BorderLayout.SOUTH);
f.pack();
f.setVisible(true);
}
public static void main(String[] args) {
new youngDialog().init();
}
}

事件处理

Java的事件处理过程主要涉及3类对象:Event Source、Event、Event Listener。

AWT的事件处理机制是一种委派式事件处理方式,其中普通组件(事件源)负责将整个事件处理委托给特定的对象(事件监听器),当该事件源发生指定的事件时就通知所委托的事件监听器,由事件监听器来处理这个事件。

AWT的事件处理机制基本步骤:

1、实现事件监听器类,该监听器类是一个特殊的Java类,必须实现一个XxxListener接口。

2、创建普通组件(事件源),创建事件监听器对象。

3、调用方法addXxxListener()将事件监听器对象注册给普通组件(事件源)。这样当事件源上发生指定事件时,AWT会触发事件监听器,由事件监听器调用相应的方法(事件处理器)来处理事件,事件源上所发生的事件会作为参数传入事件处理器。

事件和事件监听器

1、事件种类

(1)低级事件:基于特定动作的事件

(2)高级事件:基于语义的事件

2、事件、监听器和处理器

3、监听应用

(1)窗口监听的接口

(2)按钮监听的接口

(3)文本框监听的接口

事件适配器






  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值