我是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(第三个问题的答案):
使用以下自动换行文本的功能。这个函数反过来调用问题中已经给出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;
}
有人知道第二个问题的答案吗?