我已经用谷歌搜索了圣洁的bejaysus,但找不到合适的单词组合。如何在没有标识符的地方使用方法?例如;

String inputBio = JOptionPane.showInputDialog(null, heroPane, "Please enter details...", JOptionPane.OK_CANCEL_OPTION);


如何引用该对话框?

最佳答案

因此,您想将输入框中键入的值分配给String吗?您需要创建一个Integer并将其分配给JOptionPane.showInputDialog,然后使用构造函数实例化带有所需选项的Input Dialog的新实例。

现在,要提取数据,请添加IF语句,然后浏览提供给用户的可能选项。如果添加了JoptionPane.OK_CANCEL_OPTION,则有两个选项:OK和CANCEL。
IF语句应为:如果用户选择“确定”,则提取字符串,然后执行任何您想执行的操作。

该字符串应从创建的文本字段中提取(我假设它位于“ heroPane”中的某个位置)

这就是代码中的样子

//the string we use for input
String inputBio;

int optionBox = JOptionPane.showInputDialog(null, heroPane, "Please enter details...", JOptionPane.OK_CANCEL_OPTION);
//now the integer value represents the option selected (OK or CANCEL)
//All we need to do is extract the data and assign it to the string

if (optionBox == JOptionPane.OK_OPTION) {
//the OK option
inputBio = nameOfYourTextField.getText();

} else {
// we have a CANCEL option
}


您当然可以在没有IF语句的情况下执行此操作,但请确保您相应地处理了所有选项

10-07 19:27
查看更多