java 如何更改 joptionpane 的大小和字体?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26913923/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 10:51:41  来源:igfitidea点击:

How do you change the size and font of a joptionpane?

javaswingnetbeansfontsjoptionpane

提问by neo999

Can you change the font and size of the text of a JOptionPane? I tried it and it works only if I "run file" on that specific java class. If you start the whole project it does not change the font. I only want to change only a specific JOptionPane not all of them.

您可以更改 JOptionPane 文本的字体和大小吗?我试过了,只有当我在那个特定的 java 类上“运行文件”时它才有效。如果您启动整个项目,它不会更改字体。我只想更改特定的 JOptionPane 而不是全部。

Here is the code:

这是代码:

 UIManager.put("OptionPane.messageFont", new FontUIResource(new Font(  
          "Arial", Font.BOLD, 18)));       
 JOptionPane.showMessageDialog(null,"MESSAGE","ERROR",JOptionPane.WARNING_MESSAGE);         

回答by Sergiy Medvynskyy

It's really easy. JOption pane accepts not only strings but also components. So you can create a label set its font and use it as message.

这真的很容易。JOption 窗格不仅接受字符串,还接受组件。因此,您可以创建一个标签,设置其字体并将其用作消息。

JLabel label = new JLabel("MESSAGE");
label.setFont(new Font("Arial", Font.BOLD, 18));
JOptionPane.showMessageDialog(null,label,"ERROR",JOptionPane.WARNING_MESSAGE);

I don't understand why nobody answered this question before

我不明白为什么以前没有人回答这个问题

回答by WesternGun

This is the way we shall use:

这是我们将使用的方式:

UIManager.getLookAndFeelDefaults().put("OptionPane.messageFont", new Font("Arial", Font.BOLD, 14)); UIManager.getLookAndFeelDefaults().put("OptionPane.buttonFont", new Font("Arial", Font.PLAIN, 12));

UIManager.getLookAndFeelDefaults().put("OptionPane.messageFont", new Font("Arial", Font.BOLD, 14)); UIManager.getLookAndFeelDefaults().put("OptionPane.buttonFont", new Font("Arial", Font.PLAIN, 12));

UIManager.put("OptionPane.messageFont", new Font("Arial", Font.BOLD, 14));
UIManager.put("OptionPane.buttonFont", new Font("Arial", Font.PLAIN, 12));

Just remember to set it before any JOptionPane dialog appears.I just put it in the first line of the mainmethod.

请记住在任何 JOptionPane 对话框出现之前设置它。我只是把它放在方法的第一行main

To see why I do this, the DOC of UIManageris always useful.

要了解我为什么这样做,UIManager的 DOC总是有用的。

Defaults

UIManager manages three sets of UIDefaults. In order, they are:

Developer defaults. With few exceptions Swing does not alter the developer defaults; these are intended to be modified and used by the developer.

Look and feel defaults. The look and feel defaults are supplied by the look and feel at the time it is installed as the current look and feel (setLookAndFeel() is invoked). The look and feel defaults can be obtained using the getLookAndFeelDefaults() method.

System defaults. The system defaults are provided by Swing. Invoking any of the various get methods results in checking each of the defaults, in order, returning the first non-null value. For example, invoking UIManager.getString("Table.foreground") results in first checking developer defaults. If the developer defaults contain a value for "Table.foreground" it is returned, otherwise the look and feel defaults are checked, followed by the system defaults. It's important to note that getDefaults returns a custom instance of UIDefaults with this resolution logic built into it. For example, UIManager.getDefaults().getString("Table.foreground") is equivalent to UIManager.getString("Table.foreground"). Both resolve using the algorithm just described. In many places the documentation uses the word defaults to refer to the custom instance of UIDefaults with the resolution logic as previously described.

默认值

UIManager 管理三组 UIDefaults。按顺序,它们是:

开发者默认。除了少数例外,Swing 不会改变开发人员的默认设置;这些旨在供开发人员修改和使用。

外观和感觉默认。外观默认值由外观在作为当前外观安装时提供(调用 setLookAndFeel())。外观和感觉默认值可以使用 getLookAndFeelDefaults() 方法获得。

系统默认。系统默认值由 Swing 提供。调用各种 get 方法中的任何一种都会导致依次检查每个默认值,并返回第一个非空值。例如,调用 UIManager.getString("Table.foreground") 会导致首先检查开发人员的默认值。如果开发人员默认值包含“Table.foreground”的值,则返回它,否则检查外观默认值,然后是系统默认值。重要的是要注意 getDefaults 返回一个 UIDefaults 的自定义实例,其中内置了这个解析逻辑。例如, UIManager.getDefaults().getString("Table.foreground") 等价于 UIManager.getString("Table.foreground")。两者都使用刚才描述的算法进行解析。

So, we should change first the developer defaults. And the method UIManager.put(Object key, Object value)is the method to use.

因此,我们应该首先更改开发人员的默认设置。而方法UIManager.put(Object key, Object value)就是使用的方法。

public static Object put(Object key, Object value)

Stores an object in the developer defaults. This is a cover method for getDefaults().put(key, value). This only affects the developer defaults, not the system or look and feel defaults.

Parameters:

key - an Object specifying the retrieval key

value - the Object to store; refer to UIDefaults for details on how null is handled

Returns: the Object returned by UIDefaults.put(java.lang.Object, java.lang.Object)

Throws:

NullPointerException - if key is null

公共静态对象放置(对象键,对象值)

将对象存储在开发人员默认设置中。这是 getDefaults().put(key, value) 的覆盖方法。这仅影响开发人员默认值,而不影响系统或外观默认值。

参数:

key - 指定检索键的对象

value - 要存储的对象;有关如何处理 null 的详细信息,请参阅 UIDefaults

返回: UIDefaults.put(java.lang.Object, java.lang.Object) 返回的Object

抛出:

NullPointerException - 如果键为空

This is exactly what I am looking for: no extra panels, no more trouble of overriding the default UI of JOptionPane.

这正是我正在寻找的:没有额外的面板,不再有覆盖JOptionPane.

A complete list of names of attributes in JOptionPaneis here:

属性名称的完整列表在JOptionPane这里:

http://www.java2s.com/Tutorial/Java/0240__Swing/CustomizingaJOptionPaneLookandFeel.htm

http://www.java2s.com/Tutorial/Java/0240__Swing/CustomizingaJOptionPaneLookandFeel.htm

Property String                                 Object Type

OptionPane.actionMap                            ActionMap
OptionPane.background                           Color
OptionPane.border                               Border
OptionPane.buttonAreaBorder                     Border
OptionPane.buttonClickThreshhold                Integer
OptionPane.buttonFont                           Font
OptionPane.buttonOrientation                    Integer
OptionPane.buttonPadding                        Integer
OptionPane.cancelButtonMnemonic                 String
OptionPane.cancelButtonText                     String
OptionPane.cancelIcon                           Icon
OptionPane.errorDialog.border.background        Color
OptionPane.errorDialog.titlePane.background     Color
OptionPane.errorDialog.titlePane.foreground     Color
OptionPane.errorDialog.titlePane.shadow         Color
OptionPane.errorIcon                            Icon
OptionPane.errorSound                           String
OptionPane.font                                 Font
OptionPane.foreground                           Color
OptionPane.informationIcon                      Icon
OptionPane.informationSound                     String
OptionPane.inputDialogTitle                     String
OptionPane.isYesLast                            Boolean
OptionPane.messageAnchor                        Integer
OptionPane.messageAreaBorder                    Border
OptionPane.messageFont                          Font
OptionPane.messageForeground                    Color
OptionPane.messageDialogTitle                   String
OptionPane.minimumSize                          Dimension
OptionPane.noButtonMnemonic                     String
OptionPane.noButtonText                         String
OptionPane.noIcon                               Icon
OptionPane.okButtonMnemonic                     String
OptionPane.okButtonText                         String
OptionPane.okIcon                               Icon
OptionPane.questionDialog.border.background     Color
OptionPane.questionDialog.titlePane.background  Color
OptionPane.questionDialog.titlePane.foreground  Color
OptionPane.questionDialog.titlePane.shadow      Color
OptionPane.questionIcon                         Icon
OptionPane.questionSound                        String
OptionPane.sameSizeButtons                      Boolean
OptionPane.separatorPadding                     Integer
OptionPane.setButtonMargin                      Boolean
OptionPane.titleText                            String
OptionPane.warningDialog.border.background      Color
OptionPane.warningDialog.titlePane.background   Color
OptionPane.warningDialog.titlePane.foreground   Color
OptionPane.warningDialog.titlePane.shadow       Color
OptionPane.warningIcon                          Icon
OptionPane.warningSound                         String
OptionPane.windowBindings                       Object[ ]
OptionPane.yesButtonMnemonic                    String
OptionPane.yesButtonText                        String
OptionPane.yesIcon                              Icon
OptionPaneUI                                    String

回答by Roshana Pitigala

There is an easy way to change the default font in JOptionPane. Pass a string modified in htmlformat, which means you can either use <font>tag or even CSS.

有一种简单的方法可以更改JOptionPane. 传递经过html格式修改的字符串,这意味着您可以使用<font>标记甚至 CSS。

Using <font>tag.

使用<font>标签。

JOptionPane.showMessageDialog(this, 
        "<html><font face='Calibri' size='15' color='red'>Hello");

font tag

字体标签

Using CSS.

使用 CSS。

JOptionPane.showMessageDialog(this, 
        "<html><h1 style='font-family: Calibri; font-size: 36pt;'>Hello");

using css

使用 CSS

回答by Sebastian

I have detected that in NIMBUS L&F initially no 'messageFont' is set (UIManager.get("OptionPane.messageFont") == null).

我检测到在 NIMBUS L&F 中最初没有设置 'messageFont' (UIManager.get("OptionPane.messageFont") == null)。

So if you want to derive the new font (/font-size) from the default one you can use the key "OptionPane.font" instead (--> UIManager.get("OptionPane.font")), which apparently never returns null. And then set the derived font using key "OptionPane.messageFont".

因此,如果您想从默认字体派生新字体(/font-size),您可以使用键“OptionPane.font”代替(--> UIManager.get("OptionPane.font")),它显然永远不会返回空值。然后使用键“OptionPane.messageFont”设置派生字体。