java 如何更改 JOptionPane.showInputDialog JTextField 中的字体?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17059575/
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
How to change the font in JOptionPane.showInputDialog JTextField?
提问by user2477252
I need to know the ui manager property that needs to be set to make the font changed for the JTextField
in JOptionPane.showInputDialog
window.
我需要知道需要设置的 ui manager 属性才能为JTextField
inJOptionPane.showInputDialog
窗口更改字体。
回答by Andrew Thompson
One trick is to make a confirmation dialog look like an input dialog. Note that this example still behaves differently to a standard input dialog in that the input field is not selected by default. For that, I have always found the tips in this article on Dialog Focusto be invaluable.
一个技巧是使确认对话框看起来像一个输入对话框。请注意,此示例的行为仍与标准输入对话框不同,因为默认情况下未选择输入字段。为此,我一直发现这篇关于Dialog Focus 的文章中的提示非常宝贵。
The following shows a standard input dialog compared to the confirm dialog using a large font.
下面显示了标准输入对话框与使用大字体的确认对话框的比较。
import java.awt.*;
import javax.swing.*;
class BigInputOptionPane {
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
String s = JOptionPane.showInputDialog(null);
if (s!=null) {
System.out.println("User chose: " + s);
} else {
System.out.println("User did not chose an option!");
}
JTextField tf = new JTextField(8);
tf.setFont(tf.getFont().deriveFont(26f));
int result = JOptionPane.showConfirmDialog(
null, tf, "Input",
JOptionPane.OK_CANCEL_OPTION);
if (result==JOptionPane.OK_OPTION) {
System.out.println("User chose: " + tf.getText());
} else {
System.out.println("User did not chose an option!");
}
}
};
SwingUtilities.invokeLater(r);
}
}
回答by Harry
This worked for me to change both the font of the text and buttons in JOptionPane:
Font font = new Font(Font.SANS_SERIF, Font.PLAIN, 20);
UIManager.put("OptionPane.messageFont", font);
UIManager.put("OptionPane.buttonFont", font);
这对我来说改变了 JOptionPane 中文本和按钮的
字体:Font font = new Font(Font.SANS_SERIF, Font.PLAIN, 20);
UIManager.put("OptionPane.messageFont", font);
UIManager.put("OptionPane.buttonFont", font);
回答by WesternGun
This is the way we shall use:
这是我们将使用的方式:
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 main
method.
请记住在任何 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 JOptionPane
is 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 MadProgrammer
I believe you want TextField.font
. This will return the font used by default for all JTextField
s for the currently installed look and feel...
我相信你想要TextField.font
。这将返回所有JTextField
s默认使用的字体,用于当前安装的外观......
A better solution might be to supply your own JTextField
, setup with the font you want to use.
更好的解决方案可能是JTextField
使用您想要使用的字体提供您自己的, setup 。
For example...
例如...
(Sorry, updated, used wrong dialog :P)
(抱歉,更新了,使用了错误的对话框:P)
import java.awt.EventQueue;
import java.awt.Font;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class TestOptionPane09 {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JTextField field = new JTextField("Hello");
field.setFont(field.getFont().deriveFont(Font.BOLD, 24));
String[] options = {"Ok", "Cancel"};
int result = JOptionPane.showOptionDialog(
null,
field,
"Help",
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
options,
0);
switch (result) {
case 0:
System.out.println("Okay");
break;
case 1:
System.out.println("Cancel");
break;
}
}
});
}
}
回答by user3785010
Here's what works for me in August 2018 using Java 1.8.0_181 to make the font size larger and thus more readable on my 4K laptop monitor. I call it near the top of main prior to invoking dialogs such as the the following:
以下是 2018 年 8 月使用 Java 1.8.0_181 使字体变大,从而在我的 4K 笔记本电脑显示器上更具可读性的方法。在调用如下对话框之前,我在 main 的顶部附近调用它:
String menuChoice = JOptionPane.showInputDialog(null, "some prompt", "Menu", JOptionPane.INFORMATION_MESSAGE);
It's very close to previous answers but differs slightly because it includes settings I did not see in those prior responses:
它与之前的答案非常接近,但略有不同,因为它包含我在之前的响应中没有看到的设置:
/**
* Changes the font size used in JOptionPane.showInputDialogs to make them more ADA
* section 508 compliant by making the text size larger,
* which is very nice for older people and anyone else with vision problems.
*/
private static void makeDialogsEasierToSee() {
// This next one is very strange; but, without it,
// any subsequent attempt to set InternalFrame.titleFont will
// be ignored, so resist the temptation to remove it.
JDialog.setDefaultLookAndFeelDecorated(true);
Font normalFont = new Font(Font.MONOSPACED, Font.PLAIN, 24);
Font boldFont = normalFont.deriveFont(Font.BOLD);
UIManager.put("OptionPane.font", normalFont);
UIManager.put("OptionPane.messageFont", normalFont);
UIManager.put("OptionPane.buttonFont", normalFont);
UIManager.put("TextField.font", normalFont);
UIManager.put("InternalFrame.titleFont", boldFont);
}