JOptionPane返回值在某种情况下不正确,怎么解决
部分代码如下,运行没有问题,不过发现了一点微妙的错误。运行后,用鼠标点击按钮,或者用 Alt + Y,Alt + N 快捷键,JOptionPane的返回值是正确无误的。
但是,用Tab键切换选择的按钮,然后Enter确认,无论选择的是哪个,返回值一律是默认的YES_OPTION。
这是为什么?这种操作有误还是我写得有问题?
--------------------编程问答-------------------- 应该是jdk的bug吧,楼主可以去看看JOptionPane的源码 --------------------编程问答--------------------
private void newFile( DrawMyselfFrame mainFrame ) {
int selectedChoice;
boolean fileSaved = mainFrame.getDrawPanel().getSaved();
DrawPanel drawPanel = mainFrame.getDrawPanel();
// 如果文件未保存
if ( !fileSaved ) {
selectedChoice = JOptionPane.showConfirmDialog(
drawPanel,
"\u662F\u5426\u4FDD\u5B58" + // 是否保存
drawPanel.getPictureName(),
"Draw Myself",
JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE );
if ( selectedChoice == JOptionPane.CANCEL_OPTION ) ; // end inner if
else {
if ( selectedChoice == JOptionPane.YES_OPTION ) {
// MenuBar.this.savePicture();
}
// set the title of the frame
// 如果选择了“是”和“否”,下面几句会将标题上的数字加1
drawPanel.setPictureName();
mainFrame.setFrameTitle( drawPanel.getPictureName() );
mainFrame.setTitle( mainFrame.getFrameTitle() );
} // end else
} // end if
只是个初学者,觉得这太难为我了...
补充:Java , Java SE