if(ui==null),这行,你确定ui就一定是null吗?

解决方案 »

  1.   

    注意
    target.getUIClassID()
    这句话
      

  2.   

    每一个JComponent都有一个UIClassID和它对应。
    这个UIClassID就是对应的....太晚了,必须走人了
    明天在说吧
      

  3.   

    例如JButton对应的就是ButtonUI
    而在WindowsLookAndFeel类里边定义如下:
        /** 
         * Initialize the uiClassID to BasicComponentUI mapping.
         * The JComponent classes define their own uiClassID constants
         * (see AbstractComponent.getUIClassID).  This table must
         * map those constants to a BasicComponentUI class of the
         * appropriate type.
         * 
         * @see BasicLookAndFeel#getDefaults
         */
        protected void initClassDefaults(UIDefaults table)
        {
            super.initClassDefaults(table);
            String windowsPackageName = "com.sun.java.swing.plaf.windows.";
            Object[] uiDefaults = {
                       "ButtonUI", windowsPackageName + "WindowsButtonUI",
                     "CheckBoxUI", windowsPackageName + "WindowsCheckBoxUI",
                  "RadioButtonUI", windowsPackageName + "WindowsRadioButtonUI",
                 "ToggleButtonUI", windowsPackageName + "WindowsToggleButtonUI",等等,也就是说默认的ButtonUI在windows下对应com.sun.java.swing.plaf.windows.WindowsButtonUI类,
    在你上边拷贝的这一段程序中:
    “ if(ui==null)
            ui=getDefaults().getUI(target);
    }显然,调用了UIDefaults的getUI函数,”这里你没有说为什么显然。
    而lookandfeel的获取就是在你这个显然里边
    UiManager的getDefault就是返回当前程序中默认的L&F所以整个流程是
    组件的updateUi->UiManager的getDefault得到对应的lookandfeel的UiDefault->根据UIDefault的table表得到对应的平台的LookAndFeel的classID->在通过UIdefault中的反射生成对应的UI对象