zoukankan      html  css  js  c++  java
  • Swing控件JSplitPane的使用(常用方法说明)

     1、swing分割窗口控件JSplitPane,用来将窗口分割成两个部分。

     2、分割后的窗口每个窗口只能放一个控件,想要方多个控件的话,可以在上面方一个JPane面板,这样就可以方多个控件。

     3、JSplitPane myJSplitPane=new JSplitPane(int newOrientaion,boolean new ContinuousLayout);

             newOrientaion:可选值为:JSplitPane.HORIZONTAL_SPLIT

                                             JSplitPane,VERTUCAL_SPLIT

       4、常用方法

           ①、setDividerSize(int size)设置分割条的大小。

           ②、getDividerSize()得到分割条的大小。

           ③、setDividerLocation(int size)按照百分比设置分割条的位置。

           ④、getOrientation获得方向。

      5、使用 JSplitPane 监听窗口缩放事件

    JSplitPane splitPane = new javax.swing.JSplitPane();     
             splitPane.addPropertyChangeListener(new java.beans.PropertyChangeListener() {     
                 public void propertyChange(java.beans.PropertyChangeEvent evt) {     
                     if (evt.getPropertyName().equals(JSplitPane.DIVIDER_LOCATION_PROPERTY)) {     
                         //action code     
                     }     
                 }     
             });  
    

       6、JSplitPane按比例分割的问题

    JSplitPane看似比Delphi的spliter
    难用许多。不过介于swing可以方便的使用记事本一类文本编辑器直接书写依据布局的界面代码我们还是姑且容忍它带来的不便。但在使用
    JSplitPane时候在MSDN上被问的比较频繁却没有良好答案的问题是JSplitPane如何按比例分
    (http://www.my400800.cn)割,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都会按比例分割。
     
  • 相关阅读:
    poj 3666 Making the Grade
    poj 3186 Treats for the Cows (区间dp)
    hdu 1074 Doing Homework(状压)
    CodeForces 489C Given Length and Sum of Digits...
    CodeForces 163A Substring and Subsequence
    CodeForces 366C Dima and Salad
    CodeForces 180C Letter
    CodeForces
    hdu 2859 Phalanx
    socket接收大数据流
  • 原文地址:https://www.cnblogs.com/jishu/p/1992270.html
Copyright © 2011-2022 走看看