Java 带有自定义按钮的 JOptionPane showInputDialog

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4223983/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 14:45:50  来源:igfitidea点击:

JOptionPane showInputDialog with custom buttons

javaswing

提问by senzacionale

Can i use showInputDialog with my custom buttons or can i rename "OK" and "Cancel" button on showInputDialog.

我可以将 showInputDialog 与我的自定义按钮一起使用,还是可以重命名 showInputDialog 上的“确定”和“取消”按钮。

采纳答案by Riduidel

There are a bunch of variants of each JOptionPanemethod. And choosing the one will usually give you access to the desired level of feature. In your case, you're looking for

每种JOptionPane方法都有许多变体。选择一个通常会让您访问所需级别的功能。在你的情况下,你正在寻找

public static Object showInputDialog(Component parentComponent,
                                 Object message,
                                 String title,
                                 int messageType,
                                 Icon icon,
                                 Object[] selectionValues,
                                 Object initialSelectionValue)

See its javadoc here: JOptionPane#showInputDialog. notice you won't change here the buttons colors (as they're look-and-feel dependant) but rather change their text (which is generally speaking enough, as you can also here set the icon displayed on dialog left side).

在这里看到它的javadoc: JOptionPane#showInputDialog。请注意,您不会在此处更改按钮颜色(因为它们取决于外观),而是更改它们的文本(一般来说就足够了,因为您也可以在此处设置显示在对话框左侧的图标)。

回答by Thediabloman

Check out the JOptionPane documentation.

查看JOptionPane 文档



You can send an array of objects that define the buttons:

您可以发送一组定义按钮的对象:

Show a warning dialog with the options OK, CANCEL, title 'Warning', and message 'Click OK to continue':

显示带有选项 OK、CANCEL、标题“Warning”和消息“Click OK to continue”的警告对话框:

Object[] options = { "OK", "CANCEL" };
JOptionPane.showOptionDialog(null, "Click OK to continue", "Warning",
JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE,
null, options, options[0]);

回答by Mot

Why not create your own JDialog-derived class?

为什么不创建自己的JDialog派生类?