我有一个带有两个部分的JSplitPane。我想在单击按钮时隐藏正确的部分,并在再次单击该按钮时将其恢复。
这是我到目前为止所做的:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
if (PrincipalSplitPane.getDividerSize() == 1.0)
PrincipalSplitPane.setDividerLocation(0.0);
else
PrincipalSplitPane.setDividerLocation(1.0);
}
答案 0 :(得分:1)
您可以使用getRightComponent()
的{{1}}方法,然后调用JSplitPane
隐藏它。
要再次显示,请使用setVisible(false)
方法调用setVisible(true)
并设置分频器位置。
setDividerLocation(...)
如果您需要保存之前的import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.SwingUtilities;
@SuppressWarnings("serial")
public class ResizingSplitPane extends JFrame implements ActionListener
{
private JButton clickMe;
private JPanel leftPanel;
private JPanel rightPanel;
private JSplitPane split;
// private int dividerLocation;
public ResizingSplitPane()
{
setSize(300, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(new BorderLayout());
// Center Panel
clickMe = new JButton("Click Me!!");
clickMe.addActionListener(this);
JPanel panel = new JPanel(new BorderLayout());
panel.add(clickMe);
// Bottom Panel
leftPanel = new JPanel(new BorderLayout());
rightPanel = new JPanel(new BorderLayout());
leftPanel.add(new JLabel("Left"), BorderLayout.CENTER);
rightPanel.add(new JLabel("Right"), BorderLayout.CENTER);
split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPanel, rightPanel);
split.setResizeWeight(.5d);
add(panel, BorderLayout.PAGE_START);
add(split, BorderLayout.CENTER);
setVisible(true);
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
new ResizingSplitPane();
}
});
}
@Override
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == clickMe)
{
if (split.getRightComponent().isVisible())
{
// dividerLocation = split.getDividerLocation();
split.getRightComponent().setVisible(false);
}
else
{
split.getRightComponent().setVisible(true);
split.setDividerLocation(0.5);
}
}
}
}
位置,可以将当前位置存储到变量中(例如代码中的divider
变量),然后调用dividerLocation
传递时在那个变量中。