Java Swing:GridBagLayout以非预期方式添加组件
我只是在下面的教程中尝试使用GridBagLayout 我不能得到我想要的 我希望每个面板占用尽可能多的空间,但事实并非如此,我真的尝试了一切,按照 但结果总是一样的: 我真的不明白为什么布局不起作用 我尝试更改JFrame的布局,将面板设置为ContentPane(),尝试添加在两个网站上找到的约束,但似乎没有任何效果 谢谢你的帮助Java Swing:GridBagLayout以非预期方式添加组件,java,swing,constraints,layout-manager,gridbaglayout,Java,Swing,Constraints,Layout Manager,Gridbaglayout,我只是在下面的教程中尝试使用GridBagLayout 我不能得到我想要的 我希望每个面板占用尽可能多的空间,但事实并非如此,我真的尝试了一切,按照 但结果总是一样的: 我真的不明白为什么布局不起作用 我尝试更改JFrame的布局,将面板设置为ContentPane(),尝试添加在两个网站上找到的约束,但似乎没有任何效果 谢谢你的帮助 完整代码 public class Monitor extends JFrame{ // Menu private JMenuBar bar
完整代码
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);
您可以通过使用BorderLayout来修复此问题 与此相反:
this.setContentPane(mainPanel);
您可以这样做:
getContentPane().add(mainPanel, BorderLayout.CENTER);
您可以通过使用BorderLayout来修复此问题 与此相反:
this.setContentPane(mainPanel);
您可以这样做:
getContentPane().add(mainPanel, BorderLayout.CENTER);
您可以通过使用BorderLayout来修复此问题 与此相反:
this.setContentPane(mainPanel);
您可以这样做:
getContentPane().add(mainPanel, BorderLayout.CENTER);
您需要将
GridBagConstraints.weightx
和GridBagConstraints.weighty
设置为非零:
从
GridBagConstraints.weightx,GridBagConstraints.weighty
用于确定如何分配空间,这对于指定大小调整行为很重要。除非为行(weightx)和列(weighty)中的至少一个组件指定权重,否则所有组件都会在其容器的中心聚集在一起。这是因为当权重为零(默认值)时,GridBagLayout对象会在其单元格网格和容器边缘之间放置任何额外的空间
如果希望所有组件都填满空间,则将其设置为1可以正常工作
在标题weightx,weighty下链接的页面上,还有关于这些字段的更多说明
编辑:
您还需要为组件设置fill
,否则默认为NONE
:
GridBagConstraints.fill
当组件的显示区域大于组件请求的大小时使用,以确定是否(以及如何)调整组件的大小。可能的值有GridBagConstraints.NONE(默认值)、GridBagConstraints.HORIZONTAL(使组件足够宽以水平填充其显示区域,但不更改其高度)、GridBagConstraints.VERTICAL(使组件足够高以垂直填充其显示区域,但不更改其宽度)和GridBagConstraints.两者(使零部件完全填充其显示区域)
以下是我通过为explorerPanel
和tagPanel
设置weightx
到0.33
以及为secondaryPanel
设置0.66
,并将fill
设置到GridBagConstraints得到的结果。这两种约束都是:
您需要将GridBagConstraints.weightx
和GridBagConstraints.weighty
设置为非零:
从
GridBagConstraints.weightx,GridBagConstraints.weighty
用于确定如何分配空间,这对于指定大小调整行为很重要。除非为行(weightx)和列(weighty)中的至少一个组件指定权重,否则所有组件在其容器的中心聚集在一起。这是因为当权重为零时(默认值),GridBagLayout对象在其单元格网格和容器边缘之间放置任何额外空间
如果希望所有组件都填满空间,则将其设置为1可以正常工作
在标题weightx,weighty下链接的页面上,还有关于这些字段的更多说明
编辑:
您还需要为组件设置fill
,否则默认为NONE
:
GridBagConstraints.fill
当组件的显示区域大于组件请求的大小时使用,以确定是否(以及如何)调整组件的大小。可能的值为GridBagConstraints.NONE(默认值)、GridBagConstraints.HORIZONTAL(使组件足够宽以水平填充其显示区域,但不更改其高度)、GridBagConstraints.VERTICAL(使组件足够高以垂直填充其显示区域,但不更改其宽度)和GridBagConstraints.all(使组件完全填充其显示区域)
以下是我通过为explorerPanel
和tagPanel
设置weightx
到0.33
以及为secondaryPanel
设置0.66
,并将fill
设置到GridBagConstraints得到的结果。这两种约束都是:
您需要将GridBagConstraints.weightx
和GridBagConstraints.weighty
设置为非零:
从
GridBagConstraints.weightx,GridBagConstraints.weighty
用于确定如何分配空间,这对于指定大小调整行为很重要。除非为行(weightx)和列(weighty)中的至少一个组件指定权重,否则所有组件在其容器的中心聚集在一起。这是因为当权重为零时(默认值),GridBagLayout对象在其单元格网格和容器边缘之间放置任何额外空间
如果希望所有组件都填满空间,则将其设置为1可以正常工作
在标题weightx,weighty下链接的页面上,还有关于这些字段的更多说明
编辑:
您还需要为组件设置fill
,否则默认为NONE
:
GridBagConstraints.fill
用w