| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 516 人关注过本帖
标题:[求助]网格块布局迷惑
只看楼主 加入收藏
魔鬼之子
Rank: 1
来 自:地狱之都
等 级:新手上路
帖 子:100
专家分:0
注 册:2007-9-22
结帖率:71.43%
收藏
 问题点数:0 回复次数:1 
[求助]网格块布局迷惑

import java.awt.*;
import javax.swing.*;
public class text extends JFrame{
public static void main(String[] args){
new text();
}
public text(){
super("邮件窗口");
Label receiveLabel=new Label("收件人");
Label ccLabel=new Label("抄送");
Label subjectLabel=new Label("主题");
Label accessoryLabel=new Label("附件");

TextField receiveField=new TextField();
TextField ccField=new TextField();
TextField subjectField=new TextField();

TextArea accessoryArea=new TextArea(1,40);
TextArea mailArea=new TextArea(8,40);
this.setLayout(new java.awt.GridBagLayout());
GridBagConstraints gridBag=new GridBagConstraints();
gridBag.fill=GridBagConstraints.HORIZONTAL;
gridBag.weightx=0;
gridBag.weighty=0;


receiveLabel.setFont(new Font("Alias",Font.BOLD,16));
this.add(receiveLabel,gridBag,0,0,1,1);
ccLabel.setFont(new Font("Alias",Font.BOLD,16));
this.add(ccLabel,gridBag,0,1,1,1);
subjectLabel.setFont(new Font("Alias",Font.BOLD,16));
this.add(subjectLabel,gridBag,0,2,1,1);
accessoryLabel.setFont(new Font("Alias",Font.BOLD,16));
this.add(accessoryLabel,gridBag,0,3,1,1);

gridBag.weightx=100;
gridBag.weighty=0;

this.add(receiveField,gridBag,1,0,1,1);
this.add(ccField,gridBag,1,1,2,1);
this.add(subjectField,gridBag,1,2,2,1);
accessoryArea.setEditable(false);
this.add(accessoryArea,gridBag,1,3,2,1);

gridBag.fill=GridBagConstraints.BOTH;
gridBag.weightx=100;
gridBag.weighty=100;
this.add(mailArea,gridBag,0,4,3,1);

this.setLocation(120,150);
this.setSize(300,300);
this.setDefaultCloseOperation(3);
this.setVisible(true);
}
private void add(Component c,GridBagConstraints gbc,int x,int y,int w,int h){
gbc.gridx=x;
gbc.gridy=y;
gbc.gridheight=h;
gbc.gridwidth=w;
add(c,gbc);
}
}
在这个程序中:
gridx,gridy,gridheight,gridwidth,weigthx,weigthy
如何理解,我已经看过帮助,但看不懂,希望各位详细解释。

搜索更多相关主题的帖子: 网格 
2007-10-27 16:26
hjlwlyhjl
Rank: 1
等 级:新手上路
帖 子:71
专家分:0
注 册:2006-12-6
收藏
得分:0 

package hello;

import java.awt.*;

import javax.swing.*;

public class Test extends JFrame
{
public static void main(String[] args)
{
new Test();
}

public Test()
{
super("邮件窗口");
Label receiveLabel = new Label("收件人");
Label ccLabel = new Label("抄送");
Label subjectLabel = new Label("主题");
Label accessoryLabel = new Label("附件");

TextField receiveField = new TextField();
TextField ccField = new TextField();
TextField subjectField = new TextField();

TextArea accessoryArea = new TextArea(1, 40);
TextArea mailArea = new TextArea(8, 40);
// this.setLayout(new java.awt.GridBagLayout());
Container pane = this.getContentPane();//应先调用窗口的getContentPane()得到其子容器pane
pane.setLayout(new java.awt.GridBagLayout());//然后对pane进行布局管理,这一点与awt中的Frame 不同
GridBagConstraints gridBag = new GridBagConstraints();
gridBag.fill = GridBagConstraints.HORIZONTAL;
gridBag.weightx = 0;
gridBag.weighty = 0;

receiveLabel.setFont(new Font("Alias", Font.BOLD, 16));
this.add(pane, receiveLabel, gridBag, 0, 0, 1, 1);
ccLabel.setFont(new Font("Alias", Font.BOLD, 16));
this.add(pane, ccLabel, gridBag, 0, 1, 1, 1);
subjectLabel.setFont(new Font("Alias", Font.BOLD, 16));
this.add(pane, subjectLabel, gridBag, 0, 2, 1, 1);
accessoryLabel.setFont(new Font("Alias", Font.BOLD, 16));
this.add(pane, accessoryLabel, gridBag, 0, 3, 1, 1);

gridBag.weightx = 100;
gridBag.weighty = 0;

this.add(pane, receiveField, gridBag, 1, 0, 1, 1);
this.add(pane, ccField, gridBag, 1, 1, 2, 1);
this.add(pane, subjectField, gridBag, 1, 2, 2, 1);
accessoryArea.setEditable(false);
this.add(pane, accessoryArea, gridBag, 1, 3, 2, 1);

gridBag.fill = GridBagConstraints.BOTH;
gridBag.weightx = 100;
gridBag.weighty = 100;
this.add(pane, mailArea, gridBag, 0, 4, 3, 1);

// this.setLocation(120,150);
this.setSize(300, 300);
this.setDefaultCloseOperation(3);
this.setVisible(true);
}

private void add(Container pane,Component c,GridBagConstraints gbc,int x,
int y,int w,int h)
{
gbc.gridx = x;
gbc.gridy = y;
gbc.gridheight = h;
gbc.gridwidth = w;
pane.add(c, gbc);//添加组件也要在pane中添加
}
}

现在解释上面提到的六个成员变量:
1) gridx,gridy:这两个成员变量用来指定组件在窗口中的位置。比如说:gridx = 0;gridy = 0 表示将要添加的组件在网格中的(0,0)位置。
2)gridwidth,gridheight: 这两个成员变量用来指定组件所占的空间,即网格数。如gridwidth = 2;gridheight = 2 表示将要添加的组件将占有宽为2个单元网格,高也为2个单元网格。
3)weigthx,weigthy: 这两个成员变量用来确定当窗口的显示区比组件优选或最小宽度(高度)更宽或更窄时如何缩放栅格中的列(行)。其缺省值为0.0


2007-10-27 20:48
快速回复:[求助]网格块布局迷惑
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016782 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved