简体   繁体   English

扩展JTree的节点可最大程度地减少GridBagLayout

[英]Expanding a node of a JTree minimizes GridBagLayout

I have created a simple Swing application which currently consists of a JToolBar, a JTree and a RSyntaxTextArea , all inside a GridBagLayout. 我创建了一个简单的Swing应用程序,该应用程序当前由GridBagLayout内部的JToolBar,JTree和RSyntaxTextArea组成

The JTree has currently only one top node and that has only one child node. JTree当前只有一个顶级节点,而只有一个子节点。 具有JToolBar,JTree和RSyntaxTextArea的完整UI Complete UI with JToolBar, JTree and RSyntaxTextArea 具有JToolBar,JTree和RSyntaxTextArea的完整UI

When expanding the top node of the JTree, the whole GridBagLayout kind of "minimizes": 扩展JTree的顶部节点时,整个GridBagLayout种类为“最小化”: 展开JTree的顶部节点后的UI。

I've googled this phenominum, but since there's no error message or something else in the console, I'm kind of helpless right now. 我已经用谷歌搜索了这个现象,但是由于控制台中没有错误消息或其他内容,我现在有点无助。

I'm using the following code to create the UI: 我正在使用以下代码创建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;
}

Update : A minimal code example to compile on your system for testing purposes: 更新 :一个最小的代码示例,可以在您的系统上进行编译以进行测试:

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": 扩展JTree的顶部节点时,整个GridBagLayout种类为“最小化”:

The GridBagLayout will shrink to the minimum size of a component when there is not enough space to display the entire component. 如果没有足够的空间来显示整个组件,则GridBagLayout将缩小到组件的最小尺寸。

Swing application which currently consists of a JToolBar, a JTree and a RSyntaxTextArea, all inside a GridBagLayout. Swing应用程序当前由GridBagLayout内部的JToolBar,JTree和RSyntaxTextArea组成。

I would just use the default BorderLayout of the frame: 我只使用框架的默认BorderLayout

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

Note how I added the JTree to a JScrollPane. 注意如何将JTree添加到JScrollPane。 Now scrollbars will appear for the tree when needed. 现在,在需要时将为树显示滚动条。

If you really want to use the GridBagLayout then read the section from the Swing tutorial on How to Use GridBagLayout for an explanation of how to use the various constraints. 如果您确实要使用GridBagLayout阅读Swing教程中有关如何使用GridBagLayout的部分, 获取有关如何使用各种约束的说明。 You may want to start with the "weightx/y" constraints which control which components get space as the frame size is changed. 您可能想从“ weightx / y”约束开始,该约束控制随着帧大小的改变哪些组件获得空间。 Also, look at the "fill" constraint. 另外,请查看“填充”约束。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM