简体   繁体   English

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

[英]Vertical filling of GridBagLayout in Java does not work

I am having troubles getting the layout right. 我在正确布局时遇到了麻烦。 I have a Class which extends JFrame and has a JPanel which has a the GridBagLayout as layout manager. 我有一个扩展JFrame的类和一个具有GridBagLayout作为布局管理器的JPanel。 I want 4 buttons which should be layout in this way 我想要4个按钮应该以这种方式进行布局

布局

Somehow the vertical filling does not work. 垂直填充不知何故不起作用。 I tried various ways but can't figure out how to make this work. 我尝试了各种方法,但不知道如何进行这项工作。

Sorry if this is a noob question, but tried it for more than an hour without any change :/ 抱歉,这是一个菜鸟问题,但尝试了一个多小时,没有任何更改:/

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


}

greets 打招呼

THE-E -

EDIT: Found the bug 编辑:发现错误

I used Seaglass as Look and Feel in the main-method 我在主方法中将Seaglass用作外观

  //Set Theme try { UIManager.setLookAndFeel("com.seaglasslookandfeel.SeaGlassLookAndFeel"); } catch (Exception e) { e.printStackTrace(); } 

It works fine with the default Look and Feel Theme. 它可以与默认的外观主题配合使用。

I have added the answer in the first post. 我已经在第一篇文章中添加了答案。 It was a bug caused by the LookAndFeel skin. 这是由LookAndFeel皮肤引起的错误。 :/ :/

greets 打招呼

THE-E -

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM