0

我在布局正确时遇到了麻烦。我有一个扩展 JFrame 的类,并有一个 JPanel,它有一个 GridBagLayout 作为布局管理器。我想要4个应该以这种方式布局的按钮

布局

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

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

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();
    }

它适用于默认的外观主题。

4

1 回答 1

0

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

打招呼

THE-E

于 2013-06-18T19:44:51.967 回答