1

我想创建 2 个嵌套边框布局,其中内部边框布局将面板保持在其中心。该面板应具有垂直流动,特定组件应增长并一起使用所有可用空间。最后这个面板应该被包裹起来JScrollPane,所以当不是所有的东西都可以以默认大小显示时,它会得到一个垂直滚动条。类似于以下内容:

+-------------------------------+
|                               |
|   +-----------------------+   |
|   |       don't grow      |   |
|   +-----------------------+   |
|                               |
|   +-----------------------+   |
|   |                       |   |
|   |       growy           |   |
|   |                       |   |
|   +-----------------------+   |
|                               |
|   +-----------------------+   |
|   |       don't grow      |   |
|   +-----------------------+   |
|                               |
|   +-----------------------+   |
|   |                       |   |
|   |       growy           |   |
|   |                       |   |
|   +-----------------------+   |
|                               |
+-------------------------------+

这是我走了多远:

在此处输入图像描述

提供以下SSCCE:

public class SimpleMain {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                //createAndShowMigGUI();
                createAndShowSwingGUI();
            }
        });
    }

    private static void createAndShowSwingGUI() {
        JFrame frame = new JFrame("Swing");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        /* Frame */
        frame.setLayout(new BorderLayout());

        /* Outer Panel */
        JPanel outerPanel = new JPanel(new BorderLayout());

        outerPanel.add(new JTextField("inner north"), BorderLayout.NORTH);
        outerPanel.add(new JTextField("inner south"), BorderLayout.SOUTH);
        outerPanel.add(new JTextField("inner west"), BorderLayout.WEST);
        outerPanel.add(new JTextField("inner east"), BorderLayout.EAST);

        /* Text Area */
        JTextArea area = new JTextArea("aHJAskjdhuyawduyawd Askjdhuyawduyawd Askjdhuyawduyawd AskjdhuyawduyawdHJAskjdhuyawduyawd Askjdhuyawduyawd Askjdhuyawduyawd AskjdhuyawduyawdHJAskjdhuyawduyawd Askjdhuyawduyawd Askjdhuyawduyawd AskjdhuyawduyawdHJAskjdhuyawduyawd Askjdhuyawduyawd Askjdhuyawduyawd AskjdhuyawduyawdHJAskjdhuyawduyawd Askjdhuyawduyawd Askjdhuyawduyawd Askjdhuyawduyawd");
        area.setLineWrap(true);
        area.setWrapStyleWord(true);

        /* Elements Container */
        JPanel innerPanel = new JPanel(new GridBagLayout());

        /* Grid Bag Constraints */
        GridBagConstraints cons = new GridBagConstraints();
        cons.fill = GridBagConstraints.HORIZONTAL;
        cons.weightx = 1;
        cons.gridx = 0;

        /* Elements */        
        innerPanel.add(area, cons); // This component should use all available Y-space
        innerPanel.add(new JRadioButton("Test hello"), cons); // Remain as-is
        innerPanel.add(new JTextField("some text field..."), cons); // Remain as-is
        innerPanel.add(new JTextField("some text field PUSH..."), cons); // This component should use all available Y-space 

        /* Add Inner Panel To Outer Panel */
        outerPanel.add(new JScrollPane(innerPanel), BorderLayout.CENTER);

        /* Add outer elements */
        frame.getContentPane().add(new JTextField("outer north"), BorderLayout.NORTH);
        frame.getContentPane().add(new JTextField("outer south"), BorderLayout.SOUTH);
        frame.getContentPane().add(new JTextField("outer west"), BorderLayout.WEST);
        frame.getContentPane().add(new JTextField("outer east"), BorderLayout.EAST);
        frame.getContentPane().add(outerPanel, BorderLayout.CENTER);

        frame.setSize(800, 600);
        frame.setVisible(true);
        frame.setLocation(40, 40);
    }
}
4

1 回答 1

2

我无法GridBagConstraints为 a 的所有组件使用一个GridBagLayout,因此我为每个组件创建了约束。反正我不记得默认值。

更改包括添加权重和为GridBagConstraints.

Swing GridBagLayout

编辑以回应评论。

当窗口 ( JFrame) 展开时,JTextArea与窗口中的其他组件一起展开。当窗口尺寸缩小时,它JTextArea不会缩小。为了解决这个问题,我将 , 放入JTextArea,并在每次大小更改时JScrollPane删除并添加。JTextAreaJFrame

这是代码。

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.WindowEvent;
import java.awt.event.WindowStateListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class SimpleMain {

    protected static final Insets   DEFAULT_INSETS  = new Insets(0, 4, 4, 4);

    protected static JTextArea area;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                // createAndShowMigGUI();
                createAndShowSwingGUI();
            }
        });
    }

    private static void createAndShowSwingGUI() {
        final JFrame frame = new JFrame("Swing");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        /* Frame */
        frame.setLayout(new BorderLayout());

        /* Outer Panel */
        JPanel outerPanel = new JPanel(new BorderLayout());

        outerPanel.add(new JTextField("inner north"), BorderLayout.NORTH);
        outerPanel.add(new JTextField("inner south"), BorderLayout.SOUTH);
        outerPanel.add(new JTextField("inner west"), BorderLayout.WEST);
        outerPanel.add(new JTextField("inner east"), BorderLayout.EAST);

        area = setTextArea();

        final JScrollPane scrollPane = new JScrollPane(area);

        /* Elements Container */
        final JPanel innerPanel = new JPanel(new GridBagLayout());

        /* Elements */
        int gridy = 0;

        // This component should use all available Y-space
        addComponent(innerPanel, scrollPane, 0, gridy++, 1, 1, 1.0D, 800.0D,
                DEFAULT_INSETS, GridBagConstraints.LINE_START,
                GridBagConstraints.BOTH);

        // Remain as-is
        JRadioButton helloButton = new JRadioButton("Test hello");
        addComponent(innerPanel, helloButton, 0, gridy++, 1, 1, 1.0D, 1.0D,
                DEFAULT_INSETS, GridBagConstraints.LINE_START,
                GridBagConstraints.HORIZONTAL);

        // Remain as-is
        JTextField textField1 = new JTextField("some text field...");
        addComponent(innerPanel, textField1, 0, gridy++, 1, 1, 1.0D, 1.0D,
                DEFAULT_INSETS, GridBagConstraints.LINE_START,
                GridBagConstraints.HORIZONTAL);

        // This component should use all available Y-space
        JTextField textField2 = new JTextField("some text field PUSH...");
        addComponent(innerPanel, textField2, 0, gridy++, 1, 1, 1.0D, 800.0D,
                DEFAULT_INSETS, GridBagConstraints.LINE_START,
                GridBagConstraints.BOTH);

        /* Add Inner Panel To Outer Panel */
        outerPanel.add(new JScrollPane(innerPanel), BorderLayout.CENTER);

        /* Add outer elements */
        frame.getContentPane().add(new JTextField("outer north"),
                BorderLayout.NORTH);
        frame.getContentPane().add(new JTextField("outer south"),
                BorderLayout.SOUTH);
        frame.getContentPane().add(new JTextField("outer west"),
                BorderLayout.WEST);
        frame.getContentPane().add(new JTextField("outer east"),
                BorderLayout.EAST);
        frame.getContentPane().add(outerPanel, BorderLayout.CENTER);

        frame.addWindowStateListener(new WindowStateListener() {
            @Override
            public void windowStateChanged(WindowEvent event) {
                scrollPane.getViewport().remove(area);
                area = setTextArea();
                scrollPane.getViewport().add(area);
            }
        });

        frame.setSize(800, 600);
        frame.setLocation(40, 40);
        frame.setVisible(true);
    }

    private static JTextArea setTextArea() {
        /* Text Area */
        JTextArea area = new JTextArea();
        area.setText("aHJAskjdhuyawduyawd Askjdhuyawduyawd Askjdhuyawduyawd "
                + "AskjdhuyawduyawdHJAskjdhuyawduyawd Askjdhuyawduyawd "
                + "Askjdhuyawduyawd AskjdhuyawduyawdHJAskjdhuyawduyawd "
                + "Askjdhuyawduyawd Askjdhuyawduyawd "
                + "AskjdhuyawduyawdHJAskjdhuyawduyawd Askjdhuyawduyawd "
                + "Askjdhuyawduyawd AskjdhuyawduyawdHJAskjdhuyawduyawd "
                + "Askjdhuyawduyawd Askjdhuyawduyawd Askjdhuyawduyawd");

        area.setLineWrap(true);
        area.setWrapStyleWord(true);
        return area;
    }

    private static void addComponent(Container container, Component component,
            int gridx, int gridy, int gridwidth, int gridheight,
            double weightx, double weighty, Insets insets, int anchor, int fill) {
        GridBagConstraints gbc = new GridBagConstraints(gridx, gridy,
                gridwidth, gridheight, weightx, weighty, anchor, fill, insets,
                0, 0);
        container.add(component, gbc);
    }
}
于 2013-07-19T15:52:09.383 回答