4 课后答案【khdaw_lxywyl】(13)

时间:2025-03-10

图10-6 卡片布局(Card Layout)

单击按钮Button1后,显示下一个按钮Button2,依此类推。 5.网格包布局

网格包 (GridBag)布局是基于网格布局之上的一种改进。和基本的网格布局不同的是,一个组件可以跨越一个或多个网格,这样一来增加了布局的灵活性。为了处 理网格的跨越性,我们可以使用GridBagConstraints类。有兴趣的读者可以参考Java API来了解它。

package sample; import java.awt.*; import java.util.*;

import java.awt.event.*;

public class MyGridBagLayout extends Panel { protected void makebutton(String name, GridBagLayout gridbag, GridBagConstraints c) { Button button = new Button(name); gridbag.setConstraints(button, c); add(button); }

public void go() {

GridBagLayout gridbag = new GridBagLayout(); GridBagConstraints c = new GridBagConstraints(); setFont(new Font("Helvetica", Font.PLAIN, 14)); setLayout(gridbag);

c.fill = GridBagConstraints.BOTH; c.weightx = 1.0;

makebutton("Button001", gridbag, c);

www.

12

khd

aw.

com

CardLayout cl = (CardLayout)f1.getLayout(); cl.next(f1); } } );

f.add(b, "button" + i); }

f.setVisible(true); } }

程序运行结果见图10-6。

makebutton("Button2", gridbag, c); makebutton("Button3", gridbag, c);

c.gridwidth = GridBagConstraints.REMAINDER; //end row makebutton("Button4", gridbag, c);

c.weightx = 0.0; //reset to the default

makebutton("Button5", gridbag, c); //another row

c.gridwidth = 2; //GridBagConstraints.RELATIVE; //next-to-last in row makebutton("Button6", gridbag, c);

c.gridwidth = GridBagConstraints.REMAINDER; //end row makebutton("Button007", gridbag, c);

c.gridwidth = 1; //reset to the default c.gridheight = 2; c.weighty = 1.0;

o makebutton("Button8", gridbag, c);

c.weighty = 1.0; //reset to the default

c.gridwidth = GridBagConstraints.REMAINDER; //end row c c.gridheight = 1; //reset to the default makebutton("Button9", gridbag, c); 网

makebutton("Button10", gridbag, c); setSize(300, 100); 案

.

}

w public static void main(String args[]) {

final Frame f = new Frame("GridBagLayout 后

演示"); f.addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent evt) { 课

a f.setVisible(false); d

f.dispose(); System.exit(0); } h k});

.

MyGridBagLayout gb = new MyGridBagLayout(); gb.go();

f.add("Center", gb); w f.pack();

f.setVisible(true); } }

程序运行结果见图10-7。

13

m

ww

…… 此处隐藏:66字,全部文档内容请下载后查看。喜欢就下载吧 ……
4 课后答案【khdaw_lxywyl】(13).doc 将本文的Word文档下载到电脑

精彩图片

热门精选

大家正在看

× 游客快捷下载通道(下载后可以自由复制和排版)

限时特价:7 元/份 原价:20元

支付方式:

开通VIP包月会员 特价:29元/月

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:fanwen365 QQ:370150219