[英]Java Swing JOptionPane Yes and No Button differ in font size after setting Nimbus LookandFeel
由於在默認的LookAndFeel中字體大小太小,我使用幾行代碼將其設置為Nimbus ,而我的JOptionPane顯示具有不同大小的Yes和No按鈕。 是仍然非常小,而“ 否”設置為我指定的大小。 有誰知道為什么或如何解決它?
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()
列出了包含optionpane或button的所有鍵 ,並且它們的字體大小都設置為20. Yes按鈕仍然是較小的字體。
JButton
的字體實際上來自Button.font
屬性。
如果我將其添加到您的屬性列表中,它會起作用。
javax.swing.UIManager.put("Button.font", ax);
為什么你會得到兩種不同的尺寸,雖然很有趣,但此時此刻已超出我的范圍。
看起來沒有為optionPane中創建的第一個按鈕獲取自定義字體的原因是它被設為rootPane的defaultButton。 因此,它有一個特殊的狀態[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.