下拉菜单无法按预期工作

时间:2012-06-08 02:30:02

标签: java swing

import javax.swing.*;
public class MainP2 {
public MainP2() {
   JOptionPane.showMessageDialog(
          null,
          "Hi welcome to my square Pyramid calculator." + "\nPress Ok to begin.",
"CIS 2235", 
          JOptionPane.PLAIN_MESSAGE
   );
   String[] possibilities = {
           "adjust height", 
           "adjust baselength", 
           "adjust height and baselength", 
           "exit" 
   };
   String s = (String)JOptionPane.showInputDialog(
           null,
           "Choose one",
           "Customized Dialog",
           JOptionPane.PLAIN_MESSAGE,
           null,
           possibilities,
           possibilities[0]
   );
   //-----here's the problem with the code, am i calling it wrong?
   if(possibilities.equals(possibilities[0])){
           String myString = JOptionPane.showInputDialog(null,"Please enter height: ");
    }
}
public static void main (String[] args) {
    MainP2 app = new MainP2();
}
}

我正在构建一个平方金字塔计算器并从头开始编写所有代码,但我似乎遇到了我的第一个问题。我正在给用户一个欢迎信息,然后给他们一个下拉菜单,询问他们想要什么"调整"对于金字塔。他们可以调整的四个选项是金字塔的heightbaselengthheightbaselength,或者他们可以退出。现在,无论他们选择哪种选项(退出选项除外),都会弹出一个输入对话框,要求输入他们选择的新值/值。

我刚尝试了第一个选项只是为了看它是否有效但没有运气。每当我点击调整高度时索引为0,程序就会结束并忽略我的if语句,如果该选项被索引为0,则应弹出JOptionPane输入对话框。

我做错了什么?

2 个答案:

答案 0 :(得分:6)

我认为你的意思是:

if(s.equals(possibilities[0]))

答案 1 :(得分:3)

替换以下内容:

if(possibilities.equals(possibilities[0])){

使用:

if(s.equals(possibilities[0])){