// 设定布局
int gridx, gridy, gridwidth, gridheight, anchor, fill, ipadx, ipady;
double weightx, weighty;
GridBagConstraints c;
Insets inset;
GridBagLayout gridbag = new GridBagLayout();
this.setLayout(gridbag);
// 0,0
gridx = 0;
gridy = 0;
gridwidth = 1;
gridheight = 1;
weightx = 1.00;
weighty = 1.00;
anchor = GridBagConstraints.CENTER;
fill = GridBagConstraints.BOTH;
inset = new Insets(up, left, down, right);
ipadx = 0;
ipady = 0;
c = new GridBagConstraints(gridx, gridy, gridwidth, gridheight,
weightx, weighty, anchor, fill, inset, ipadx, ipady);
JScrollPane js=new JScrollPane(msgArea);
gridbag.setConstraints(js, c);
this.add(js);
以上代码中,Insets构造函数四个参数的顺序依次为上,左,下,右, 逆时针方向。这样比较好记忆。
Swing中其它类似的四参数形式(如BorderFactory.createEmptyBorder(top, left, down, right))也类同此例。