2

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

黄色 =JPanelBorderLayout

蓝色 =JPanelGridBagLayout

紫红色 =JPanelFlowLayout

有两个面板没有被颜色遮挡,值得一提:

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吗?在我看来,它就像“蓝色”,他只是给每个孩子一半CENTER的可用空间并保留剩余空间而不是向上收缩。我在这里想念什么?这仅仅是在“Fuchsia”上设置首选或最大尺寸的问题吗?这似乎不会让我到达我想去的地方,因为“紫红色”周围的边框(由我的颜色编码覆盖)是我希望组件结束的地方。BorderLayoutJPanel

4

2 回答 2

4

不使用 GridBagLayout 可能是

在此处输入图像描述

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();
            }
        });
    }
}
于 2012-06-07T16:00:25.727 回答
4

布局快照

看看这个输出,来自这个代码示例:

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();
            }
        });
    }
}
于 2012-06-07T16:19:08.230 回答