JSplitPane:有没有办法显示/隐藏其中一个窗格?

我有一个带有两个组件A和B的JSplitPane,但有时我希望能够隐藏B,以便满足以下任一条件:

  • 组件A和B在JSplitPane中可见
  • 只有组件A在JSplitPane占用的空间中可见

有没有办法做到这一点?

哎呀,我会尝试解决方案……

import java.awt.Dimension; import java.awt.event.*; import javax.swing.*; public class Test { public static void main(String[] args) { JFrame frame = new JFrame(); final JPanel contentPane = (JPanel)frame.getContentPane(); final JButton leftBtn = new JButton("Left Button"); final JButton rightBtn = new JButton("Right Button"); final JSplitPane jsp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftBtn, rightBtn); ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent e) { JButton source = (JButton)e.getSource(); if (jsp.isVisible()) { jsp.remove(rightBtn); jsp.remove(leftBtn); jsp.setVisible(false); contentPane.removeAll(); contentPane.add(source); } else { contentPane.removeAll(); jsp.setLeftComponent(leftBtn); jsp.setRightComponent(rightBtn); jsp.setDividerLocation(0.5); jsp.setVisible(true); contentPane.add(jsp); } contentPane.revalidate(); contentPane.repaint(); source.requestFocusInWindow(); } }; rightBtn.addActionListener(actionListener); leftBtn.addActionListener(actionListener); contentPane.add(jsp); contentPane.setPreferredSize(new Dimension(800, 600)); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); jsp.setDividerLocation(0.5); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } 

我发现Hovercraft Full Of Eels的版本存在问题而且是我自己的版本。

Hovercraft Full Of Eels的一个工作,但是如果你单击一个按钮,调整框架大小,然后再次单击按钮,则会出现图形错误; 也像amol说的那样,你可能希望拆分器位置在此过程中保持不变。

 import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JSplitPane; import javax.swing.UIManager; public class JSplitPaneShowHidePane { public static void main(String[] args) { JFrame frame = new JFrame(); JPanel contentPane = (JPanel)frame.getContentPane(); final JButton leftBtn = new JButton("Left Button"); final JButton rightBtn = new JButton("Right Button"); final JSplitPane jsp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftBtn, rightBtn); ActionListener actionListener = new ActionListener() { private int loc = 0; public void actionPerformed(ActionEvent e) { JButton source = (JButton)e.getSource(); if(jsp.getLeftComponent().isVisible() && jsp.getRightComponent().isVisible()){ loc = jsp.getDividerLocation(); jsp.setDividerSize(0); jsp.getLeftComponent().setVisible(source == leftBtn); jsp.getRightComponent().setVisible(source == rightBtn); }else{ jsp.getLeftComponent().setVisible(true); jsp.getRightComponent().setVisible(true); jsp.setDividerLocation(loc); jsp.setDividerSize((Integer) UIManager.get("SplitPane.dividerSize")); } } }; rightBtn.addActionListener(actionListener); leftBtn.addActionListener(actionListener); contentPane.add(jsp); frame.pack(); frame.setSize(800, 600); frame.setLocationRelativeTo(null); frame.setVisible(true); jsp.setDividerLocation(0.5); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } 

假设HORIZONTAL_SPLIT分为两个组件(左边是A,右边是B)

这是你如何隐藏A并让B占据所有splitpane的空间

 myButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { mySplitPane.setDividerSize(0); mySplitPane.setDividerLocation(mySplitPane.getLocation().x); } }); 

隐藏组件B并显示A –

 ... mySplitPane.setDividerLocation(pane.getLocation().x+pane.getSize().width); ... 

如果您有垂直分割,请使用类似的方法并使用ywidthheight切换x

要获得完整的解决方案,您必须收听resize事件(如果适用)并重新计算分隔符位置(这意味着您将存储当前在某处可见的状态)

如果您有对组件A和B的引用,则可以使用组件A或B的 JSplitPane.remove()方法或JComponent.setVisible(false)方法

测试代码:

  final JFrame f = new JFrame(); final JSplitPane jsp = new JSplitPane(); final JButton leftB = new JButton("Left: Hide Self"); final JButton rightB = new JButton("Right: Show Left"); jsp.setOneTouchExpandable(true); leftB.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jsp.remove(leftB); } }); rightB.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jsp.setLeftComponent(leftB); } }); jsp.setLeftComponent(leftB); jsp.setRightComponent(rightB); f.add(jsp); f.setSize(800, 600); f.setVisible(true); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

我用它来切换框架底部的日志面板:

 private void toggleLogPanel() { if(m_logPanel.isShowing()) { m_logDimension = m_logPanel.getSize(); m_splitpane.setBottomComponent(null); m_splitpane.setDividerSize(0); } else { m_logPanel.setPreferredSize(m_logDimension); m_splitpane.setBottomComponent(m_logPanel); m_splitpane.setDividerSize(new JSplitpane().getDividerSize()); } m_splitpane.resetToPreferredSizes(); } 

这会记住并恢复组件大小。