簡體   English   中英

設置Nimbus LookandFeel后,Java Swing JOptionPane是和否按鈕的字體大小不同

[英]Java Swing JOptionPane Yes and No Button differ in font size after setting Nimbus LookandFeel

由於在默認的LookAndFeel中字體大小太小,我使用幾行代碼將其設置為Nimbus ,而我的JOptionPane顯示具有不同大小的YesNo按鈕。 仍然非常小,而“ 否”設置為我指定的大小。 有誰知道為什么或如何解決它?

public static void setUIFont(Font a){
    FontUIResource ax=new FontUIResource(a);
    javax.swing.UIManager.put("OptionPane.messageFont", ax);
    javax.swing.UIManager.put("OptionPane.buttonFont", ax);
    javax.swing.UIManager.put("OptionPane.Font", ax);
    javax.swing.UIManager.put("InternalFrame.titleFont", ax);
    javax.swing.UIManager.put("TextField.font", ax);
    javax.swing.UIManager.put("ComboBox.font", ax);
}

...

UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"); 

class UseNimBus extends SwingInvoker{
    public void run(){
        JDialog.setDefaultLookAndFeelDecorated(true);
        setUIFont(new FontUIResource(new Font(ufont, Font.PLAIN, 20)));
    }
}
(new UseNimBus()).execute();// just invokeLater()

以下行顯示選項窗格,但它具有“ 是”和“ 否”,具有不同的大小。 這是正常還是只是我的問題?

inputValuex=JOptionPane.showConfirmDialog(
    myWin, "Are you exiting?", "You clicked X", JOptionPane.YES_NO_OPTION);

更新

還是行不通。 我試過使用代碼

javax.swing.UIManager.put("OptionPane.isYesLast", true);

更改按鈕的位置,但它沒有任何效果。 我只是想看看如何設置這些值,如布爾值。

另外,我甚至在UIManager.getDefaults()列出了包含optionpanebutton的所有 ,並且它們的字體大小都設置為20. Yes按鈕仍然是較小的字體。

JButton的字體實際上來自Button.font屬性。

如果我將其添加到您的屬性列表中,它會起作用。

javax.swing.UIManager.put("Button.font", ax);

為什么你會得到兩種不同的尺寸,雖然很有趣,但此時此刻已超出我的范圍。

看起來沒有為optionPane中創建的第一個按鈕獲取自定義字體的原因是它被設為rootPane的defaultButton。 因此,它有一個特殊的狀態[Default],用於查找屬性 - 如果按鈕的設置字體是UIResource類型。

那么會發生什么

  • OptionPaneUI將其創建的所有按鈕的字體設置為它在UIManager中為“OptionPane.buttonFont”找到的字體
  • SynthButtonUI忽略狀態[Default]的按鈕字體,因為它是UIResource

由於某種原因,我不知道(但想想要記住Nimbus中的命名/子組件的查找被破壞),下面的設置都沒有任何效果

UIManager.put("\"OptionPane.button\"[Default].font", ax);
UIManager.put("OptionPane:\"OptionPane.button\"[Default].font", ax);

此外,當將其設置為UIResource時,第二個按鈕的自定義字體在翻轉/聚焦時會丟失。 一個hackaround是將其設置為UIResource,即

public static void setUIFont(Font a){
    // force usage of the button's font as set by optionPaneUI
    // by _not_ making it a uiResource
    UIManager.put("OptionPane.buttonFont", a);
    // use uiResource for others
    FontUIResource ax=new FontUIResource(a);
    UIManager.put("OptionPane.messageFont", ax);
    ...
}

通常,不是最好的想法。 但是我們可能會在這里使用它,因為按鈕是在每次調用JOptionPane.create時創建的,對單個實例沒有持久影響。

暫無
暫無

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

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