我想有一个jsplitPane和交换右组件与左组件,同时运行我的程序。我将分割位置设置为0.2左右。当我交换左组件和右组件并将分区位置设置为0.8左右时,jSplitPane出现了问题。它被锁住了,我不能移动除数。同样在那之后,当我尝试将另一个组件分配给JSplitPane的右侧或左侧时,这些组件显示为粗体。在交换左右组件之前,我尝试过使用setDivisionLocation()方法,但没有效果。还有repaint()方法....请给我指引
regards...sajad
发布于 2011-02-02 16:00:27
我认为你的问题是你添加了两次组件(这真的会让think看起来很奇怪)。例如,你可以这样做: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);
}https://stackoverflow.com/questions/4871874
复制相似问题