jsplitpane里面不是有个onetouchexpandable属性吗,比方说加了个水平方向的jsplitpane,那么就有向上以及向下的按钮,我现在只想要一个向上的按钮该怎么写啊,请指教,多谢啦~

解决方案 »

  1.   

    你还是改SplitPane的UI吧,否则别无他法。
      

  2.   

    有没有哪位大哥有稍微详细点的代码啊,我正在重写,就是BasicSplitPaneDivider,我想改下里面的oneTouchExpandableChanged方法应该就行了,不过里面太多private的东西了,改起来遇到不少麻烦,所以想看看高手们怎么做的~
      

  3.   

    重写JSplitPane的UI,定义自己的Divider组件就可以了。
    在UI的方法里重写下面的方法:
    public BasicSplitPaneDivider createDefaultDivider() {
    return new MetalSplitPaneDivider(this);//这里返回
    }
      

  4.   

    我感觉应该是改这个方法oneTouchExpandableChanged(),
    protected void oneTouchExpandableChanged() {
            if (!DefaultLookup.getBoolean(splitPane, splitPaneUI,
                               "SplitPane.supportsOneTouchButtons", true)) {
                // Look and feel doesn't want to support one touch buttons, bail.
                return;
            }
            if (splitPane.isOneTouchExpandable() &&
                leftButton == null &&
                rightButton == null) {
                /* Create the left button and add an action listener to
                   expand/collapse it. */
                leftButton = createLeftOneTouchButton();
                if (leftButton != null)
                    leftButton.addActionListener(new OneTouchActionHandler(true));
                /* Create the right button and add an action listener to
                   expand/collapse it. */
                rightButton = createRightOneTouchButton();
                if (rightButton != null)
                    rightButton.addActionListener(new OneTouchActionHandler
        (false));            if (leftButton != null && rightButton != null) {
                    add(leftButton);
                    add(rightButton);//把这个注掉就好了
                }
            }
            revalidate();
        }
    这里不把rightbutton给add进去就好了,现在我的界面上的确只剩一个向上的按钮了,
    但响应不知道怎么加,
    原来有响应的代码,但leftButton.addActionListener(new OneTouchActionHandler(true));
    这一句里面的OneTouchActionHandler这东西是个private的,
    我没办法访问,照抄一把以后里面又有private的东西,这样总是把代码都copy进去也不是个事啊,像这种情况有没有啥比较好的办法啊,多谢啦~
      

  5.   

    createLeftOneTouchButton();你需要重写上面的方法,打到向上按钮的绘制效果。另外你可以添加一个remove方法就可以了啊,
    private JButton leftBt = null;private JButton rightBt = null;protected JButton createLeftOneTouchButton(){
    leftBt = new JButton(){;
    .....//这里是你的处理逻辑。
    };
    return leftBt;
    }protected JButton createRightOneTouchButton(){
    rightBt = new JButton(){;
    .....//这里是你的处理逻辑。
    };
    return rightBt;
    }private void removeRightBt(){
    if(rightBt == null){
    return;
    }
    remove(rightBt);//删除按钮
    }protected void oneTouchExpandableChanged() { 
    super.oneTouchExpandableChanged();
    this.removeRightBt();
    revalidate(); 
    }
      

  6.   

    光改Divider不行,divider中有调用SplitPaneUI的方法,即
        void setKeepHidden(boolean keepHidden) {
    this.keepHidden = keepHidden;
        }
    注意访问控制,你自己写的divider无法调用SplitPaneUI的这个方法,
    所以divider和SplitPaneUI要一起修改
      

  7.   

    多谢各位的解答,现在已经能只显示一个向上按钮了,
    就用7楼的那个办法就行,8楼的办法也是可以的,
    还要多谢9楼的提醒,的确要把SplitPaneUI也重写一下,否则事件的响应是加不进去的,
    不过有个初始化的问题不知道怎么解决,是这样的://这是我写的SplitPaneUI,因为有几个private的东西访问不到,只好copy一下了
    public class NewSplitPaneUI extends BasicSplitPaneUI{

    private boolean keepHidden = false;
        void setKeepHidden(boolean keepHidden) {
    this.keepHidden = keepHidden;
        }
    }
    //这是我写的divider
    public class NewSplitPaneDivider extends BasicSplitPaneDivider { private static final long serialVersionUID = 3963216987946515073L;

    protected NewSplitPaneUI splitPaneUI;
    //********************************************************
    //如9楼的兄弟所说,还是得写一个,我猜这里应该要初始化一下吧,该怎么初始化好呢
    //这东西在BasicSplitPaneDivider里面声明的时候就一句话
    //protected BasicSplitPaneUI splitPaneUI;
    //******************************************************** public NewSplitPaneDivider(BasicSplitPaneUI ui) {
    super(ui);
    }

    protected void oneTouchExpandableChanged() {
            if (!DefaultLookup.getBoolean(splitPane, splitPaneUI,
                               "SplitPane.supportsOneTouchButtons", true)) {
                return;
            }
            if (splitPane.isOneTouchExpandable() &&
                leftButton == null &&
                rightButton == null) {
                
                leftButton = createLeftOneTouchButton();
                if (leftButton != null)
                    leftButton.addActionListener(new OneTouchActionHandler(true));            rightButton = createRightOneTouchButton();            if (leftButton != null && rightButton != null) {
                    add(leftButton);
                    //add(rightButton);注掉这一句就能只显示一个向上的按钮了
                }
            }
            revalidate();
        }

    private void revalidate() {
            invalidate();
            if (splitPane != null) {
                splitPane.revalidate();
            }
        }




    //**************************************************************
    //OneTouchActionHandler  这个东西是个private的,只好copy过来
    //**************************************************************
        private class OneTouchActionHandler implements ActionListener { private boolean toMinimum; OneTouchActionHandler(boolean toMinimum) {
        this.toMinimum = toMinimum;
    }        public void actionPerformed(ActionEvent e) {
                Insets  insets = splitPane.getInsets();
        int     lastLoc = splitPane.getLastDividerLocation();
                int     currentLoc = splitPaneUI.getDividerLocation(splitPane);//*************************************************************************************
    //上面这一句会报错,空指针,原因应该是splitPaneUI初始化不对
    //*************************************************************************************
        int     newLoc;
        if (toMinimum) {
    if (orientation == JSplitPane.VERTICAL_SPLIT) {
        if (currentLoc >= (splitPane.getHeight() -
           insets.bottom - getHeight())) {
    int maxLoc = splitPane.getMaximumDividerLocation();
    newLoc = Math.min(lastLoc, maxLoc);
    splitPaneUI.setKeepHidden(false);
    add(leftButton);
    remove(rightButton);
                        }
        else {
    newLoc = insets.top;
    splitPaneUI.setKeepHidden(true);
    add(rightButton);
    remove(leftButton);
                        }
    }
    else {
        if (currentLoc >= (splitPane.getWidth() -
           insets.right - getWidth())) {
    int maxLoc = splitPane.getMaximumDividerLocation();
    newLoc = Math.min(lastLoc, maxLoc);
    splitPaneUI.setKeepHidden(false);
                        }
        else {
    newLoc = insets.left;
    splitPaneUI.setKeepHidden(true);
                        }
    }
        }
        else {
    if (orientation == JSplitPane.VERTICAL_SPLIT) {
        if (currentLoc == insets.top) {
    int maxLoc = splitPane.getMaximumDividerLocation();
    newLoc = Math.min(lastLoc, maxLoc);
    splitPaneUI.setKeepHidden(false);
                        }
        else {
    newLoc = splitPane.getHeight() - getHeight() -
             insets.top;
    splitPaneUI.setKeepHidden(true);
                        }
    }
    else {
        if (currentLoc == insets.left) {
    int maxLoc = splitPane.getMaximumDividerLocation();
    newLoc = Math.min(lastLoc, maxLoc);
    splitPaneUI.setKeepHidden(false);
                        }
        else {
    newLoc = splitPane.getWidth() - getWidth() - 
             insets.left;
    splitPaneUI.setKeepHidden(true);
        }
    }
        }
        if (currentLoc != newLoc) {
    splitPane.setDividerLocation(newLoc);
    splitPane.setLastDividerLocation(currentLoc);
        }
            }
        }
    }
      

  8.   

    在构造函数中添加: public NewSplitPaneDivider(BasicSplitPaneUI ui) {
    super(ui);
    this.splitPaneUI = (NewSplitPaneUI) ui;
    }
      

  9.   

    已经解决了,多谢各位,结贴,
    应有人要求,顺便把代码贴出来~
    //主函数中的声明和调用
    JSplitPane splitPane = new JSplitPane();
    splitPane.setUI(new NewSplitPaneUI());
    splitPane.disable();
    //NewSplitPaneUI.java
    import javax.swing.plaf.basic.BasicSplitPaneDivider;
    import javax.swing.plaf.basic.BasicSplitPaneUI;public class NewSplitPaneUI extends BasicSplitPaneUI{

    private boolean keepHidden = false; public NewSplitPaneUI() {
    super();
    }    void setKeepHidden(boolean keepHidden) {
    this.keepHidden = keepHidden;
        }
        
        public BasicSplitPaneDivider createDefaultDivider() 
        { 
    return new NewSplitPaneDivider( this ); 
        } 
    }
    //NewSplitPaneDivider.java
    import java.awt.Insets;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;import javax.swing.JSplitPane;
    import javax.swing.plaf.basic.BasicSplitPaneDivider;
    import javax.swing.plaf.basic.BasicSplitPaneUI;import sun.swing.DefaultLookup;public class NewSplitPaneDivider extends BasicSplitPaneDivider { private static final long serialVersionUID = 3963216987946515073L;

    protected NewSplitPaneUI splitPaneUI = new NewSplitPaneUI(); public NewSplitPaneDivider(BasicSplitPaneUI ui) {
    super(ui);
    }

    protected void oneTouchExpandableChanged() {
            if (!DefaultLookup.getBoolean(splitPane, splitPaneUI,
                               "SplitPane.supportsOneTouchButtons", true)) {
                // Look and feel doesn't want to support one touch buttons, bail.
                return;
            }
            if (splitPane.isOneTouchExpandable() &&
                leftButton == null &&
                rightButton == null) {
                /* Create the left button and add an action listener to
                   expand/collapse it. */
                leftButton = createLeftOneTouchButton();
                if (leftButton != null)
                    leftButton.addActionListener(new OneTouchActionHandler(true));
                /* Create the right button and add an action listener to
                   expand/collapse it. */
                rightButton = createRightOneTouchButton();
                if (rightButton != null)
                    rightButton.addActionListener(new OneTouchActionHandler
        (false));            if (leftButton != null && rightButton != null) {
                    add(leftButton);
    //add(rightButton);
                }
            }
            revalidate();
        }

    private void revalidate() {
            invalidate();
            if (splitPane != null) {
                splitPane.revalidate();
            }
        }





    /**
         * Listeners installed on the one touch expandable buttons.
         */
        private class OneTouchActionHandler implements ActionListener {
    /** True indicates the resize should go the minimum (top or left)
     * vs false which indicates the resize should go to the maximum.
     */
    private boolean toMinimum; OneTouchActionHandler(boolean toMinimum) {
        this.toMinimum = toMinimum;
    }        public void actionPerformed(ActionEvent e) {
                Insets  insets = splitPane.getInsets();
        int     lastLoc = splitPane.getLastDividerLocation();
                int     currentLoc = splitPane.getDividerLocation();//splitPaneUI.getDividerLocation(splitPane);
        int     newLoc;     // We use the location from the UI directly, as the location the
        // JSplitPane itself maintains is not necessarly correct.
        if (toMinimum) {
    if (orientation == JSplitPane.VERTICAL_SPLIT) {
        if (currentLoc >= (splitPane.getHeight() -
           insets.bottom - getHeight())) {
    int maxLoc = splitPane.getMaximumDividerLocation();
    newLoc = Math.min(lastLoc, maxLoc);
    splitPaneUI.setKeepHidden(false);
    add(leftButton);
    remove(rightButton);
                        }
        else {
    newLoc = insets.top;
    splitPaneUI.setKeepHidden(true);
    add(rightButton);
    remove(leftButton);
                        }
    }
    else {
        if (currentLoc >= (splitPane.getWidth() -
           insets.right - getWidth())) {
    int maxLoc = splitPane.getMaximumDividerLocation();
    newLoc = Math.min(lastLoc, maxLoc);
    splitPaneUI.setKeepHidden(false);
                        }
        else {
    newLoc = insets.left;
    splitPaneUI.setKeepHidden(true);
                        }
    }
        }
        else {
    if (orientation == JSplitPane.VERTICAL_SPLIT) {
        if (currentLoc == insets.top) {
    int maxLoc = splitPane.getMaximumDividerLocation();
    newLoc = Math.min(lastLoc, maxLoc);
    splitPaneUI.setKeepHidden(false);
    add(leftButton);
    remove(rightButton);
                        }
        else {
    newLoc = splitPane.getHeight() - getHeight() -
             insets.top;
    splitPaneUI.setKeepHidden(true);
    add(rightButton);
    remove(leftButton);
                        }
    }
    else {
        if (currentLoc == insets.left) {
    int maxLoc = splitPane.getMaximumDividerLocation();
    newLoc = Math.min(lastLoc, maxLoc);
    splitPaneUI.setKeepHidden(false);
                        }
        else {
    newLoc = splitPane.getWidth() - getWidth() - 
             insets.left;
    splitPaneUI.setKeepHidden(true);
        }
    }
        }
        if (currentLoc != newLoc) {
    splitPane.setDividerLocation(newLoc);
    // We do this in case the dividers notion of the location
    // differs from the real location.
    splitPane.setLastDividerLocation(currentLoc);
        }
            }
        }
    }