我覺得你的問題是,你可以添加該組件的兩倍(即可以真正使想看起來很奇怪)。例如,您可以執行如下操作: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);
}
正是......此代碼: GUI6.gui6.jSplitPane1.setRightComponent(null); GUI6.gui6.jSplitPane1.setLeftComponent(null); 是問題的關鍵。我嘗試過方法removeAll()之前。但這是最好的方法。非常感謝 ! – sajad 2011-02-02 08:20:35