部分代码如下,运行没有问题,不过发现了一点微妙的错误。运行后,用鼠标点击按钮,或者用 Alt + Y,Alt + N 快捷键,JOptionPane的返回值是正确无误的。
但是,用Tab键切换选择的按钮,然后Enter确认,无论选择的是哪个,返回值一律是默认的YES_OPTION。
这是为什么?这种操作有误还是我写得有问题? 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
但是,用Tab键切换选择的按钮,然后Enter确认,无论选择的是哪个,返回值一律是默认的YES_OPTION。
这是为什么?这种操作有误还是我写得有问题? 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
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货