// 設定布局
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))也類同此例。