2

当我隐藏 JXMultiSplitPane 的一个节点时,我遇到了绘画问题。以下 SSCCE 说明了这种行为:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

import org.jdesktop.swingx.JXMultiSplitPane;
import org.jdesktop.swingx.MultiSplitLayout;
import org.jdesktop.swingx.MultiSplitLayout.Divider;
import org.jdesktop.swingx.MultiSplitLayout.Leaf;
import org.jdesktop.swingx.MultiSplitLayout.Node;
import org.jdesktop.swingx.MultiSplitLayout.Split;

public class TestMultiSplitPane {

    public static final String LEFT = "left";
    public static final String CENTER = "center";
    public static final String RIGHT = "right";

    public static final String TOP = "top";
    public static final String MIDDLE = "middle";
    public static final String BOTTOM = "bottom";

    protected void initUI() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Split horizontal = new Split();
        Split left = getVerticalSplit(LEFT, 0.5, 0.5);
        left.setWeight(0.2);
        Split center = getVerticalSplit(CENTER, 0.8, 0.2);
        center.setWeight(0.6);
        Split right = getVerticalSplit(RIGHT, 0.5, 0.5);
        right.setWeight(0.2);
        horizontal.setChildren(left, new Divider(), center, new Divider(), right);
        MultiSplitLayout layout = new MultiSplitLayout(horizontal);
        JXMultiSplitPane splitPane = new JXMultiSplitPane(layout);
        addButton(LEFT + TOP, splitPane);
        addButton(CENTER + TOP, splitPane);
        addButton(RIGHT + TOP, splitPane);
        addButton(LEFT + BOTTOM, splitPane);
        addButton(CENTER + BOTTOM, splitPane);
        addButton(RIGHT + BOTTOM, splitPane);
        frame.add(splitPane);
        frame.setBounds(50, 50, 1024, 768);
        frame.setVisible(true);
    }

    protected void addButton(final String buttonName, final JXMultiSplitPane splitPane) {
        final JButton button = new JButton(buttonName);
        splitPane.add(buttonName, button);
        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                MultiSplitLayout layout = splitPane.getMultiSplitLayout();
                Node node = layout.getNodeForName(buttonName);
                Split parent = node.getParent();
                parent.hide(node);
                splitPane.invalidate();
                splitPane.revalidate();
                splitPane.repaint();
            }
        });
    }

    public Split getVerticalSplit(String name, double topWeight, double bottomWeight) {
        Split split = new Split();
        split.setRowLayout(false);
        Leaf top = new Leaf(name + TOP);
        top.setWeight(topWeight);
        Leaf bottom = new Leaf(name + BOTTOM);
        bottom.setWeight(bottomWeight);
        split.setChildren(top, new Divider(), bottom);
        return split;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new TestMultiSplitPane().initUI();

            }
        });
    }

}

您可以单击显示的任何按钮,它应该会被隐藏,但是当我将鼠标移到不同的按钮上时,拆分窗格的重新绘制是有问题的。

对于那些使用 Maven 的人,这里是必需的依赖项:

    <dependency>
        <groupId>org.swinglabs.swingx</groupId>
        <artifactId>swingx-core</artifactId>
        <version>1.6.3</version>
    </dependency>
4

2 回答 2

1

我最终找到了解决方案。要在布局中隐藏节点,我们可以简单地使用 方法displayNode(String, boolean)

因此,在 中actionPerformed,我需要调用的是:

layout.displayNode(buttonName, false);

完整的工作代码:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

import org.jdesktop.swingx.JXMultiSplitPane;
import org.jdesktop.swingx.MultiSplitLayout;
import org.jdesktop.swingx.MultiSplitLayout.Divider;
import org.jdesktop.swingx.MultiSplitLayout.Leaf;
import org.jdesktop.swingx.MultiSplitLayout.Split;

public class TestMultiSplitPane {

    public static final String LEFT = "left";
    public static final String CENTER = "center";
    public static final String RIGHT = "right";

    public static final String TOP = "top";
    public static final String MIDDLE = "middle";
    public static final String BOTTOM = "bottom";

    protected void initUI() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Split horizontal = new Split();
        Split left = getVerticalSplit(LEFT, 0.5, 0.5);
        left.setWeight(0.2);
        Split center = getVerticalSplit(CENTER, 0.8, 0.2);
        center.setWeight(0.6);
        Split right = getVerticalSplit(RIGHT, 0.5, 0.5);
        right.setWeight(0.2);
        horizontal.setChildren(left, new Divider(), center, new Divider(), right);
        MultiSplitLayout layout = new MultiSplitLayout(horizontal);
        JXMultiSplitPane splitPane = new JXMultiSplitPane(layout);
        addButton(LEFT + TOP, splitPane);
        addButton(CENTER + TOP, splitPane);
        addButton(RIGHT + TOP, splitPane);
        addButton(LEFT + BOTTOM, splitPane);
        addButton(CENTER + BOTTOM, splitPane);
        addButton(RIGHT + BOTTOM, splitPane);
        frame.add(splitPane);
        frame.setBounds(50, 50, 1024, 768);
        frame.setVisible(true);
    }

    protected void addButton(final String buttonName, final JXMultiSplitPane splitPane) {
        final JButton button = new JButton(buttonName);
        splitPane.add(buttonName, button);
        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                MultiSplitLayout layout = splitPane.getMultiSplitLayout();
                layout.displayNode(buttonName, false);
            }
        });
    }

    public Split getVerticalSplit(String name, double topWeight, double bottomWeight) {
        Split split = new Split();
        split.setRowLayout(false);
        Leaf top = new Leaf(name + TOP);
        top.setWeight(topWeight);
        Leaf bottom = new Leaf(name + BOTTOM);
        bottom.setWeight(bottomWeight);
        split.setChildren(top, new Divider(), bottom);
        return split;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new TestMultiSplitPane().initUI();

            }
        });
    }
}
于 2013-03-14T13:55:49.307 回答
1

我知道你问这个问题已经有一段时间了,但我搜索了很多,直到我找到了一个解决方案(虽然不是一个很好的解决方案),也许这会对某人有所帮助。
我在添加到 splitPane 的面板中添加/删除了很多组件,如果我在添加组件之前调整了拆分器的大小,它就会卡在那里,要么剪掉我的面板的一部分,要么变大。

正如文档中所说:

MultiSplitLayout 的“floatingDividers”属性最初为 true,一旦任何 Divider 重新定位,MultiSplitPane 就会将其设置为 false。当 floatingDividers 为 false 时,每个 Leaf(组件)的右/下边缘由其后面的 Divider 的位置定义。换句话说,一旦用户移动 Divider,布局就不再取决于任何组件的首选大小,而是由 Divider 的当前位置和权重定义。

我所做的是在每次调整大小时调用 setFloatingDividers(true) 方法(与添加/删除组件相关的调整大小)。由此产生的问题是分离器的位置丢失。

于 2013-03-14T13:48:39.000 回答