gpt4 book ai didi

java - 配置弹出窗口的宽度、换行和字体

转载 作者:行者123 更新时间:2023-11-29 05:58:55 26 4
gpt4 key购买 nike

我是 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. 如何将字体更改为弹出文本/消息和按钮文本(是/否)?
  2. 如何将弹出窗口宽度限制为其父宽度?
  3. 如何对弹出的文字进行换行(如文字区域的文字换行)?

更新:(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;
}

有人知道第二个问题的答案吗?

最佳答案

您可以使用 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(...);
...
}

附录:我从哪里获得不同的 key

UIManager Defaults是一个很好的资源,因为它可以检查已安装的外观实现的默认属性。

关于java - 配置弹出窗口的宽度、换行和字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10927156/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com