java - 当 OneTouchExpandable 设置为 true 时,如何以编程方式设置 JSplitPane 以隐藏右侧/底部组件?

标签 java swing jsplitpane

JSplitPane 中,您有 setOneTouchExpandable 方法,它为您提供了 2 个按钮来快速完全隐藏或完全显示 JSplitPane

我的问题是如何以编程方式“单击”JSplitPane 上的隐藏按钮?

我可能解释错了。我希望拆分 Pane 在开始时仅显示 2 个组件之一(这就是我单击的意思)。

这个有效:

import javax.swing.*;

class SplitPaneDefault {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JSplitPane sp = new JSplitPane(
                    JSplitPane.HORIZONTAL_SPLIT,
                    new JTree(),
                    new JTree());
                sp.setOneTouchExpandable(true);
                sp.setDividerLocation(0.0);
                JOptionPane.showMessageDialog(null, sp);
            }
        });
    }
}

但是将 0.0 替换为 1.0 并没有隐藏正确的组件。这是我的问题!

最佳答案

import javax.swing.*;

class SplitPaneDefault {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JSplitPane sp = new JSplitPane(
                    JSplitPane.HORIZONTAL_SPLIT,
                    new JTree(),
                    new JTree());
                sp.setOneTouchExpandable(true);
                sp.setDividerLocation(0.0);
                JOptionPane.showMessageDialog(null, sp);
            }
        });
    }
}

replace 0.0 with 1.0 and you get my problem

细读手册,解决问题。

This method immediately changes the size of the split pane based on its current size. If the split pane is not correctly realized and on screen, this method will have no effect ...

SplitPaneDefault

import javax.swing.*;

class SplitPaneDefault {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JSplitPane sp = new JSplitPane(
                    JSplitPane.HORIZONTAL_SPLIT,
                    new JTree(),
                    new JTree());
                sp.setOneTouchExpandable(true);
                JFrame f = new JFrame("Split Pane To Right");
                f.add(sp);
                f.pack();
                // sp now has a non-zero size!
                sp.setDividerLocation(1.0);
                f.setLocationByPlatform(true);
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f.setVisible(true);
            }
        });
    }
}

关于java - 当 OneTouchExpandable 设置为 true 时,如何以编程方式设置 JSplitPane 以隐藏右侧/底部组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9177182/

相关文章:

java - 使 BoxLayout 将组件移动到顶部,同时从左到右堆叠

java - 使用 JFrame.pack() 时如何布局两个 JSplitPanes 并均匀放置分隔符

java - 如何使 JPanel 在 SplitPane 中采用 100% 宽度和高度

java - 使用 JSplitPane 在 java 中向框架添加 3 个面板?

Java 程序将 n 以内的整数的倒数相加

java - 如何将 XmlCursor 内容插入 DOM 文档

java - 在 JTextArea 下面添加按钮

java - JList getSelectionValue 清除后返回 null

java - 我应该如何对 Java Web 应用程序进行单元测试

java - 如何使用 JDBC 在一个事务中执行 2 个更新查询