[英]Java: Custom Buttons in showInputDialog
How do you add custom text to the buttons of a JOptionPane.showInputDialog? 如何将自定义文本添加到JOptionPane.showInputDialog的按钮?
I know about this question JOptionPane showInputDialog with custom buttons , but it doesn't answer the question asked, it just references them to JavaDocs, which doesn't answer it. 我知道这个带有自定义按钮的问题JOptionPane showInputDialog ,但是它没有回答所提出的问题,它只是将它们引用到JavaDocs中,而JavaDocs却没有回答。
Code So Far: 到目前为止的代码:
Object[] options1 = {"Try This Number",
"Choose A Random Number",
"Quit"};
JOptionPane.showOptionDialog(null,
"Enter a number between 0 and 10000",
"Enter a Number",
JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.PLAIN_MESSAGE,
null,
options1,
null);
I would like to add a text field to this. 我想为此添加一个文本字段。
You can use custom component instead of a string message, for example: 您可以使用自定义组件而不是字符串消息,例如:
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class TestDialog {
public static void main(String[] args) {
Object[] options1 = { "Try This Number", "Choose A Random Number",
"Quit" };
JPanel panel = new JPanel();
panel.add(new JLabel("Enter number between 0 and 1000"));
JTextField textField = new JTextField(10);
panel.add(textField);
int result = JOptionPane.showOptionDialog(null, panel, "Enter a Number",
JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE,
null, options1, null);
if (result == JOptionPane.YES_OPTION){
JOptionPane.showMessageDialog(null, textField.getText());
}
}
}
Have a look at How to Make Dialogs: Customizing Button Text . 看看如何制作对话框:自定义按钮文本 。
Here is an example given: 这是一个示例:
Object[] options = {"Yes, please",
"No, thanks",
"No eggs, no ham!"};
int n = JOptionPane.showOptionDialog(frame,//parent container of JOptionPane
"Would you like some green eggs to go "
+ "with that ham?",
"A Silly Question",
JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,//do not use a custom Icon
options,//the titles of buttons
options[2]);//default button title
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.