【问题标题】:how do I place an if statement after a selection if made from dropdown如果从下拉列表中进行选择,如何在选择后放置 if 语句
【发布时间】:2017-04-20 20:52:05
【问题描述】:

我有两个下拉列表,我想在选择它们后使用它们我希望根据选择弹出另一个框以显示信息。当我尝试使用 if 语句并按下 OK 按钮时,它只会继续到下一行。我从 showmessagedialog 开始,但我想在某些下拉选项中显示警告。它不工作。我还需要能够循环回到主菜单,以便他们做出不同的选择。

我现在拥有的示例:

 int x = JOptionPane.showOptionDialog(null, "Zookeepers would you like to     view animal activities or monitor habitats?",
            "Welcome to the Brooklyn Zoo!",         JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, icon,     options,     options[0]);
System.out.println(x);  



if(x==0){  

String[] choices = {"Lions","Tigers","Bears","Giraffes",};
String input = (String) JOptionPane.showInputDialog(null,"Select     Animal:","Zoo Animals",
JOptionPane.QUESTION_MESSAGE,null,choices,choices[1]);


if (choices.equals("Lions"));
String [] button = {"OK","Cancel","Warning"};
JOptionPane.showOptionDialog(null, "\"Animal: Lion\\nName: Leo\\nAge: 5    \\nFeeding Schedule: Twice daily\\n\\nALERT: Cut on left front paw",
    "Animal",JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.WARNING_MESSAGE,     options, options [0]);

【问题讨论】:

标签: java swing if-statement joptionpane dropdown


【解决方案1】:

我有两个下拉菜单

没有这样的组件。使用正确的名称JComboBox,这样我们就不必猜测您在说什么。

我真的不明白你的问题,因为你没有发布正确的MCVE,但是,以下是一个明显的问题:

String[] choices = {"Lions","Tigers","Bears","Giraffes",};
String input = (String) JOptionPane.showInputDialog(...);

...

if (choices.equals("Lions"));

变量“choices”是一个数组。您不能将数组与字符串进行比较。

也许你想要:

if (input.equals("Lions"));

还请仔细看看下面的代码:

if (choices.equals("Lions"));
String [] button = {"OK","Cancel","Warning"};
JOptionPane.showOptionDialog(...)

您不使用 {} 来标记 if 语句的语句。所以只有第一个语句被认为是 if 语句的一部分。然后将始终执行 JOptionPane... 语句。

在编写代码时始终使用以下结构,以免出错:

if (....)
{
    // do something
}

if 语句中代码的缩进也使其更易于阅读。

【讨论】:

  • 我在 JOptionPane 中遇到错误,说实际参数列表和形式参数列表的长度不同。我尝试将 if 设置为 (input.equals("Lions")) 并且当我运行它时
  • @azoltek,这是基本的 Java。我不是读心术。我不知道你会做错什么。你还没有发布你的“最小、完整和可验证的例子”,所以我猜你并不是真的需要帮助。你得到你所给予的。学会用解决问题所需的所有信息提出正确的问题。
  • 我正在努力。很抱歉你觉得我造成了问题。如果这是一个问题,我将不再发布。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-24
  • 1970-01-01
  • 2017-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多