阅读背景:

2019.11.06

来源:互联网 

1、用盒式布局分别实现以下效果:

代码:

package MyAdvancedLayout;
import javax.swing.*;
public class MyBox1 {
    JFrame f;
    JPanel p;
    Box box1,box2,box3,box4;
    JButton b11,b21,b31,b12,b22,b32,b13,b23,b33;
    public MyBox1(){
        f=new JFrame("盒子归一");
        p=new JPanel();
        b11=new JButton("按钮1");
        b21=new JButton("按钮2");
        b31=new JButton("按钮3");
        b12=new JButton("按钮1");
        b22=new JButton("按钮2");
        b32=new JButton("按钮3");
        b13=new JButton("按钮1");
        b23=new JButton("按钮2");
        b33=new JButton("按钮3");
        //第一个垂直盒子
        box1=Box.createVerticalBox();
        box1.add(b11);
        box1.add(b21);
        box1.add(b31);
        //第二个垂直盒子
        box2=Box.createVerticalBox();
        box2.add(b12);
        box2.add(Box.createVerticalStrut(10));
        box2.add(b22);
        box2.add(Box.createVerticalStrut(50));
        box2.add(b32);
        //第三个垂直盒子
        box3=Box.createVerticalBox();
        box3.add(b13);
        box3.add(Box.createVerticalGlue());
        box3.add(b23);
        box3.add(Box.createVerticalGlue());
        box3.add(b33);
        //水平盒子
        box4=Box.createHorizontalBox();
        box4.add(box1);
        box4.add(Box.createHorizontalStrut(50));
        box4.add(box2);
        box4.add(Box.createHorizontalStrut(50));
        box4.add(box3);
        
        p.add(box4);
        f.add(p);
        f.setVisible(true);
        f.setBounds(200,200,400,200);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public static void main(String[]args){
        new MyBox1();
    }
}package MyAdvancedLayo



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: