簡體   English   中英

如何在Swing GTK LookAndFeel中更改默認字體大小?

[英]How to change the default font size in the Swing GTK LookAndFeel?

有沒有辦法在Swing GTK LaF中更改默認字體大小?

GTK LaF似乎假定為72dpi,因此所有字體只有使用96dpi屏幕時應該是的大小的3/4。 有關詳細信息,請參閱此Fedora錯誤 我想在此期間找到一個解決方法,同時我等待修復。

我已經嘗試過通過UIDefaults重置字體大小,例如, 這里推薦的,但是(如前所述)GTK LaF似乎忽略了這一點。

可以構建一個小部件工廠,它也可以設置所需的字體大小來創建我的所有Swing小部件,但這將是大規模侵入性的,所以如果有任何其他方式,我想避免使用該路徑。

編輯:以下不起作用:

public class GTKLaF extends com.sun.java.swing.plaf.gtk.GTKLookAndFeel {
  @Override
  public UIDefaults getDefaults() {
    final float scale = 3f;

    final UIDefaults defaults = super.getDefaults(); 

    final Map<Object,Object> changes = new HashMap<Object,Object>();

    for (Map.Entry<Object,Object> e : defaults.entrySet()) {
      final Object key = e.getKey();
      final Object val = e.getValue();

      if (val instanceof FontUIResource) {
        final FontUIResource ores = (FontUIResource) val;
        final FontUIResource nres =
          new FontUIResource(ores.deriveFont(ores.getSize2D()*scale));
        changes.put(key, nres);
        System.out.println(key + " = " + nres);
      } 
      else if (val instanceof Font) {
        final Font ofont = (Font) val;
        final Font nfont = ofont.deriveFont(ofont.getSize2D()*scale);
        changes.put(key, nfont);
        System.out.println(key + " = " + nfont);
      }
    }

    defaults.putAll(changes);

    return defaults;
  }
}

您可能認為這會打印至少十二個鍵值對,但它只打印一個:TitledBorder.font。 顯然其他字體屬性不是由GTLLookAndFeel提供的,而是來自其他地方!

我的建議是創建自己的GTKLookAndFeel子類並使用它。

在你的子類中,我可能會覆蓋getDefaults() 在你的getDefaults()你可以得到GTKLookAndFeel要返回的UIDefaults並在必要時調整它們。 (通過包裝或子類化它們並覆蓋UIDefaults.getFont方法。)

獲取當前的DPI

int dpi = Toolkit.getDefaultToolkit().getScreenResolution();

如果它不是72,你可以相應地改變字體大小。

編輯:您發布的鏈接不起作用,我相信因為GTKLookAndFeel會覆蓋所涉及的方法,並且不會讓其祖先的任何默認值通過。 反過來覆蓋它應該可以讓你繞過這個問題。

似乎無法設置默認字體大小。 但是,問題中引用的錯誤已經修復,因此現在需要這樣做是沒有實際意義的。

我失去了很多時間來弄清楚如何讓我們的應用程序在ubuntu上體面,我來到這個黑客。 我試過像某人建議擴展GTKLookAndFeel,但因為它使用了許多對GTKEngine的本地調用,我意識到它不可行。 我希望這有幫助

在設置外觀之后調用我的hack checkGTKLookAndFeel()它會降低兩點的字體標准:

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
try {
    InvoicexUtil.checkGTKLookAndFeel();
} catch (Exception e) {
    e.printStackTrace();
}

public static void checkGTKLookAndFeel() throws Exception {
    LookAndFeel look = UIManager.getLookAndFeel();
    if (!look.getID().equals("GTK")) return;

    new JFrame();
    new JButton();
    new JComboBox();
    new JRadioButton();
    new JCheckBox();
    new JTextArea();
    new JTextField();
    new JTable();
    new JToggleButton();
    new JSpinner();
    new JSlider();
    new JTabbedPane();
    new JMenu();
    new JMenuBar();
    new JMenuItem();

    Object styleFactory;
    Field styleFactoryField = look.getClass().getDeclaredField("styleFactory");
    styleFactoryField.setAccessible(true);
    styleFactory = styleFactoryField.get(look);

    Field defaultFontField = styleFactory.getClass().getDeclaredField("defaultFont");
    defaultFontField.setAccessible(true);
    Font defaultFont = (Font) defaultFontField.get(styleFactory);
    FontUIResource newFontUI;
    newFontUI = new FontUIResource(defaultFont.deriveFont((float)(defaultFont.getSize() - 2f)));
    defaultFontField.set(styleFactory, newFontUI);

    Field stylesCacheField = styleFactory.getClass().getDeclaredField("stylesCache");
    stylesCacheField.setAccessible(true);
    Object stylesCache = stylesCacheField.get(styleFactory);
    Map stylesMap = (Map) stylesCache;
    for (Object mo : stylesMap.values()) {
        Field f = mo.getClass().getDeclaredField("font");
        f.setAccessible(true);
        Font fo = (Font)f.get(mo);
        f.set(mo, fo.deriveFont((float)(fo.getSize() - 2f)));
    }
}

調用各種swing組件構造函數來初始化GTK樣式。 不是很優雅,但它的工作原理

您鏈接的代碼有問題。 UIDefaults.get(key)函數不必返回Font實例。 它可能是一個javax.swing.plaf.FontUIResource實例。 下面的代碼片段找到所有已安裝的外觀,不是改變所有的字體大小,其比例scale

float scale=1.1f;
UIManager.LookAndFeelInfo looks[] = UIManager.getInstalledLookAndFeels();

for (UIManager.LookAndFeelInfo info : looks) {

    //if you want to change LaF to a spesific LaF,such as "GTK"
    //put here a if statement like:
    //if(info.getClassName().contains("GTK"))
    UIManager.setLookAndFeel(info.getClassName());

    UIDefaults defaults = UIManager.getDefaults();
    Enumeration newKeys = defaults.keys();

    while (newKeys.hasMoreElements()) {
        Object obj = newKeys.nextElement();
        Object current = UIManager.get(obj);
        if (current instanceof FontUIResource) {
            FontUIResource resource = (FontUIResource) current;
            defaults.put(obj, new FontUIResource(resource.deriveFont(resource.getSize2D()*scale)));
            // System.out.printf("%50s : %s\n", obj,  UIManager.get(obj));
        } else if (current instanceof Font) {
            Font resource = (Font) current;
            defaults.put(obj, resource.deriveFont(resource.getSize2D()*scale));
            // System.out.printf("%50s : %s\n", obj,  UIManager.get(obj));
        }
    }
}

我找到了關於GTK LaF問題的一本書,一本堅果殼中的Java例子,第三版。 看看這里

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM