更改 JOptionPane.showInputDialog 个选项的文字大小

Change text size of JOptionPane.showInputDialog options

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

    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 更改组件属性也会影响所有未来的 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");
}

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

如果在没有任何选择的情况下选择 确定 按钮,则 Accuracy Selected Is: 100% 将显示在控制台 Window 中。另一方面,如果选择 取消 或标题栏关闭按钮 [x],则 Input Canceled! 会显示在控制台 Window。

如果从对话框中的组合框中选择了 20% 并且选择了 确定 按钮,则 Accuracy Selected Is: 20% 是在控制台中显示 Window.

另外一个说明,如果你想在对话框中添加自定义图像,如下所示:

然后将这一行添加到代码的顶部:

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)。

您可以通过这种方式轻松了解它的灵活性,并且...它不会影响任何其他未来的 JOPtionPanes。