2

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

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

我如何引用该对话框?

4

2 回答 2

1

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

现在,要提取数据,添加一个 IF 语句并检查呈现给用户的可能选项。如果您添加了 JoptionPane.OK_CANCEL_OPTION,那么您有两个选项:OK 和 CANCEL。IF 语句应该是:如果用户选择 OK,提取字符串,ELSE 做任何你想做的事情。

该字符串应从创建的文本字段中提取(我假设它位于“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 语句的情况下执行此操作,但请确保您相应地处理所有选项

于 2013-04-02T18:14:02.560 回答
0

如果您需要对 的引用JOptionPane,则不能使用showInputDialog便捷方法。您将需要构造一个JOptionPane对象。

于 2013-04-02T18:11:22.453 回答