GridBagLayout在运行

时间:2016-02-16 21:14:41

标签: java layout jpanel gridbaglayout

我有以下类,Launcher,它是主类 和其他一些,TopPanel,CenterPanel等只是扩展JPanel。

上面有类,有些结果图像,正确的一个和两个假。第一次使用GridBagLayout所以我不知道我做错了什么

   public class Launcher extends JFrame{
        private final Dimension dimension, screen;
        private TopPanel top;
        private CenterPanel center;
        private ProgressPanel progress;
        private BotPanel bot;
        private final GridBagConstraints c;


    public Launcher(){
        dimension = new Dimension(500,400);
        screen = Toolkit.getDefaultToolkit().getScreenSize();
        c = new GridBagConstraints();
        initialize();
        initialize_top();
        initialize_center();
        initialize_progress();
        initialize_bot();
    }

    private void initialize(){
        setUndecorated(true);
        getContentPane().setPreferredSize(dimension);
        setVisible(true);
        setLayout(new GridBagLayout());
        pack();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setWindowShape(this, new RoundRectangle2D.Double(0,0,getWidth(),getHeight(),20,20));
        setLocation(screen.width/2-dimension.width/2, screen.height/2-dimension.width/2);
    }

    private void initialize_top(){
        top = new TopPanel(this);
        top.setBackground(Color.red);
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weighty = 0.1;
        c.ipady = 75;
        c.gridx=0;
        c.gridy=0;
        add(top, c);
    }

    private void initialize_center(){
        center = new CenterPanel(this);
        center.setBackground(Color.yellow);
        c.fill = GridBagConstraints.HORIZONTAL;
        c.ipady = 170;
        c.gridx=0;
        c.gridy=1;
        add(center, c);
    }

    private void initialize_progress(){
        progress = new ProgressPanel(this);
        progress.setBackground(Color.blue);
        c.fill = GridBagConstraints.HORIZONTAL;
        c.ipady = 30;
        c.gridx=0;
        c.gridy=2;
        add(progress, c);
    }

    private void initialize_bot(){
        bot = new BotPanel(this);
        bot.setBackground(Color.green);
        c.fill = GridBagConstraints.HORIZONTAL;
        c.ipady = 85;
        c.gridx=0;
        c.gridy=3;
        add(bot, c);
    }
}

public class TopPanel extends JPanel{
    private JPanel logo, product, settings, minimize, exit;
    private GridBagConstraints c;

    public TopPanel(Launcher aThis) {
        initialize();
    }

    private void initialize(){
        setLayout(new GridBagLayout());
        c = new GridBagConstraints();
        set_logo();
        set_product();
        set_settings();
        set_minimize();
        set_exit();
    }

    private void set_logo(){
        logo = new JPanel();
        logo.setBackground(Color.CYAN);
        c.weighty = 0.5;
        c.ipadx = 138;
        c.gridx=0;
        c.gridheight=2;
        c.gridy=0;
        add(logo, c);
    }

    private void set_product(){
        product = new JPanel();
        product.setBackground(Color.white);
        c.ipadx = 162;
        c.gridx=1;
        c.gridheight=2;
        c.gridy=0;
        add(product, c);
    }

    private void set_settings(){
        settings = new JPanel();
        c.ipadx = 120;
        c.gridx=2;
        c.gridheight=1;
        c.gridy=0;
        add(settings, c);
    }

    private void set_minimize(){
        minimize = new JPanel();
        c.ipadx = 40;
        c.gridx=3;
        c.gridheight=1;
        c.gridy=0;
        add(minimize, c);
    }

    private void set_exit(){

    }

}

我想要的是这样的 correct result

但如果我运行该项目,有时我会得到正确的结果,但有时我会得到类似这样的东西而不会改变代码中的任何内容

enter image description here enter image description here

任何想法?

0 个答案:

没有答案