java 如何更改 JOptionPane 的字体大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12724686/
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-10-31 09:58:31  来源:igfitidea点击:

how to change font size of JOptionPane

javaswingjoptionpane

提问by Roy Payat

import java.awt.ComponentOrientation;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;

public class trial2 
{   
public static void main(String[] args) 
{
    String[] option = {"Chorita M. Adlawan", "Noel B. Angeles", "Julie P. Benenoso", "Percival P.Bermas", 
            "Beverly Mae M. Brebante","Adela N. Cabaylo", "Carol G. Cainglet", "Oscar U. Cainglet", 

    String selected = (String)JOptionPane.showInputDialog(null, "MINES AND GEOSCIENCES BUREAU - REGION XI, DAVAO CITY\n" +
            "                         LIST OF REGISTERED EMPLOYEES", 
            "Please Select Employee",JOptionPane.DEFAULT_OPTION, null, option , "Adlawan");     

}
}

Those are my codes above. My question is, can i change the font size and style of my JOptionPane to make my list font larger, very much appreciated for any tip. Thanks.

这些是我上面的代码。我的问题是,我可以更改 JOptionPane 的字体大小和样式以增大我的列表字体,非常感谢您提供任何提示。谢谢。

回答by halex

If you want all JOptionPanes to have the same font style and size, you can change the property of the UIManagerwith:

如果您希望所有JOptionPanes 具有相同的字体样式和大小,您可以更改UIManagerwith的属性:

javax.swing.UIManager.put("OptionPane.font", new Font("yourFontName", Font.BOLD, 30));

To get a list of the available font names, use the following snippet:

要获取可用字体名称的列表,请使用以下代码段:

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] fontNames = ge.getAvailableFontFamilyNames();

for (int index = 0; index < fontNames.length; index++)
{
     System.out.println(fontNames[index]);
}

回答by Redandwhite

One of the easiest ways is to use HTML directly in the String literal. For example:

最简单的方法之一是直接在字符串字面量中使用 HTML。例如:

"MINES AND GEOSCIENCES BUREAU - REGION XI, DAVAO CITY\n"

"MINES AND GEOSCIENCES BUREAU - REGION XI, DAVAO CITY\n"

Can easily become:

很容易变成:

"<html><span style='font-size:2em'>MINES AND GEOSCIENCES BUREAU - REGION XI, DAVAO CITY"

"<html><span style='font-size:2em'>MINES AND GEOSCIENCES BUREAU - REGION XI, DAVAO CITY"

Note that when you use HTML, you can no longer use \nfor new lines. Use <br>instead.

请注意,当您使用 HTML 时,您不能再使用\n换行符。使用<br>来代替。