当我隐藏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>
答案 0 :(得分:1)
我知道自从你提出这个问题以来已经有一段时间了,但我搜索了很多,直到我找到了一个解决方案(虽然不是一个很好的解决方案),也许这会对某人产生帮助。
我在添加到splitPane的面板中做了很多添加/删除组件,如果我在添加组件之前调整了分割器的大小,它就会卡在那里并切断我的面板的一部分或者变大。
正如文档中所述:
MultiSplitLayout&#34; floatingDividers&#34;只要重新定位任何Divider,MultiSplitPane就会将属性(最初为true)设置为false。当floatingDividers为false时,每个Leaf(组件)的右/下边缘由其后面的Divider的位置定义。换句话说,一旦用户移动分隔符,布局就不再取决于任何组件的首选大小,它由分隔符的当前位置和权重定义。
我所做的是在每次调整大小时调用setFloatingDividers(true)方法(调整大小与添加/删除组件相关)。由此产生的问题是分离器的位置丢失了。
答案 1 :(得分: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();
}
});
}
}