InputDialog框显示随机“-1”

时间:2014-05-20 16:42:50

标签: java user-interface joptionpane inputbox

一切运行完美但String am1 = (String)JOptionPane.showInputDialog显示随机默认值“-1”。

private void am1ActionPerformed(java.awt.event.ActionEvent evt) {     

getinfo();//Set the random question and answer
    am1.setEnabled(false);
Object[] options = {"Answer", "Cancel"};
int n = JOptionPane.showOptionDialog(null,
        JeopardyGUI.question1_1,//Reference the question set
        "",
        JOptionPane.YES_NO_OPTION,
        JOptionPane.PLAIN_MESSAGE,
        null,     //do not use a custom Icon
        options,  //the titles of buttons
        options[0]); //default button title

    if(n == JOptionPane.YES_OPTION){
       String am1 = (String)JOptionPane.showInputDialog("",JOptionPane.PLAIN_MESSAGE);

       if(am1.equalsIgnoreCase(JeopardyGUI.answer1_1)){
           Jscore += 100;
           JOptionPane.showMessageDialog(null, JeopardyGUI.answer1_1 );
       }
       else if(!am1.equalsIgnoreCase(JeopardyGUI.answer1_1)){
           Jscore += -100;
           JOptionPane.showMessageDialog(null, JeopardyGUI.answer1_1 );
       }
 //           else
 //               am1.setEnabled(true);

    }
    if(n == JOptionPane.NO_OPTION){
        //am1.setVisible(false);
        am1.setEnabled(true);
    }
}                                   

2 个答案:

答案 0 :(得分:1)

您正在使用:

public static String showInputDialog(Object message, Object initialSelectionValue)

在这种情况下,JOptionPane.PLAIN_MESSAGE是您的initialSelectionValue。它是一个等于-1的int,我在猜测。你真正想要的可能是:

JOptionPane.showInputDialog("Actual message", "");

另外,要小心:

String am1 = ...

将am1隐藏为类成员,它似乎是某个组件。

我还建议将处理逻辑重写为:

if(am1 != null && am1.equalsIgnoreCase(JeopardyGUI.answer1_1)){
    Jscore += 100;
else Jscore += -100;
JOptionPane.showMessageDialog(null, JeopardyGUI.answer1_1 );

答案 1 :(得分:0)

您需要使用其他静态方法,它不会收到默认值:

JOptionPane.showInputDialog("")

检查JOptionPane API here