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
如何理解,我已经看过帮助,但看不懂,希望各位详细解释。
----------------解决方案--------------------------------------------------------
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
----------------解决方案--------------------------------------------------------