好像缺省左右兩邊有Jbutton,但我想在setrightcomponent 前先清除右邊,然後再加上別的,但我調用setrightcomponent(null)或者remove方法,分割條自動移動了,我不想讓他自動移動,如何做到呢?

解决方案 »

  1.   

    JSplitPane看似比Delphi的spliter难用许多。不过介于swing可以方便的使用记事本一类文本编辑器直接书写依据布局的界面代码我们还是姑且容忍它带来的不便。但在使用JSplitPane时候在MSDN上被问的比较频繁却没有良好答案的问题是JSplitPane如何按比例分割,setDividerLocation(double d)为什么没有作用。要解决这个问题首先看JAVA DOC.关于setDividerLocation的介绍是这样的:setDividerLocation
    public void setDividerLocation(double proportionalLocation)设置分隔条的位置为 JSplitPane 大小的一个百分比。 
    根据 setDividerLocation(int) 来实现此方法。此方法以分隔窗格的当前大小为基础迅速改变窗格的大小。如果分隔窗格没有正确地实现并且不显示在屏幕上,此方法将不产生任何影响(新的分隔条位置将成为 0(当前的 size * proportionalLocation ))。 参数: 
    proportionalLocation - 指示百分比的双精度浮点值,从 0 (top/left) 到 1.0 (bottom/right) 
    抛出: 
    IllegalArgumentException - 如果指定的位置为 < 0 or > 1.0 
    看完后没什么概念只觉得写的不是那么直白,也许确有什么猫腻在里边。特别是"如果分隔窗格没有正确地实现并且不显示在屏幕上,此方法将不产生任何影响"这句,没大理解因而去看看JSplitPane的源码。关于setDividerLocation大致如下:    public void setDividerLocation(double proportionalLocation) {
            if (proportionalLocation < 0.0 || 
               proportionalLocation > 1.0) {
                throw new IllegalArgumentException("proportional location must " +
                                                   "be between 0.0 and 1.0.");
            }
            if (getOrientation() == VERTICAL_SPLIT) {
                setDividerLocation((int)((double)(getHeight() - getDividerSize()) *
                                         proportionalLocation));
            } else {
                setDividerLocation((int)((double)(getWidth() - getDividerSize()) *
                                         proportionalLocation));
            }
        }这下有些明白了,setDividerLocation(double)这个函数会用到getWidth()或者getHeight()这样的函数,而java桌面程序在没有主窗体setVisible之前,如果使用布局,尚未validate()和paint()每个组件的宽和高默认都是0。也就是说一定要在主窗体setVisible(true)之后再使用setDividerLocation(double)才会有效。下边给出一个例子:package tlw.zbe.info.stratch;import javax.swing.JFrame;
    import javax.swing.JSplitPane;
    import java.awt.BorderLayout;
    import javax.swing.JPanel;public class MainFrame extends JFrame {
        public static void main(String[] args){
            MainFrame f=new MainFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设定窗体关闭后自动退出进程
            f.setSize(800,600);//设定窗体的默认尺寸
            f.setExtendedState(JFrame.MAXIMIZED_BOTH);//设定窗体状态为屏幕最大化,即全屏尺寸。
            f.setVisible(true);//显示窗体
            f.jSplitPane1.setDividerLocation(0.7);//设定分割面板的左右比例(这时候就生效了,如果放在setVisible(true)这据之前就不会有效果。)
        }
        public MainFrame() {
            try {
                jbInit();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }    private void jbInit() throws Exception {
            this.getContentPane().add(jSplitPane1, java.awt.BorderLayout.CENTER);
            jSplitPane1.add(jPanel1, JSplitPane.LEFT);
            jSplitPane1.add(jPanel2, JSplitPane.RIGHT);
        }    JSplitPane jSplitPane1 = new JSplitPane();
        JPanel jPanel1 = new JPanel();
        JPanel jPanel2 = new JPanel();
    }很好看出是JBuilder的界面代码,运行它会发现JSplitPane已经乖乖的按照比例分割。问题是当拖动split后界面做一个最大化后比例不能维持。解决这个问题就是加一个ComponentListener。例如下:package tlw.zbe.info.stratch;import javax.swing.JFrame;
    import javax.swing.JSplitPane;
    import java.awt.BorderLayout;
    import javax.swing.JPanel;
    import java.awt.event.ComponentEvent;
    import java.awt.event.ComponentAdapter;public class MainFrame extends JFrame {
        public static void main(String[] args){
            MainFrame f=new MainFrame();
        }
        private void myInit(){
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设定窗体关闭后自动退出进程
            setSize(800,600);//设定窗体的默认尺寸
            setVisible(true);//显示窗体
            jSplitPane1.setDividerLocation(0.7);//设定分割面板的左右比例(这时候就生效了,如果放在setVisible(true)这据之前就不会有效果。)
            /*****初始化事件***/
            this.addComponentListener(new ComponentAdapter(){
                public void componentResized(ComponentEvent e) {
                    jSplitPane1.setDividerLocation(0.7);
                }
            });
        }
        public MainFrame() {
            try {
                jbInit();
                myInit();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }    private void jbInit() throws Exception {
            this.getContentPane().add(jSplitPane1, java.awt.BorderLayout.CENTER);
            jSplitPane1.add(jPanel1, JSplitPane.LEFT);
            jSplitPane1.add(jPanel2, JSplitPane.RIGHT);
        }    JSplitPane jSplitPane1 = new JSplitPane();
        JPanel jPanel1 = new JPanel();
        JPanel jPanel2 = new JPanel();
    }
    看到有个myInit()方法?这是我个人的习惯,自己写的界面代码区为了分于生成的都放在myInit()下边。一般来说里边初始化一些界面默认值和界面事件。添加的ComponentListener决定了任意改变界面尺寸后JSplitPane都会按比例分割。