我是Java初学者。我正在使用以下代码在我的应用程序中显示弹出窗口。
public static int showConfirmDialog(Component parentComponent,
Object message, String title, int optionType)
{
JOptionPane pane = new JOptionPane(message, JOptionPane.QUESTION_MESSAGE,
optionType);
//pane.setFont(new java.awt.Font("SansSerif", 0, 12));//Not working to change the font of pop-up text and button texts
final JDialog dialog = pane.createDialog(parentComponent, title);
dialog.setVisible(false) ;
//dialog.setFont(new java.awt.Font("SansSerif", 0, 12)); //Not working to change the font of pop-up text and button texts
dialog.setLocationRelativeTo(parentComponent);
dialog.setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
dialog.setModal(true);
dialog.setVisible(true) ;
dialog.dispose();
Object o = pane.getValue();
if (o instanceof Integer) {
return (Integer)o;
}
return JOptionPane.CLOSED_OPTION;
}
弹出窗口正确显示,但存在以下问题:
更新:(答案为1) 从JOptionPane获取组件并设置如下字体:
private static final String key = "OptionPane.messageFont";
//Question: QUESTION_MESSAGE
public static int showConfirmDialog(Component parentComponent,
Object message, String title, int optionType){
UIManager.put(key, new java.awt.Font("SansSerif", 0, 12));
JOptionPane pane = new JOptionPane(message, JOptionPane.QUESTION_MESSAGE,
optionType);
JPanel buttonPanel = (JPanel)pane.getComponent(1);
Object buttonOk[] = buttonPanel.getComponents();
for (int i = 0; i < buttonOk.length; i++) {
JButton button = (JButton)buttonOk[i];
button.setFont(new java.awt.Font("SansSerif", 0, 12));
button.validate();
}
pane.setFont(new java.awt.Font("SansSerif", 0, 12));
final JDialog dialog = pane.createDialog(parentComponent, title);
dialog.setVisible(false) ;
dialog.setFont(new java.awt.Font("SansSerif", 0, 12));
dialog.setLocationRelativeTo(parentComponent);
dialog.setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
dialog.setModal(true);
dialog.setVisible(true) ;
dialog.dispose();
Object o = pane.getValue();
if (o instanceof Integer) {
return (Integer)o;
}
return JOptionPane.CLOSED_OPTION;
}
更新2(第3个问题的答案):
使用以下功能自动包装文本。此函数调用showConfirmDialog(parentComponent, message, title, optionType);
showConfirmDialog(...)
的代码已在问题中给出。
/**
* Question/Confirmation Message Dialog Multiline.
* It DONOT require linebreak ("\n") for multiline messages. It will automatically wrap the text to new lines.
* Note: last when used was not properly working. Please verify it.
*/
public static int showConfirmDialogMultiLine(Component parentComponent,
Object message, String title, int optionType){
JTextArea textArea = new JTextArea((String)message);
textArea.setColumns(50);
textArea.setLineWrap( true );
textArea.setWrapStyleWord( true );
textArea.setSize(textArea.getPreferredSize().width, 1);
textArea.setBackground(parentComponent.getBackground());
textArea.setFont(Usability.getFont("DialogBoxes.DialogTextFont")); //$NON-NLS-1$
textArea.setEditable(false);
message = textArea;
int retVal = showConfirmDialog(parentComponent,
message,
title,
optionType);
return retVal;
}
有人知道第二个问题的答案吗?
答案 0 :(得分:3)
您可以使用UIManager
更改OptionPane.messageFont
,如下所示。
private static final String key = "OptionPane.messageFont";
...
private int showConfirmDialog(...) {
UIManager.put(key, UIManager.getFont(key).deriveFont(Font.ITALIC, 20));
JOptionPane pane = new JOptionPane(...);
...
}
附录:我从哪里获得不同的密钥?
UIManager Defaults是一个很好的资源,因为它可以检查已安装的Look&amp; amp;的默认属性。感觉实施。