Swing:GridBagLayout 添加组件不是预期的方式
Swing : GridBagLayout adding components not the expected way
我只是想按照 this 教程使用 GridBagLayout。
我得不到我想要的。
我希望每个 Panel 尽可能多地占用 space 但事实并非如此,我真的尝试了所有方法,按照 Oracle Docs
中的步骤
但结果总是一样的:
我真的不明白为什么布局不起作用。
我尝试更改 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;
结果
您可以使用 BorderLayout
解决这个问题
而不是这个:
this.setContentPane(mainPanel);
你可以这样做:
getContentPane().add(mainPanel, BorderLayout.CENTER);
您需要将 GridBagConstraints.weightx
和 GridBagConstraints.weighty
设置为 non-zero:
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.
如果您希望所有组件都填满 space,将所有组件设置为 1 应该可以正常工作。space。
您在 weightx, weighty.
标题下链接的 Oracle tutorial page 上也有对这些字段的更多解释
编辑:
你还需要为组件设置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).
这是我通过将 weightx
设置为 0.33
(对于 explorerPanel
和 tagPanel
,以及将 0.66
设置为 secondaryPanel
,以及将所有的 fill
设置为 GridBagConstraints.BOTH
:
你的问题出在你的维度上。
不要只使用 "preferred"。也使用最小值。
explorerPanel = new JPanel();
explorerPanel.setBackground(Color.BLACK);
explorerPanel.setMinimumSize(new Dimension(250, 300));
explorerPanel.setPreferredSize(new Dimension(250, 300));
tagPanel = new JPanel();
tagPanel.setBackground(Color.yellow);
tagPanel.setMinimumSize(new Dimension(250, 300));
tagPanel.setPreferredSize(new Dimension(250, 300));
secondaryPanel = new JPanel();
secondaryPanel.setBackground(Color.blue);
secondaryPanel.setMinimumSize(new Dimension(500, 600));
secondaryPanel.setPreferredSize(new Dimension(500, 600));
mainPanel = new JPanel();
mainPanel.setPreferredSize(new Dimension(750, 600));
mainPanel.setMinimumSize(new Dimension(750, 600));
mainPanel.setLayout(new GridBagLayout());
这在我的测试中有效:
我只是想按照 this 教程使用 GridBagLayout。
我得不到我想要的。
我希望每个 Panel 尽可能多地占用 space 但事实并非如此,我真的尝试了所有方法,按照 Oracle Docs
中的步骤但结果总是一样的:
我真的不明白为什么布局不起作用。
我尝试更改 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;
结果
您可以使用 BorderLayout
解决这个问题而不是这个:
this.setContentPane(mainPanel);
你可以这样做:
getContentPane().add(mainPanel, BorderLayout.CENTER);
您需要将 GridBagConstraints.weightx
和 GridBagConstraints.weighty
设置为 non-zero:
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.
如果您希望所有组件都填满 space,将所有组件设置为 1 应该可以正常工作。space。
您在 weightx, weighty.
标题下链接的 Oracle tutorial page 上也有对这些字段的更多解释编辑:
你还需要为组件设置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).
这是我通过将 weightx
设置为 0.33
(对于 explorerPanel
和 tagPanel
,以及将 0.66
设置为 secondaryPanel
,以及将所有的 fill
设置为 GridBagConstraints.BOTH
:
你的问题出在你的维度上。
不要只使用 "preferred"。也使用最小值。
explorerPanel = new JPanel();
explorerPanel.setBackground(Color.BLACK);
explorerPanel.setMinimumSize(new Dimension(250, 300));
explorerPanel.setPreferredSize(new Dimension(250, 300));
tagPanel = new JPanel();
tagPanel.setBackground(Color.yellow);
tagPanel.setMinimumSize(new Dimension(250, 300));
tagPanel.setPreferredSize(new Dimension(250, 300));
secondaryPanel = new JPanel();
secondaryPanel.setBackground(Color.blue);
secondaryPanel.setMinimumSize(new Dimension(500, 600));
secondaryPanel.setPreferredSize(new Dimension(500, 600));
mainPanel = new JPanel();
mainPanel.setPreferredSize(new Dimension(750, 600));
mainPanel.setMinimumSize(new Dimension(750, 600));
mainPanel.setLayout(new GridBagLayout());
这在我的测试中有效: