Java 使GridBagLayout JPanel的大小与内容而不是容器相符?
我有一个Java 使GridBagLayout JPanel的大小与内容而不是容器相符?,java,swing,size,layout-manager,gridbaglayout,Java,Swing,Size,Layout Manager,Gridbaglayout,我有一个GridBagLayoutJPanel包含在BorderLayoutJPanel中。我希望GridBagLayout根据其内部网格调整自身大小。相反,它将自身调整为BorderLayout,在内部网格的两侧添加空白(gridbagloayout中的所有权重均为0) 我的想法是希望BorderLayout提供空白(使用胶水)。相反,GridBagLayout提供空白;通过查看我添加的标题顺序 这样做的原因是,当我将GridBagLayout发送到打印机时,我不希望包含所有空格 import
GridBagLayout
JPanel
包含在BorderLayout
JPanel
中。我希望GridBagLayout
根据其内部网格调整自身大小。相反,它将自身调整为BorderLayout
,在内部网格的两侧添加空白(gridbagloayout
中的所有权重均为0)
我的想法是希望BorderLayout
提供空白(使用胶水)。相反,GridBagLayout
提供空白;通过查看我添加的标题顺序
这样做的原因是,当我将GridBagLayout
发送到打印机时,我不希望包含所有空格
import java.awt.*;
import javax.swing.*;
public class GBLDemo {
public static void main (String[] args) {
JFrame jframe = new JFrame("GridBagLayout Demo");
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane = new JPanel(new BorderLayout());
jframe.setContentPane(contentPane);
GridBagConstraints gbc = new GridBagConstraints();
JPanel gb = new JPanel(new GridBagLayout());
contentPane.add(gb);
gbc.gridx = gbc.gridy = 0;
gb.add(new JLabel("Look "), gbc);
gbc.gridx = 1;
gb.add(new JLabel("at"), gbc);
gbc.gridx = 0;
gbc.gridy = 1;
gb.add(new JLabel("the "), gbc);
gbc.gridx = 1;
gb.add(new JLabel("border"), gbc);
gb.setBorder(BorderFactory.createTitledBorder("Border"));
jframe.pack();
jframe.setVisible(true);
jframe.setSize(640, 480);
} // main(args)
} // GBLDemo
下面是我想要的ASCII模型:
+-------------------------------------------+
| |
| |
| +Border-----+ |
| |Look at | |
| | the border| |
| +-----------+ |
| |
| |
+-------------------------------------------+
+-------------------------------------------+
| |
| |
|+边界------|
||看||
||边界||
| +-----------+ |
| |
| |
+-------------------------------------------+
以下是由上述代码生成的显示:
具有绝对布局(null)的解决方案如下所示:
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class GBLDemo {
public static void main (String[] args) {
JFrame jframe = new JFrame("GridBagLayout Demo");
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane = new JPanel();
contentPane.setLayout(null);
jframe.setContentPane(contentPane);
jframe.setVisible(true);
jframe.setSize(640, 480);
GridBagConstraints gbc = new GridBagConstraints();
JPanel gb = new JPanel(new GridBagLayout());
contentPane.add(gb);
gbc.gridx = gbc.gridy = 0;
gb.add(new JLabel("Look "), gbc);
gbc.gridx = 1;
gb.add(new JLabel("at"), gbc);
gbc.gridx = 0;
gbc.gridy = 1;
gb.add(new JLabel("the "), gbc);
gbc.gridx = 1;
gb.add(new JLabel("border"), gbc);
gb.setBorder(BorderFactory.createTitledBorder("Border"));
gb.setSize(gb.getPreferredSize());
gb.setLocation(jframe.getWidth()/2-gb.getWidth()/2, jframe.getHeight()/2-gb.getHeight()/2);
jframe.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent arg0) {
gb.setSize(gb.getPreferredSize());
gb.setLocation(jframe.getWidth()/2-gb.getWidth()/2, jframe.getHeight()/2-gb.getHeight()/2);
}
});
} // main(args)
} // GBLDemo
制作此文件:
gb JPanel将始终处于中心位置,ComponentListener负责这一点。gp将使用
gb.setSize(gb.getPreferredSize())更改其大小以自动适应包含的对象代码>将组件居中于其首选尺寸(例如问题代码中的contentPane
)的一个好技巧是将其作为单个组件添加到具有网格袋布局的容器中,而不指定约束
下面是上面的代码,完全适合这样做
import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class GBLDemo {
private JComponent ui = null;
GBLDemo() {
initUI();
}
private Container getContentPanel() {
Container contentPane = new JPanel(new BorderLayout());
GridBagConstraints gbc = new GridBagConstraints();
JPanel gb = new JPanel(new GridBagLayout());
contentPane.add(gb);
gbc.gridx = gbc.gridy = 0;
gb.add(new JLabel("Look "), gbc);
gbc.gridx = 1;
gb.add(new JLabel("at"), gbc);
gbc.gridx = 0;
gbc.gridy = 1;
gb.add(new JLabel("the "), gbc);
gbc.gridx = 1;
gb.add(new JLabel("border"), gbc);
gb.setBorder(BorderFactory.createTitledBorder("Border"));
return contentPane;
}
public void initUI() {
if (ui!=null) return;
// A single object added to a grid bag layout with no constraint,
// will be centered within the available space.
ui = new JPanel(new GridBagLayout());
ui.setBorder(new EmptyBorder(4,4,4,4));
ui.add(getContentPanel());
}
public JComponent getUI() {
return ui;
}
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception useDefault) {
}
GBLDemo o = new GBLDemo();
JFrame f = new JFrame(o.getClass().getSimpleName());
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLocationByPlatform(true);
f.setContentPane(o.getUI());
f.pack();
f.setMinimumSize(f.getSize());
f.setVisible(true);
}
};
SwingUtilities.invokeLater(r);
}
}
您可以将另一个具有GridBagLayout的面板添加到contentPane,并将面板gb添加到该面板(无填充),而不是直接将面板gb添加到contentPanel
在为北/东/西/南组件指定其首选尺寸后,具有边界布局的面板的“中心”组件始终获得所有剩余空间。因此,上面的中间人组件将“吸收”BorderLayout提供给它的所有空间,只要你没有添加带有“填充”约束的gb,它应该以你想要的方式居中显示。根据你对问题的口头描述,我们无法猜测你在做什么。张贴一篇正确的演示问题的文章。以最小尺寸提供ASCII艺术或GUI预期布局的简单绘图,如果可以调整大小,则提供更大的宽度和高度。@camickr完成!这更好吗?@AndrewThompson这样行吗?是的。这正是我们所需要的。看到我的答案了。这是朝着正确的方向迈出的一步,但我希望GridBagLayout的大小能够自动计算,以紧密地适应网格。我的目标是消除它周围的空白。@BruceFeist我根据您的评论修改了源代码。您所需要的只是gb.setSize(gb.getPreferredSize());虽然我选择了@AndrewThompson,但这对我来说似乎是一个很好的解决方案。如果可能的话,我更喜欢避免绝对布局。这对我来说没什么区别。从BorderLayout切换到@AndrewThompson建议的另一个GridBagLayout几乎是一样的,但这确实有效。我使用了你的建议(虽然不是你的代码,我需要对其进行分析,以了解与我通常的方法不相关的差异),它非常有效。谢谢