嵌套的JPanels和GridBagLayout Not" Packing"组件

时间:2012-06-07 15:43:32

标签: java swing awt gridbaglayout

我附上了适用以下Border图例的屏幕截图:

黄色= JPanel BorderLayout

蓝色= JPanel GridBagLayout

Fuchsia = JPanel FlowLayout

有两个面板没有被遮挡的颜色需要提及:

1)显示“主要”一词的标题面板;此面板位于“黄色”面板中的BorderLayout.NORTH处。

2)设备图像所在的图像面板;这个小组是“Fuchsia”的兄弟姐妹

Nested Layout Problem

“蓝色”位于“黄色”中的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?它看起来像“蓝色”,位于CENTER的{​​{1}}只是给每个孩子BorderLayout一半的可用空间并保留剩余空间而不是向上收缩。我在这里错过了什么?这只是在“紫红色”上设置首选或最大尺寸的问题吗?它似乎不会让我到达我想要的地方,因为“Fuchsia”(我的颜色编码覆盖)周围的边界是我想要组件结束的地方。

2 个答案:

答案 0 :(得分:4)

不使用GridBagLayout可能

enter image description here

import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.LineBorder;

public class NestedLayout {

    private JFrame frame = new JFrame();
    private JPanel yellowNorthPanel = new JPanel();
    private JPanel yellowPanel = new JPanel();
    private JPanel bluePanel = new JPanel();
    private JPanel fuchsiaTopPanel = new JPanel();
    private JPanel fuchsiaBottonPanel = new JPanel();

    public NestedLayout() {
        yellowNorthPanel.setBorder(new LineBorder(Color.yellow, 5));
        yellowPanel.setLayout(new BorderLayout());
        yellowPanel.setBorder(new LineBorder(Color.yellow, 5));
        bluePanel.setLayout(new BorderLayout(5, 5));
        bluePanel.setBorder(new LineBorder(Color.blue, 5));
        fuchsiaTopPanel.setBorder(new LineBorder(Color.cyan, 5));
        fuchsiaBottonPanel.setBorder(new LineBorder(Color.cyan, 5));
        bluePanel.add(fuchsiaTopPanel, BorderLayout.NORTH);
        bluePanel.add(fuchsiaBottonPanel);
        yellowPanel.add(bluePanel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(yellowNorthPanel, BorderLayout.NORTH);
        frame.add(yellowPanel);
        //frame.pack();
        frame.setSize(400, 300);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new NestedLayout();
            }
        });
    }
}

答案 1 :(得分:4)

LAYOUT SNAPSHOT

从这个代码示例中查看此输出:

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();
            }
        });
    }
}