如何更改JOptionPane.showInputDialog中按钮的默认文本 [英] how to change the default text of buttons in JOptionPane.showInputDialog

查看:210
本文介绍了如何更改JOptionPane.showInputDialog中按钮的默认文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将 JOptionPane.showInputDialog
中的OK和CANCEL按钮的文本设置为我自己的字符串。

I want to set the text of OK and CANCEL buttons in JOptionPane.showInputDialog to my own strings.

有一种方法可以在 JOptionPane.showOptionDialog 中更改按钮的文本,但我找不到在 showInputDialog 中更改它的方法。

There is a way to change the buttons' text in JOptionPane.showOptionDialog, but I couldn't find a way to change it in showInputDialog.

推荐答案

如果你想要JOptionPane带有自定义按钮文本的.showInputDialog,你可以扩展JOptionPane:

If you want the JOptionPane.showInputDialog with custom button texts, you could extend JOptionPane:

public class JEnhancedOptionPane extends JOptionPane {
    public static String showInputDialog(final Object message, final Object[] options)
            throws HeadlessException {
        final JOptionPane pane = new JOptionPane(message, QUESTION_MESSAGE,
                                                 OK_CANCEL_OPTION, null,
                                                 options, null);
        pane.setWantsInput(true);
        pane.setComponentOrientation((getRootFrame()).getComponentOrientation());
        pane.setMessageType(QUESTION_MESSAGE);
        pane.selectInitialValue();
        final String title = UIManager.getString("OptionPane.inputDialogTitle", null);
        final JDialog dialog = pane.createDialog(null, title);
        dialog.setVisible(true);
        dialog.dispose();
        final Object value = pane.getInputValue();
        return (value == UNINITIALIZED_VALUE) ? null : (String) value;
    }
}

您可以这样称呼它:

JEnhancedOptionPane.showInputDialog("Number:", new Object[]{"Yes", "No"});

这篇关于如何更改JOptionPane.showInputDialog中按钮的默认文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆