日期:2014-05-20 浏览次数:20751 次
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
@SuppressWarnings("serial")
public class GridBagLayoutExample extends JPanel {
private JButton top = new JButton() ;
private JButton left = new JButton() ;
private JButton right1 = new JButton() ;
private JButton right2 = new JButton() ;
public GridBagLayoutExample() {
GridBagLayout gridBag = new GridBagLayout() ;
this.setLayout(gridBag);
GridBagConstraints gridBagC = new GridBagConstraints() ;
gridBagC.fill = GridBagConstraints.HORIZONTAL ;
gridBagC.gridx = 0 ;
gridBagC.gridy = 0 ;
gridBagC.gridwidth = 2 ;
gridBag.setConstraints(top, gridBagC);
this.add(top) ;
gridBagC.gridx = 0 ;
gridBagC.gridy = 1 ;
gridBagC.gridheight = 2 ;
gridBagC.fill = GridBagConstraints.VERTICAL ;
gridBag.setConstraints(left, gridBagC);
this.add(left) ;
gridBagC.gridx = 1 ;
gridBagC.gridy = 1 ;
gridBag.setConstraints(right1, gridBagC);
this.add(right1);
gridBagC.gridx = 1 ;
gridBagC.gridy = 2 ;
gridBag.setConstraints(right2, gridBagC);
this.add(right2);
}
public static void main(String[] args) {
JFrame f = new JFrame("GridBagLayoutExample");
JPanel p = new GridBagLayoutExample();
f.getContentPane().add(p , BorderLayout.CENTER);
f.setSize(230, 230);
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
public GridBagLayoutExample() {
GridBagLayout gridBag = new GridBagLayout() ;
this.setLayout(gridBag);
GridBagConstraints gridBagC = new GridBagConstraints() ;
gridBagC.fill = GridBagConstraints.HORIZONTAL ;
gridBagC.gridx = 0 ;
gridBagC.gridy = 0 ;
gridBagC.gridwidth = 2 ;
gridBag.setConstraints(top, gridBagC);
this.add(top) ;
gridBagC = new GridBagConstraints() ;
gridBagC.gridx = 0 ;
gridBagC.gridy = 1 ;
gridBagC.gridheight = 2 ;
gridBagC.fill = GridBagConstraints.VERTICAL ;
gridBag.setConstraints(left, gridBagC);
this.add(left) ;
gridBagC = new GridBagConstraints() ;
gridBagC.gridx = 1 ;
gridBagC.gridy = 1 ;
gridBag.setConstraints(right1, gridBagC);
this.add(right1);
gridBagC = new GridBagConstraints() ;
gridBagC.gridx = 1 ;
gridBagC.gridy = 2 ;
gridBag.setConstraints(right2, gridBagC);
this.add(right2);
}