gpt4 book ai didi

java - Swing : GridBagLayout adding components not the expected way

转载 作者:行者123 更新时间:2023-12-01 22:03:08 24 4
gpt4 key购买 nike

我只是尝试使用 this 之后的 GridBagLayout教程。

我无法得到我想要的东西。

我希望每个面板占用尽可能多的空间,但事实并非如此,我真的尝试了一切,按照 Oracle Docs 中的步骤进行操作。

但是结果总是一样的:

GBC

我真的不明白为什么布局不起作用。

我尝试更改 JFrame 的布局,将面板设置为 ContentPane(),尝试添加在两个网站上找到的约束,但似乎没有任何效果。

非常感谢您的帮助。

<小时/>

完整代码

public class Monitor extends JFrame{

// Menu
private JMenuBar bar = new JMenuBar();
private JMenu file = new JMenu("File");
private JMenuItem open = new JMenuItem("Open");
private JMenuItem exit = new JMenuItem("Exit");

private JPanel mainPanel = new JPanel();

private JPanel secondaryPanel;
private JPanel explorerPanel, tagPanel;

public Monitor(){
setTitle("");
setSize(750,600);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setResizable(false);


initComps();
}

public void initComps(){

explorerPanel = new JPanel();
explorerPanel.setBackground(Color.BLACK);
explorerPanel.setPreferredSize(new Dimension(250,300));

tagPanel = new JPanel();
tagPanel.setBackground(Color.yellow);
tagPanel.setPreferredSize(new Dimension(250, 300));

secondaryPanel = new JPanel();
secondaryPanel.setBackground(Color.blue);
secondaryPanel.setPreferredSize(new Dimension(500, 600));

mainPanel = new JPanel();
mainPanel.setPreferredSize(new Dimension(750, 600));
mainPanel.setLayout(new GridBagLayout());

GridBagConstraints gbc = new GridBagConstraints();

gbc.gridx = 0 ; gbc.gridy = 0 ; gbc.gridwidth = 1 ; gbc.gridheight = 1;
mainPanel.add(explorerPanel, gbc);

gbc.gridx = 1 ; gbc.gridy = 0 ; gbc.gridwidth = 2 ; gbc.gridheight = 2;
mainPanel.add(secondaryPanel, gbc);

gbc.gridx = 0 ; gbc.gridy = 1 ; gbc.gridwidth = 1 ; gbc.gridheight = 1;
mainPanel.add(tagPanel, gbc);

this.setContentPane(mainPanel);

// Menu Bar
file.add(open);

exit.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
exit.setMnemonic('q');
file.add(exit);

bar.add(file);
setJMenuBar(bar);
}

public static void main(String[] args) {
new Monitor().setVisible(true);

}
}
<小时/>

编辑:

添加时

gbc.weightx = 1;
gbc.weighty = 1;

结果

GBC

最佳答案

您需要将 GridBagConstraints.weightxGridBagConstraints.weighty 设置为非零值:

来自documentation

GridBagConstraints.weightx, GridBagConstraints.weighty

Used to determine how to distribute space, which is important for specifying resizing behavior. Unless you specify a weight for at least one component in a row (weightx) and column (weighty), all the components clump together in the center of their container. This is because when the weight is zero (the default), the GridBagLayout object puts any extra space between its grid of cells and the edges of the container.

如果您希望所有组件都填满空间,则将其设置为 1 应该可以正常工作。

Oracle tutorial page 上还有这些字段的更多解释。您在weightx,weighty标题下链接。

编辑:

您还需要为组件设置fill,否则默认为NONE:

GridBagConstraints.fill

Used when the component's display area is larger than the component's requested size to determine whether (and how) to resize the component. Possible values are GridBagConstraints.NONE (the default), GridBagConstraints.HORIZONTAL (make the component wide enough to fill its display area horizontally, but don't change its height), GridBagConstraints.VERTICAL (make the component tall enough to fill its display area vertically, but don't change its width), and GridBagConstraints.BOTH (make the component fill its display area entirely).

这是我通过将 explorerPaneltagPanelweightx 设置为 0.33 得到的结果,以及 secondaryPanel 为 0.66,并将所有这些的 fill 设置为 GridBagConstraints.BOTH:

enter image description here

关于java - Swing : GridBagLayout adding components not the expected way,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33353670/

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