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

时间:2019-02-28 00:43:29

标签: java joptionpane

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

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

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

1 个答案:

答案 0 :(得分: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

如果未选择确定按钮,则会在控制台窗口中显示Accuracy Selected Is: 100%。另一方面,如果选择了取消或标题栏关闭按钮 [x] ,则会在控制台窗口中显示Input Canceled!

如果从对话框的组合框中选择了 20%,并且选择了 OK 按钮,则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。