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

问题描述 投票:1回答:1

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

    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%");

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

java joptionpane
1个回答
0
投票

请记住,通过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");
}

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

enter image description here

如果选择了OK按钮而没有任何选择,则Accuracy Selected Is: 100%将显示在控制台窗口中。另一方面,如果选择了取消或标题栏关闭按钮[x],则控制台窗口中将显示Input Canceled!

如果从对话框中的组合框中选择了20%并且选择了确定按钮,则控制台窗口中将显示Accuracy Selected Is: 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)。

通过这种方式,您可以很容易地看到它的灵活性......它不会影响任何其他未来的JOPtionPanes。

© www.soinside.com 2019 - 2024. All rights reserved.