在Java中垂直填充GridBagLayout不起作用

时间:2013-06-13 18:12:37

标签: java swing jpanel layout-manager gridbaglayout

我遇到麻烦让布局正确。 我有一个扩展JFrame的类,并且有一个JPanel,它有一个GridBagLayout作为布局管理器。我想要4个按钮,这应该是这样的布局

Layout

不知何故垂直填充不起作用。 我尝试了各种方法,但无法弄清楚如何使这项工作。

很抱歉,如果这是一个菜鸟问题,但是尝试了一个多小时没有任何改变:/

public class ButtonPanel extends JFrame {

    private static final long serialVersionUID = 1L;

    public ButtonPanel() {
        this.setSize(800, 600);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setVisible(true);
        frame();
    }

    JButton objectsBtn = new JButton("Objekte");    //TODO Strings einfügen
    JButton tenantBtn = new JButton("Mieter");              //TODO Strings einfügen
    JButton ownerBtn = new JButton("Eigentümer");               //TODO Strings einfügen
    JButton optionsBtn = new JButton("Einstellungen");          //TODO Strings einfügen

    JPanel panel = new JPanel();

    public void frame(){

        panel.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        c.insets = new Insets(5, 5, 5, 5);
        c.fill = GridBagConstraints.BOTH;
        c.gridwidth = GridBagConstraints.RELATIVE;
        c.gridheight = GridBagConstraints.RELATIVE;

        //Elemente
        c.weightx = 1.0;
        c.weighty = 1.0;
        c.gridx = 0;
        c.gridy = 0;
        panel.add(objectsBtn, c);

        c.weightx = 1.0;
        c.weighty = 1.0;
        c.gridx = 1;
        c.gridy = 0;
        panel.add(optionsBtn, c);

        c.weightx = 1.0;
        c.weighty = 1.0;
        c.gridx = 0;
        c.gridy = 1;
        panel.add(ownerBtn, c);

        c.weightx = 1.0;
        c.weighty = 1.0;
        c.gridx = 1;
        c.gridy = 1;
        panel.add(tenantBtn, c);


        this.add(panel);

        objectsBtn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                new Object();
            }
        });

        ownerBtn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                //TODO Eigentümer erstellen
            }
        });
    }


}

招呼

THE-E

编辑:发现错误

我在主法

中使用了Seaglass作为外观
  //Set Theme
    try {
        UIManager.setLookAndFeel("com.seaglasslookandfeel.SeaGlassLookAndFeel");
    } catch (Exception e) {
        e.printStackTrace();
    }

使用默认的外观和感觉主题时效果很好。

1 个答案:

答案 0 :(得分:0)

我在第一篇文章中添加了答案。这是由LookAndFeel皮肤引起的错误。 :/

招呼

THE-E