Java中GridBagLayout的垂直填充不起作用
我很难把布局弄对。 我有一个扩展JFrame的类,还有一个JPanel,它有一个GridBagLayout作为布局管理器。我想要4个按钮,应该以这种方式布局 不知何故,垂直填充不起作用。 我尝试了各种方法,但不知道如何使这项工作 如果这是一个noob问题,很抱歉,但尝试了一个多小时,没有任何更改:/Java中GridBagLayout的垂直填充不起作用,java,swing,jpanel,layout-manager,gridbaglayout,Java,Swing,Jpanel,Layout Manager,Gridbaglayout,我很难把布局弄对。 我有一个扩展JFrame的类,还有一个JPanel,它有一个GridBagLayout作为布局管理器。我想要4个按钮,应该以这种方式布局 不知何故,垂直填充不起作用。 我尝试了各种方法,但不知道如何使这项工作 如果这是一个noob问题,很抱歉,但尝试了一个多小时,没有任何更改:/ public class ButtonPanel extends JFrame { private static final long serialVersionUID = 1L;
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作为外观和感觉
它适用于默认的外观主题。我在第一篇文章中添加了答案。这是一种由外观皮肤引起的虫子/ 问候
E看起来你应该使用GridLayout。我运行了程序,看到了你提到的输出@E,不要在注释中发布代码。用您用来测试程序的代码更新您的原始问题,这样我们就不会花时间猜测您在做什么。
//Set Theme
try {
UIManager.setLookAndFeel("com.seaglasslookandfeel.SeaGlassLookAndFeel");
} catch (Exception e) {
e.printStackTrace();
}