zoukankan      html  css  js  c++  java
  • java swing 学习






    JSplitPane固定分割比例和禁止拖动分割条

    有知友问JSplitPane的问题,在写代码的时候不想让分割条拖动,结果找不到方法,百度了 居然也找不到...

    后来在一个犄角旮旯里发现了 ,,  就写上来让大家看看吧...  

     

    import java.awt.event.ComponentAdapter;

    import java.awt.event.ComponentEvent;

     

    import javax.swing.JFrame;

    import javax.swing.JSplitPane;

     

    public class Test {

      public static void main(String[] args) {

        JFrame frame=new JFrame("Test");

        final JSplitPane jsp=new JSplitPane();

        frame.add(jsp);

        frame.setLocation(200,200);

        frame.setSize(400,400);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        jsp.setEnabled(false);//进制拖动分割条

        frame.setVisible(true);

        frame.addComponentListener(new ComponentAdapter(){

          public void componentResized(ComponentEvent e) {

                       jsp.setDividerLocation(0.67);

                 }

        });

        jsp.setDividerLocation(0.67);//设置分割比例,注意必须在frame.setVisible(true);之后才有效..  理由看下边..

      }

    }

    理由:

    setDividerLocationpublic 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)才会有效。

    Java代码  收藏代码
    1. 1.设置窗口最大(伪最大化)  
    2. JFrame frame =new JFrame();         
    3. frame.setSize(Toolkit.getDefaultToolkit().getScreenSize());   
    4. frame.setLocation(0,0);         
    5. frame.show();   
    6.   
    7. 2.设置最大化(JDK1.4以上)         
    8. JFrame frame =new JFrame();          
    9. frame.show();         
    10. frame.setExtendedState(JFrame.MAXIMIZED_BOTH);  
    11.   
    12. 3.设置全屏模式  
    13. GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(frame);   
    14.    

  • 相关阅读:
    signal(SIGCHLD, SIG_IGN)和signal(SIGPIPE, SIG_IGN);
    关于pthread_cond_wait使用while循环判断的理解
    linux的sleep()和usleep()的使用和区别
    C中结构体的存储分配
    扯扯python的多线程的同步锁 Lock RLock Semaphore Event Condition
    线程属性的初始化以及销毁
    Mysql数据库一个表字段中存了id,并以逗号分隔,id对应的详细信息在另一个表中
    sqlyog 注册码
    Oracle 12C卸载图文教程
    Oracle12c Release1安装图解(详解)
  • 原文地址:https://www.cnblogs.com/daichangya/p/12959349.html
Copyright © 2011-2022 走看看