类似这样的代码:JOptionPane.showMessageDialog(null,
                            "错误信息!",
                            "Alert",
                            JOptionPane.ERROR_MESSAGE);会弹出一个框,一个确认按钮。
如果此时按回车键,则JOptionPane就会关闭掉!这样可能会导致某些过快按回车键,而不能清楚地看到这个对话框了!
我的问题是:如何才能使按回车键后JOptionPane不会关掉?是不是和焦点有关系?请大家帮忙了,谢谢!

解决方案 »

  1.   

    Object[] options = {"Yes, please", "No way!"};
    JOptionPane.showOptionDialog(this,"Would you like green eggs and ham?", "A Silly Question",  JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE,  null,  options, null);
    最后一个null就是设置JOptionPane的默认值的参数
      

  2.   

    上面的JOptionPane就不可以用回车关掉, 因为JOptionPane没有默认值 ,
    下面的就设置了默认值 ,所以可以用回车关掉JoptionPane
    Object[] options = {"Yes, please",
                        "No way!"};
    int n = JOptionPane.showOptionDialog(frame,
        "Would you like green eggs and ham?",
        "A Silly Question",
        JOptionPane.YES_NO_OPTION,
        JOptionPane.QUESTION_MESSAGE,
        null,     //do not use a custom Icon
        options,  //the titles of buttons
        options[0]); //default button title
      

  3.   

    谢谢楼上的,但是我没有想要用有选择YES 或者NO的,只想有“确认”按钮的
      

  4.   

    谢谢楼上的,我把options 数组只设为一个值就行了,谢谢!
    结帖,给分了,分不多!