gpt4 book ai didi

java - 更改 JOptionPane.showInputDialog 选项的文本大小

转载 作者:太空宇宙 更新时间:2023-11-04 09:49:56 25 4
gpt4 key购买 nike

我正在尝试更改以下代码中提供的可能性的字体大小:

    font = new Font("Arial", Font.PLAIN, 20);

UIManager.put("OptionPane.messageFont", font);
UIManager.put("OptionPane.buttonFont", font);

Object[] possibilities = {"100%", "80%", "20%"};
selected = (String)JOptionPane.showInputDialog(
frame,
"Select the accuracy level.",
"Accuracy",
JOptionPane.PLAIN_MESSAGE,
null, possibilities,
"100%");

如您所见,我知道如何更改消息字体和按钮字体,但我无法更改选项本身的字体。

最佳答案

请记住,通过 UIManager 更改组件属性也会影响所有 future 使用这些已更改属性的 JOptionPanes。最好通过简单地为对话框创建自定义面板来处理手头的 JOptionPane。

在下面的示例代码中,我们使用自定义确认对话框作为输入框。以下是您可以如何做到的:

// Your desired Accuracy choices for dialog combox.
Object[] possibilities = {"20%", "80%", "100%"};

//The panel to display within the Dialog
JPanel jp = new JPanel();
jp.setLayout(new BorderLayout()); // Panel layout manager

// JLabel to hold the dialog text. HTML is used to add pizzaz. :)
JLabel jl = new JLabel(
"<html>Select the desired <font color=blue><b>Accuracy Level</font>:"
+ "</b><br><br></html>");
// Desired font, style, and size for Message
Font font = new Font("Arial", Font.PLAIN, 14);
jl.setFont(font); // Set the font to JLabel (the msg)
jp.add(jl, BorderLayout.NORTH); // Add JLabel to top of Dialog

// JComboBox to hold all the available Accuracy choices.
JComboBox jc = new JComboBox(possibilities);
jc.setSelectedIndex(2); // Set 100% as default in Combo
// Desired font, style, and size for combo items
font = new Font("Arial", Font.PLAIN, 20);
jc.setFont(font); // Set the font to combo
jp.add(jc, BorderLayout.SOUTH); // Add JComboBox to Bottom section of Dialog

String valueSelected; // Variable to hold the combo selected value.

/* Display the custom Input Box Dialog which is actually a
customized Confirm Dialog Box with the above JPanel supplied
as the message content. Also, if the OK button was selected
then fill the valueSelected string variable declared above
with the Combo selection. 100% has been set as default.*/
if (JOptionPane.showConfirmDialog(this, jp, "Accuracy",
JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE) == 0) {
valueSelected = jc.getSelectedItem().toString();
System.out.println("Accuracy Selected Is: " + valueSelected);
}
else {
System.out.println("Input Canceled");
}

最终,当遇到此代码时,将显示一个自定义输入对话框,如下所示:

enter image description here

如果在没有任何选择的情况下选择确定按钮,则控制台窗口中将显示选定的准确度为:100%。另一方面,如果选择了取消或标题栏关闭按钮[x],则控制台窗口中会显示输入已取消!

如果从对话框内的组合框中选择20%,并选择确定按钮,则控制台窗口中将显示选择的精度为:20%

enter image description here

另一点,如果您想向对话框添加自定义图像,如下所示:

enter image description here

然后将此行添加到代码顶部:

final ImageIcon icon = new ImageIcon("AccuracyIcon.png");

然后将对话框的调用行更改为:

if (JOptionPane.showConfirmDialog(this, jp, "Accuracy", 
JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, icon) == 0) {
valueSelected = jc.getSelectedItem().toString();
System.out.println("Accuracy Selected Is: " + valueSelected);
}
else {
System.out.println("Input Canceled");
}

当然,您必须为要使用的图像提供正确的路径和文件名。如果你想使用示例中的图像,那么你可以获取它 here (请务必相应地调整其大小 - 72x76)。

您可以轻松地看到这样做的灵 active ,并且...它不会影响任何其他 future 的 JOPtionPanes。

关于java - 更改 JOptionPane.showInputDialog 选项的文本大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54916694/

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