import javaawtDimension;import javaawtGridBagConstraints;import javaawtGridBagLayout;import javaawtGridLayout;import javaawtInsets;import javaxswing*;public class Layout {
/**
* @param args
*/
public static void main(String[] args) {
new LayoutFrame()setVisible(true)
}}class LayoutFrame extends JFrame {
LayoutFrame(){
thissetTitle(Layout Test)
thissetSize()
thissetDefaultCloseOperation(JFrameEXIT_ON_CLOSE)
JPanel jp = new JPanel()
jpsetLayout(new GridBagLayout())
jpadd(new JScrollPane(new JTextArea())new Constraint())
JPanel p = new JPanel()
psetLayout(new GridLayout())
psetBorder(BorderFactorycreateEtchedBorder())
padd(new JLabel(标 签))
padd(new JTextField())
jpadd(pnew Constraint())
jpadd(new MPanel()new Constraint())
jpadd(new MPanel()new Constraint())
JPanel p = new JPanel()
psetLayout(new GridLayout())
psetBorder(BorderFactorycreateEtchedBorder())
padd(new JButton(按钮))
padd(new JButton(按钮))
jpadd(pnew Constraint())
jpadd(new MPanel()new Constraint())
jpadd(new MPanel()new Constraint())
jpadd(new JScrollPane(new JTextArea())new Constraint())
MPanel mp = new MPanel()
/*mpadd(new JButton(hellp))
mpadd(new JButton(hellp))
mpadd(new JButton(hellp))
mpadd(new JButton(hellp))*/
jpadd(mpnew Constraint())
jpadd(new MPanel()new Constraint())
jpadd(new MPanel()new Constraint())
add(jp)
}}class MPanel extends JPanel {
MPanel(){
/*Dimension d = new Dimension()
//thissetPreferredSize(new Dimension())
thissetMaximumSize(d)
thissetMinimumSize(d)*/
add(new JLabel(面板))
thissetBorder(BorderFactorycreateEtchedBorder())
}}class Constraint extends GridBagConstraints {
Constraint(int gridxint gridy) {
thisgridx = gridx; //gridxgridy 指表格坐标指此物件的开始位置在(xy)处也即在 x列y行处 xy 都从开始
thisgridy = gridy;
thissetFill(ConstraintBOTH) //默认物作是居中的设成both后如果物件比所占area小就会在横竖两个方向拉伸
//thissetWeight( )//空白区域怎么分配
thissetInsets()//边界
}
Constraint(int gridxint gridyint gridheightint gridwidth) {
this(gridxgridy)
//高和宽指物件占用的空间 四个参数可理解 了 xydydx
thisgridheight = gridheight;
thisgridwidth = gridwidth;
}
public Constraint setFill(int fill) {
thisfill = fill;
return this;
}
public Constraint setAnchor(int anchor) {
thisanchor = anchor; //是指物件放在所在区域的什么位置默认是center CENTER NORTH NORTHEAST EAST SOUTHEAST SOUTH SOUTHWEST WEST
//and NORTHWEST The relative values are: PAGE_START PAGE_END LINE_START LINE_END FIRST_LINE_START FIRST_LINE_END LAST_LINE_START and LAST_LINE_END The default value is CENTER
return this;
}
public Constraint setWeight(double weightxdouble weighty) {
thisweightx = weightx;
thisweighty = weighty;
return this;
}
public Constraint setInsets(int distance){
thisinsets = new Insets(distancedistancedistancedistance)
return this;
}}