[英]Make GridBagLayout JPanel size to contents, not container?
我有一个GridBagLayout
JPanel
包含一个内BorderLayout
JPanel
。 我希望GridBagLayout
自行调整其内部网格的大小。 而是将其自身设置为BorderLayout
大小,并在内部网格的侧面添加空格( GridBagLayout
中的所有权重均为0)。
这个想法是我希望BorderLayout
提供空白(带有胶水)。 相反, GridBagLayout
提供空白; 您可以通过查看我添加的TitleBorder
看到它。
这样做的原因是,当我将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| | | +-----------+ | | | | | +-------------------------------------------+
这是上面的代码产生的显示:
将组件以其首选大小居中的一个好技巧(例如,问题代码中的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);
}
}
具有绝对布局(空)的解决方案如下所示:
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());
容纳所包含的对象gb.setSize(gb.getPreferredSize());
。
除了将面板gb直接添加到contentPanel之外,还可以将另一个带有GridBagLayout的面板添加到contentPane并将面板gb添加到该面板(不填充)。
在给定了北/东/西/南组件的首选大小之后,带有BorderLayout的Panel的“中心”组件将始终获得所有剩余空间。 因此,上面的中间人组件将“吸收” BorderLayout为其提供的所有空间,并且,只要您未添加带有“填充”约束的gb,它就会以所需方式居中显示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.