在 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 ...
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/