错误或功能:Swing默认gui字体Win6 +不正确 [英] Bug or feature: Swing default gui font incorrect for Win6+

查看:237
本文介绍了错误或功能:Swing默认gui字体Win6 +不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(令人惊讶的是;-)注意到应用程序在我的win6 +机器上看起来如此拥挤的原因(与Vista和Win7相同,都是120dpi设置,jdk6和jdk7):从桌面属性查找的控制字体同时具有错误的字体系列和错误的大小:

pre $ public static void main(String [] args){
Font guiFont =(Font)Toolkit.getDefaultToolkit()。getDesktopProperty(win.defaultGUI.font);
int guiSize = guiFont.getSize();
Font iconFont =(Font)Toolkit.getDefaultToolkit()。getDesktopProperty(win.icon.font);
System.out.println(gui default:+ guiFont +\ nicon default:+ iconFont);

$ / code $ / pre
$ b $输出:

<$
图标默认值:java.awt.Font [family = gui默认值:java.awt.Font [family = Tahoma,name = Tahoma,style = plain,size = Segoe UI,name = Segoe UI,style = plain,size = 15]

后者用于几乎所有文本的原生应用程序,而Swing使用前... ...

问题:

$ ul

  • 是否有任何理由,或只是一个bug?
  • 谁负责:Swing查找(从相关系统资源读入desktopProperty时)或操作系统没有正确报告?

  • 如何强制使用后者?


    解决最后一个问题:


    • 完全控制LAF的人可能会考虑设置所有相关的文本字体(这是JGoodies所做的, FontPolicy /套)。

    • 一个肮脏的黑客行为是将defaultGUI桌面属性的值设置为正确的值 - 它涉及对工具箱的反射访问,这自然会影响到安全约束的上下文。



    • $ b

      编辑

      为了防止任何人感兴趣,这里有一个肮脏的黑客攻击:

      pre code $ / b
      替换如果前者较小,则默认的gui桌面字体属性为图标字体
      *。

      *
      public static void ensureDefaultGUIFontSize(){
      Toolkit toolkit = Toolkit.getDefaultToolkit();
      Font guiFont =(Font)toolkit.getDesktopProperty(win.defaultGUI.font);
      字体iconFont =(字体)toolkit.getDesktopProperty(win.icon.font);
      if(guiFont.getSize()< iconFont.getSize()){
      invokeDeclaredMethod(setDesktopProperty,Toolkit.class,
      toolkit,win.defaultGUI.font,iconFont) ;



      private static void invokeDeclaredMethod(String methodName,
      Class<?> clazz,Object instance,String propertyName,
      Object propertyValue) {
      try {
      方法method = clazz.getDeclaredMethod(methodName,String.class,Object.class);
      method.setAccessible(true);
      method.invoke(instance,propertyName,propertyValue);
      catch(NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e){
      LOG.finer(强制桌面属性失败+ e.getStackTrace());


      $ b $ / code $ / pre
      $ b $编辑2 /只是为了澄清:黑客是完全有效的只有WindowsLAF。 Nimbus完全忽略系统设置,Metal部分:后者的字体始终是Dialog,只有大小取自desktopProperties。听起来不错,但不是:对于主要字体f.i,映射相当怪异。大量使用的controlFont大小设置为win.ansiVar.font.height(这是什么化石剩余的?),这是我的机器上的13 ...

      <编辑3



      即使在windows ui中,黑客攻击也是有限的,那些在@ Walter评论中提到的:


      扩展Windows UI时,此错误尤为明显。仅供参考,打开一个JFileChooser将恢复黑客行为。此外,JTree / JTable行高不会自动更新为新的字体大小,并且还需要缩放图标。


      解决方案

      我认为这不是一个bug,但基本属性的Win7和built_in主题,有趣的字体大小,我仍然使用较小的字体(默认设置从操作系统instalation)
      $ b

      例如,如果我设置/切换

      1.Windows7基本主题

      <$
      图标默认值:java.awt.Font [family = gui默认值:java.awt.Font [系列= Tahoma,name = Tahoma,style = plain,size = Segoe UI,name = Segoe UI,style = plain,size = 12]



      2.Windows7 Classic主题

        gui default:java.awt.Font [family = Tahoma,name = Tahoma,style = plain,size = 11] 
      图标默认值:java.awt.Font [family = Tahoma,name = Tahoma,style = plain,size = 11]

      未触及Font属性,将从WinXP继续



      <3> wXP修改主题

        gui default:java.awt.Font [family = Tahoma,name = Tahoma,style = plain,size = 11] 
      图标默认值:java.awt.Font [family = Tahoma,name = Tahoma,style = plain,size = 13]

      4.Windows7经典主题
      $ b $

        gui默认值:java.awt.Font [family = Tahoma,name = Tahoma,style = plain,size = 11] 
      icon默认:java.awt.Font [family = Tahoma,name = Tahoma,style = plain,size = 11]


      just (astonishingly ;-) noticed the reason why apps look so cramped on my win6+ machines (same for Vista and Win7, both with 120dpi setting, jdk6 and jdk7): the control font looked up from the the desktop property has both the wrong font family and the wrong size:

      public static void main(String[] args) {
          Font guiFont = (Font) Toolkit.getDefaultToolkit().getDesktopProperty("win.defaultGUI.font");
          int guiSize = guiFont.getSize();
          Font iconFont = (Font) Toolkit.getDefaultToolkit().getDesktopProperty("win.icon.font");
          System.out.println("gui default: " + guiFont + "\nicon default: " + iconFont);
      }
      

      output:

      gui default: java.awt.Font[family=Tahoma,name=Tahoma,style=plain,size=13]
      icon default: java.awt.Font[family=Segoe UI,name=Segoe UI,style=plain,size=15] 
      

      The latter is used in native applications for nearly all text, while Swing uses the former ...

      Questions:

      • Could there be any reason for this, or just a bug?
      • Who's responsible: the Swing lookup (when reading-in the desktopProperty from relevant system resources) or the OS in not reporting it correctly?
      • How to force the usage of the latter?

      Options for solving the last:

      • With full control about the LAF one might consider to set all relevant text fonts (that's what JGoodies does, factored into a FontPolicy/Set).
      • A dirty hack is to set the value of the defaultGUI desktop property to the correct value - it involves reflective access to the toolkit, which naturally will blow in security constrained contexts.
      • ??

      Edit

      Just in case anybody is interested, here's the dirty hack:

      /**
       * Replaces the default gui desktop font property with the icon font
       * if the former is smaller.
       * 
       */
      public static void ensureDefaultGUIFontSize() {
          Toolkit toolkit = Toolkit.getDefaultToolkit();
          Font guiFont = (Font) toolkit.getDesktopProperty("win.defaultGUI.font");
          Font iconFont = (Font) toolkit.getDesktopProperty("win.icon.font");
          if (guiFont.getSize() < iconFont.getSize()) {
              invokeDeclaredMethod("setDesktopProperty", Toolkit.class, 
                  toolkit, "win.defaultGUI.font", iconFont);
          }
      }
      
      private static void invokeDeclaredMethod(String methodName,
              Class<?> clazz, Object instance, String propertyName,
              Object propertyValue) {
          try {
              Method method = clazz.getDeclaredMethod(methodName, String.class, Object.class);
              method.setAccessible(true);
              method.invoke(instance, propertyName, propertyValue);
          } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
              LOG.finer("forcing desktop property failed " + e.getStackTrace());
          }
      
      }
      

      Edit 2

      Just to clarify: the hack is fully effective only for WindowsLAF. Nimbus ignores system settings completely, Metal partly: the latter's font is always Dialog, only size is taken from desktopProperties. Sounds half-way good, but isn't: the mapping is rather weird for the main fonts, f.i. the heavily used controlFont size is set to "win.ansiVar.font.height" (what fossil leftover is that?) which is 13 on my machine ...

      Edit 3

      Even in windows ui, the hack is ... a hack with limitations, f.i. those mentioned in @Walter's comment:

      This bug is especially noticeable when you scale the Windows UI. FYI, opening a JFileChooser reverts the hack. Also JTree/JTable row height will not be automatically updated to the new font size and you'll need to scale your icons as well

      解决方案

      I think that isn't a bug but basic property of Win7 and built_in themes, interesting size of Font, I still use smaller Fonts (default setting from OS instalation)

      for example if I set / switch

      1.Windows7 Basic theme

      gui default: java.awt.Font[family=Tahoma,name=Tahoma,style=plain,size=11]
      icon default: java.awt.Font[family=Segoe UI,name=Segoe UI,style=plain,size=12]
      

      2.Windows7 Classic theme

      gui default: java.awt.Font[family=Tahoma,name=Tahoma,style=plain,size=11]
      icon default: java.awt.Font[family=Tahoma,name=Tahoma,style=plain,size=11]
      
      don't touched the Font property, will be continue for from WinXP
      

      3.WindowXP modified theme

      gui default: java.awt.Font[family=Tahoma,name=Tahoma,style=plain,size=11]
      icon default: java.awt.Font[family=Tahoma,name=Tahoma,style=plain,size=13]
      

      4.Windows7 Classic theme

      gui default: java.awt.Font[family=Tahoma,name=Tahoma,style=plain,size=11]
      icon default: java.awt.Font[family=Tahoma,name=Tahoma,style=plain,size=11]
      

      这篇关于错误或功能:Swing默认gui字体Win6 +不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆