2011-02-02 39 views
1

我想在运行我的程序时通过左侧组件有一个jsplitPane和交换右侧组件。我设置了分割位置约0.2。当我将左边的分量和右边的分量交换并设置分割位置大约为0.8时; jSplitPane存在问题。它被锁定,我不能移除除数。在那之后;当我尝试将另一个组件分配给JSplitPane的右侧或左侧时,组件显示为bollixed。在交换右侧和左侧组件之前,我尝试了setDivisionLocation()方法;但它不是有效的。并重绘()方法.... 请指导我java:问题与JSplitpane

问候......萨贾德

回答

2

我觉得你的问题是,你可以添加该组件的两倍(即可以真正使想看起来很奇怪)。例如,您可以执行如下操作: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); 
} 

而且演示是在这里(也移动分隔条的位置):

before after

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); 
} 
+0

正是......此代码: GUI6.gui6.jSplitPane1.setRightComponent(null); GUI6.gui6.jSplitPane1.setLeftComponent(null); 是问题的关键。我尝试过方法removeAll()之前。但这是最好的方法。非常感谢 ! – sajad 2011-02-02 08:20:35