【问题标题】:Expanding a node of a JTree minimizes GridBagLayout扩展 JTree 的节点最小化 GridBagLayout
【发布时间】:2017-12-31 17:47:04
【问题描述】:

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

JTree 目前只有一个顶部节点,并且只有一个子节点。 带有 JToolBar、JTree 和 RSyntaxTextArea 的完整 UI

当扩展 JTree 的顶部节点时,整个 GridBagLayout 有点“最小化”:

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

我正在使用以下代码来创建 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);
            }
        });
    }

}

【问题讨论】:

  • 请考虑创建一个有效的minimal reproducible example 并在您的问题中发布一个small 程序,我们可以编译、运行并证明您的问题对我们来说。
  • @HovercraftFullOfEels 当然,更新了我的问题。
  • (1-) @AlexanderLeithner,您发布的代码不是 MCVE,因为我们无权访问自定义类。无论如何,您已经有了答案。是时候做一些阅读了。
  • @camickr 哦,谢谢,忽略了它们...现在将用简单的 JTextAreas 替换它们...

标签: java swing jtree rsyntaxtextarea


【解决方案1】:

当扩展 JTree 的顶部节点时,整个 GridBagLayout 类型的“最小化”:

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

Swing 应用程序目前由一个 JToolBar、一个 JTree 和一个 RSyntaxTextArea 组成,都在一个 GridBagLayout 内。

我只会使用框架的默认BorderLayout

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

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

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

【讨论】:

  • 这会将 RSyntaxTextArea 附加到对话框的末尾,对吗?这不是我希望它显示的方式,就在它右侧的 JTree 旁边。但是很高兴知道缩小(但我真的不知道为什么应该没有足够的空间)......
  • @AlexanderLeithner, I want it to be displayed, just next to the JTree on its right - 然后使用不同的 BorderLayout 约束将文本区域放在右侧。阅读教程。我提供了指向 GridBagLayout 的链接。您还将在 BorderLayout 上找到一个部分。
  • 谢谢,忽略了weightx/y(但之前读过这个教程)...
猜你喜欢
  • 2010-10-05
  • 2011-08-20
  • 2012-05-18
  • 2015-10-23
  • 2011-01-29
  • 1970-01-01
  • 2010-11-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多