扩展 JTree 的节点最小化 GridBagLayout

Expanding a node of a JTree minimizes GridBagLayout

我创建了一个简单的 Swing 应用程序,它目前由一个 JToolBar、一个 JTree 和一个 RSyntaxTextArea 组成,都在一个 GridBagLayout 中。

JTree目前只有一个顶层节点,也只有一个子节点。 使用 JToolBar、JTree 和 RSyntaxTextArea 完成 UI

当扩展JTree 的顶部节点时,整个GridBagLayout 类"minimizes":

我用谷歌搜索了这个现象,但由于控制台中没有错误消息或其他信息,我现在有点无助。

我正在使用以下代码创建 UI:

RSyntaxTextArea textArea = new RSyntaxTextArea(50, 150);
textArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JAVA);
textArea.setCodeFoldingEnabled(true);
RTextScrollPane sp = new RTextScrollPane(textArea);

cp.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;

c.gridx = 0;
c.gridy = 0;
cp.add(createToolbar(), c);

c.gridx = 0;
c.gridy = 1;
c.ipadx = 90;
c.fill = GridBagConstraints.BOTH;
cp.add(createTree(), c);

c.gridx = 1;
c.gridy = 1;
c.fill = GridBagConstraints.HORIZONTAL;
cp.add(sp, c);

...

private JToolBar createToolbar() {
    JToolBar tb = new JToolBar("Toolbar", JToolBar.HORIZONTAL);

    JButton ob = new JButton(new ImageIcon("..."));

    tb.add(ob);

    tb.setFloatable(false);
    tb.setRollover(true);

    return tb;
}

...

private JTree createTree() {
    DefaultMutableTreeNode top = new DefaultMutableTreeNode("Projects");
    JTree tree = new JTree(top);

    DefaultMutableTreeNode test = new DefaultMutableTreeNode("I'm a test!");
    top.add(test);

    return tree;
}

更新:为测试目的在您的系统上编译的最小代码示例:

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class Tester extends JFrame {

    public Tester () {
        initializeComponent();
    }

    private void initializeComponent() {
        JPanel cp = new JPanel(new BorderLayout());
        JTextArea textArea = new JTextArea(50, 150);
        JScrollPane sp = new JScrollPane(textArea);
        cp.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 0;
        cp.add(createToolbar(), c);
        c.gridx = 0;
        c.gridy = 1;
        c.ipadx = 90;
        c.fill = GridBagConstraints.BOTH;
        cp.add(createTree(), c);
        c.gridx = 1;
        c.gridy = 1;
        c.fill = GridBagConstraints.HORIZONTAL;
        cp.add(sp, c);
        setContentPane(cp);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        pack();
        setLocationRelativeTo(null);
    }

    private JTree createTree() {
        DefaultMutableTreeNode top = new DefaultMutableTreeNode("Projects");
        JTree tree = new JTree(top);
        DefaultMutableTreeNode test = new DefaultMutableTreeNode("I'm a test!");
        top.add(test);
        return tree;
    }

    private JToolBar createToolbar() {
        JToolBar tb = new JToolBar("Toolbar", JToolBar.HORIZONTAL);
        JButton ob = new JButton("Button");
        tb.add(ob);
        tb.setFloatable(false);
        tb.setRollover(true);
        return tb;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new Tester().setVisible(true);
            }
        });
    }

}

When expanding the top node of the JTree, the whole GridBagLayout kind of "minimizes":

当没有足够的space显示整个组件时,GridBagLayout 将缩小到组件的最小尺寸。

Swing application which currently consists of a JToolBar, a JTree and a RSyntaxTextArea, all inside a GridBagLayout.

我只使用框架的默认 BorderLayout:

add(toolbar, BorderLayout.PAGE_START);
add(treeScrollPane, BorderLayout.CENTER);
add(textAreaScrollPane, BorderLayout.PAGE_END);

请注意我是如何将 JTree 添加到 JScrollPane 的。现在滚动条会在需要时出现在树上。

如果您真的想使用 GridBagLayout,请阅读 Swing 教程中有关 How to Use GridBagLayout 的部分,了解如何使用各种约束。您可能希望从 "weightx/y" 约束开始,该约束控制在帧大小更改时哪些组件获得 space。另外,请查看 "fill" 约束条件。