Java JOptionPane默认文本



当我要求用户输入我使用下面代码编写的程序的数量时,默认文本为3。

String input = JOptionPane.showInputDialog(null, "Please enter new quantity",
                                           JOptionPane.QUESTION_MESSAGE);

我如何改变这个?

您使用的方法是:

public static String showInputDialog(Component parentComponent,
                                     Object message,
                                     Object initialSelectionValue)

这里第三个参数(initialSelectionValue)是文本字段中的默认值。你给JOptionPane.QUESTION_MESSAGE作为第三个参数,它是一个值= 3的整型常数。因此,在文本字段中输入的默认值为3。

试试这个:

String input = JOptionPane.showInputDialog(null,
                "Please enter new quantity", "");

String input = JOptionPane.showInputDialog(null,
                "Please enter new quantity", "Please enter new quantity",
                JOptionPane.QUESTION_MESSAGE);

这样就可以了:

String input = (String)JOptionPane.showInputDialog(null, "Please enter new quantity",
"Please enter new quantity", JOptionPane.QUESTION_MESSAGE,null,null,"default text");

您正在使用的方法是JOptionPane。showwinputdialog(组件,对象,对象)。

你想使用的方法是JOptionPane。showwinputdialog(组件,对象,字符串,int).

最新更新