【问题标题】:JSplitPane hide and show the right sectionJSplitPane 隐藏和显示右侧部分
【发布时间】:2014-11-21 11:15:15
【问题描述】:

我有一个包含两个部分的 JSplitPane。我想在单击按钮时隐藏右侧部分,并在再次单击同一按钮时将其恢复。

这是我目前所拥有的:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    if (PrincipalSplitPane.getDividerSize() == 1.0)
        PrincipalSplitPane.setDividerLocation(0.0);
    else
        PrincipalSplitPane.setDividerLocation(1.0);
    }

【问题讨论】:

  • 我怀疑getDividerSize() == 1.0 作为谓词; setDividerLocation() 似乎在这个 complete example 中工作。

标签: java swing jsplitpane


【解决方案1】:

您可以使用JSplitPanegetRightComponent()方法,然后调用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 变量),然后在调用setDividerLocation 时传入该变量。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-10
    • 2013-09-06
    • 1970-01-01
    • 2010-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多