gpt4 book ai didi

java - GridBagLayout 中底部按钮

转载 作者:行者123 更新时间:2023-12-02 07:33:50 25 4
gpt4 key购买 nike

我正在为一些按钮设置布局。我想在中间有 2 个按钮,在最后有一个。我有两个在中间,但最后一个在一边。如何将“后退”按钮设置在其他按钮下方。 (我对此进行了研究)。

public class Options extends JPanel
{
private static final long serialVersionUID = 1L;
JButton b1 = new JButton("Back");
JButton b4 = new JButton("Textures");
JButton b5 = new JButton("Settings");

public Options()
{
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.anchor = GridBagConstraints.CENTER;
c.weighty = 1;
c.gridx = 0;
c.gridy = 0;
c.ipadx = 5;
add(b5, c);
c.ipadx = 1;
c.gridy = 1;
add(b4, c);
c.weighty = 1;
c.gridy = 2;
c.anchor = GridBagConstraints.PAGE_END;
add(b1, c);
}
}

My layout error

编辑:我已经更新了上面的代码。偏移错误已修复,但 b5 位于顶部而不是居中(b4 居中,b1 位于底部)。

最佳答案

这应该与我认为您想要获得的布局足够接近:

    setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();

JPanel layout = new JPanel(new GridLayout(0, 1));
layout.add(new JButton("Settings"));
layout.add(new JButton("Textures"));

c.anchor = GridBagConstraints.CENTER;
c.weighty = 1.0;
c.gridy = 0;
add(layout, c);

c.gridy = 1;
c.weighty = 0.1;
c.anchor = GridBagConstraints.PAGE_END;
add(new JButton("Back"), c);

关于java - GridBagLayout 中底部按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12545531/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com