博学谷——让IT教学更简单,让IT学习更有效
3)创建六个按钮以不同的占位分布
4)编写Example09类,创建Layout类的实例
二、案例实现
编写Example09类,代码如下所示:
import java.awt.*;
class Layout extends Frame {
public Layout(String title) { }
// 增加组件的方法
private void addComponent(String name, GridBagLayout layout,
GridBagConstraints c) {
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(\this.addComponent(\this.addComponent(\
c.gridwidth = GridBagConstraints.REMAINDER; // 添加的组件是本行最后一个组件 this.addComponent(\
c.weightx = 0; // 设置横向权重为0 c.weighty = 0; // 设置纵向权重为0 addComponent(\
c.gridwidth = 1; // 设置组件跨一个网格(默认值) this.addComponent(\
c.gridwidth = GridBagConstraints.REMAINDER; // 添加的组件是本行最后一个组件 this.addComponent(\c.gridheight = 2; // 设置组件纵向跨两个网格 c.gridwidth = 1; // 设置组件横向跨一个网格 c.weightx = 2; // 设置横向权重为2 c.weighty = 2; // 设置纵向权重为2 this.addComponent(\c.gridwidth = GridBagConstraints.REMAINDER; c.gridheight = 1;
this.addComponent(\this.addComponent(\this.pack();
this.setVisible(true);
Button bt = new Button(name); // 创建一个名为name的按钮
16
博学谷——让IT教学更简单,让IT学习更有效
}
public class Example09 { }
public static void main(String[] args) { }
new Layout(\ }
layout.setConstraints(bt, c); // 设置GridBagConstraints对象和按钮的关联 this.add(bt); // 增加按钮
运行结果如图9-13所示。
图9-13 运行结果
从运行结果可以看到,程序正常执行。
三、案例总结
使用GridBagLayout布局管理器的步骤如下:
1、创建GridbagLayout布局管理器,并使容器采用该布局管理器。
GridBagLayout layout = new GridBagLayout(); container.setLayout(layout);
2、创建GridBagContraints对象(布局约束条件),并设置该对象的相关属性。
GridBagConstraints constraints = new GridBagConstraints(); constraints.gridx = 1; // 设置网格的左上角横向索引 constraints.gridy = 1; // 设置网格的左上角纵向索引 constraints.gridwidth = 1; // 设置组件横向跨越的网格 constraints.gridheight = 1; // 设置组件纵向跨越的网格
3、调用GridBagLayout对象的setConstraints()方法建立GridBagConstraints对象和受控组件之间的关联。
layout.setConstraints(component,constraints);
4、向容器中添加组件
container.add(conponent);
GridBagConstraints对象可以重复使用,只需要改变它的属性即可。如果要向容器中添加多个组件,则重复2、3、4步骤。
17
博学谷——让IT教学更简单,让IT学习更有效
案例9-10 CardLayout布局管理器 一、案例描述
1、 考核知识点
编号:00109012
名称:CardLayout布局管理器
2、 练习目标
? 掌握CardLayout布局管理器的使用
3、 需求分析
在操作程序时,我们经常会通过选项卡按钮来切换程序中的界面,这些界面就相当于一张张卡片,而管理这些卡片的布局管理器就是卡片布局管理器(CardLayout)。为了让初学者熟悉CardLayout布局管理器,本案例将演示CardLayout中常用方法的使用。
4、 设计思路(实现原理)
1)定义类Cardlayout继承Frame类,实现ActionListener接口
2)在该类中定义Panel面板放置卡片,再定义一个Panel面板放置按钮
3)定义卡片布局对象,在卡片面板中添加2个文本标签分别为“传智播客”和“黑马程序员” 4)定义一个方法实现按钮的监听触发,并对触发事件做出相应的处理 5)编写类Example10,创建Cardlayout实例
二、案例实现
定义Example10类,代码如下所示:
import java.awt.*; import javax.swing.*; import java.awt.event.*;
//定义Cardlayout继承Frame类,实现ActionListener接口
class Cardlayout extends Frame implements ActionListener {
Panel cardPanel = new Panel(); // 定义Panel面板放置卡片 Panel controlpaPanel = new Panel(); // 定义Panel面板放置按钮 CardLayout cardLayout = new CardLayout();// 定义卡片布局对象
public Cardlayout() { // 定义构造方法,设置卡片布局管理器的属性
setSize(300, 200); setVisible(true); // 为窗口添加关闭事件监听器
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) { }
Cardlayout.this.dispose();
Button nextbutton, preButton;
});
cardPanel.setLayout(cardLayout); // 设置cardPanel面板对象为卡片布局
18
博学谷——让IT教学更简单,让IT学习更有效
}
public class Example10 { }
public static void main(String[] args) { }
Cardlayout cardlayout = new Cardlayout(); }
// 下面的代码实现了按钮的监听触发,并对触发事件做出相应的处理 public void actionPerformed(ActionEvent e) { }
// 如果用户单击nextbutton,执行的语句 if (e.getSource() == nextbutton) {
// 切换cardPanel面板中当前组件之后的一个组件,若当前组件为最后一个组件,则显示第一个组件。 }
if (e.getSource() == preButton) {
// 切换cardPanel面板中当前组件之前的一个组件,若当前组件为第一个组件,则显示最后一个组件。 }
cardLayout.previous(cardPanel); cardLayout.next(cardPanel); // 在cardPanel面板对象中添加2个文本标签
cardPanel.add(new Label(\传智播客\cardPanel.add(new Label(\黑马程序员\// 创建两个按钮对象
nextbutton = new Button(\下一张卡片\preButton = new Button(\上一张卡片\// 为按钮对象注册监听器
nextbutton.addActionListener(this); preButton.addActionListener(this); // 将按钮添加到controlpaPanel中 controlpaPanel.add(preButton); controlpaPanel.add(nextbutton);
// 将cardPanel面板放置在窗口边界布局的中间,窗口默认为边界布局 this.add(cardPanel, BorderLayout.CENTER); // 将controlpaPanel面板放置在窗口边界布局的南区, this.add(controlpaPanel, BorderLayout.SOUTH);
运行结果如图9-14所示。
19
博学谷——让IT教学更简单,让IT学习更有效
图9-14 运行结果
三、案例总结
卡片布局管理器将界面看做一系列卡片,在任何时候只有其中一张卡片是可见的,这张卡片占据容器的整个区域。在CardLayout布局管理中经常会用到下面几个方法,如下表所示。 方法声明 void first(Container parent) void last(Container parent) void previous(Container parent) void next(Container parent) void show(Container parent, String name)
功能描述 显示parent容器的第一张卡片 显示parent容器的最后一张卡片 显示parent容器的前一张卡片 显示parent容器的下一张卡片 显示parent容器中名称为name的组件,如果不存在,则不会发生任何操作。 案例9-11 AWT绘图 一、案例描述
1、 考核知识点
编号:00109013 名称:Graphics类
2、 练习目标
? 掌握使用Graphics类中的方法在组件上绘制图形
3、 需求分析
很多GUI程序都需要在组件上绘制图形,在java.awt包中专门提供了一个Graphics类,它相当于一个抽象的画笔,其中提供了各种绘制图形的方法。为了让初学者掌握在组件上绘制图形,本案例将演示如何使用Graphics类中的方法。
4、 设计思路(实现原理)
1)编写类Example11,创建Frame对象。
2)设计类MyPanel继承类Panel,用于画验证码。 3)设置验证码的背景颜色、字体颜色以及画干扰点。
4)从“0123456789abcdefghijkmnopqrstuvwxyzABCDEFG”中每次产生四个随机数
20