我想要一个 jsplitPane 并在运行我的程序时用左组件交换右组件。我将划分位置设置为 0.2 左右。当我交换左分量和右分量并将分割位置设置为 0.8 时; jSplitPane 有问题。它被锁定,我无法移动除数。也是在那之后;当我尝试将另一个组件分配到 JSplitPane 的右侧或左侧时,这些组件出现了 bollixed。在交换左右组件之前,我尝试了 setDivisionLocation() 方法;但它没有效果。还有 repaint() 方法.... 请指导我
问候...sajad
最佳答案
我认为你的问题是你添加了两次组件(这真的会让想法看起来很奇怪)。例如,您执行如下操作:split.setLeftComponent(split.getRightComponent())
。
因此,当您进行交换时,您需要先移除组件:
private static void swap(JSplitPane split) {
Component r = split.getRightComponent();
Component l = split.getLeftComponent();
// remove the components
split.setLeftComponent(null);
split.setRightComponent(null);
// add them swapped
split.setLeftComponent(r);
split.setRightComponent(l);
}
演示在这里(也移动了分隔线位置):
public static void main(String[] args) {
JFrame frame = new JFrame("Test");
final JSplitPane split = new JSplitPane(
JSplitPane.HORIZONTAL_SPLIT,
new JLabel("first"),
new JLabel("second"));
frame.add(split, BorderLayout.CENTER);
frame.add(new JButton(new AbstractAction("Swap") {
@Override
public void actionPerformed(ActionEvent e) {
// get the state of the devider
int location = split.getDividerLocation();
// do the swap
swap(split);
// update the devider
split.setDividerLocation(split.getWidth() - location
- split.getDividerSize());
}
}), BorderLayout.SOUTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
frame.setVisible(true);
}
关于java: JSplitpane 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4871874/