我已附上适用以下 Border
图例的屏幕截图:
黄色 = JPanel
和 BorderLayout
蓝色 = JPanel
和 GridBagLayout
Fuchsia = JPanel
和 FlowLayout
有两个面板没有用颜色遮挡,值得一提:
1) 显示“Primary”字样的标题面板;该面板位于“黄色”面板中的 BorderLayout.NORTH
处。
2) 设备图像所在的图像面板;该面板是“Fuchsia”的同级面板
“蓝色”位于“黄色”中的 BorderLayout.CENTER
,而“紫红色”和图像面板受到以下约束:
GridBagConstraints c = new GridBagConstraints();
c.weightx = 1.0;
c.weighty = 1.0;
c.gridx = 0;
c.gridy = 0;
c.anchor = GridBagConstraints.NORTHWEST;
c.insets = new Insets(0, 10, 0, 0);
c.fill = GridBagConstraints.BOTH;
//"Blue".add(imagePanel, c);
c.weighty = 0.80;
c.gridy = 1;
c.fill = GridBagConstraints.HORIZONTAL;
//"Blue".add("Fuchsia", c);
正如您可能从图像中看出的那样,我正在尝试消除“紫红色”正下方“蓝色”中的“浪费”空间。我似乎无法使用 GridBagConstraints 来做到这一点,所以我只是使用了错误的 LayoutManager 吗?在我看来,“Blue”位于 BorderLayout
中的 CENTER
,只是为每个子 JPanel
提供了一半的可用空间并保留剩余空间而不是向上收缩。我在这里缺少什么?这只是在“Fuchsia”上设置首选或最大尺寸的问题吗?这似乎不会让我到达我想要的位置,因为“Fuchsia”周围的边框(被我的颜色编码覆盖)是我想要组件末尾的位置。
最佳答案
看看这个代码示例的输出:
import java.awt.*;
import javax.swing.*;
public class LayoutTest
{
private void displayGUI()
{
JFrame frame = new JFrame("Layout Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel contentPane = new JPanel();
contentPane.setOpaque(true);
contentPane.setBackground(Color.YELLOW);
contentPane.setLayout(new BorderLayout(2, 2));
JPanel topPanel = new JPanel();
topPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
JLabel headingLabel = new JLabel("Primary");
topPanel.add(headingLabel);
contentPane.add(topPanel, BorderLayout.PAGE_START);
JPanel centerPanel = new JPanel();
centerPanel.setOpaque(true);
centerPanel.setBackground(Color.BLUE);
centerPanel.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1.0;
gbc.weighty = 0.2;
gbc.gridx = 0;
gbc.gridy = 0;
JPanel imagePanel = new JPanel();
JLabel imageLabel = null;
try
{
imageLabel = new JLabel(
new ImageIcon(
new java.net.URL(
"http://pscode.org/"
+ "tame/screenshot/"
+ "landscape/slider1.gif")));
}
catch(Exception e)
{
e.printStackTrace();
}
imagePanel.add(imageLabel);
centerPanel.add(imagePanel, gbc);
JPanel detailsPanel = new JPanel();
detailsPanel.setOpaque(true);
detailsPanel.setBackground(Color.WHITE);
detailsPanel.setBorder(
BorderFactory.createEmptyBorder(
5, 5, 5, 5));
detailsPanel.setLayout(new GridLayout(0, 1, 5, 5));
JLabel statusLabel = new JLabel("Chassis Status : ");
JLabel usageLabel = new JLabel("Bandwidth Usage : ");
JLabel fanLabel = new JLabel("Fan Status : ");
detailsPanel.add(statusLabel);
detailsPanel.add(usageLabel);
detailsPanel.add(fanLabel);
gbc.fill = GridBagConstraints.BOTH;
gbc.weighty = 0.8;
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridheight = 3;
centerPanel.add(detailsPanel, gbc);
contentPane.add(centerPanel, BorderLayout.CENTER);
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new LayoutTest().displayGUI();
}
});
}
}
关于java - 嵌套 JPanels 和 GridBagLayout 不是 "Packing"组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10935206/