我创建了一个JOptionPane
,它只有两个按钮YES_NO_OPTION
。JOptionPane的选择Yes/No确认对话框问题
经过JOptionPane.showConfirmDialog
弹出后,我想点击YES BUTTON
继续打开JFileChooser
,如果我点击NO BUTTON
它应该取消操作。
这似乎很容易,但我不知道哪里是我的错误。
代码段:
if (textArea.getLineCount() >= 1) { //The condition to show the dialog if there is text inside the textArea
int dialogButton = JOptionPane.YES_NO_OPTION;
JOptionPane.showConfirmDialog (null, "Would You Like to Save your Previous Note First?","Warning",dialogButton);
if (dialogButton == JOptionPane.YES_OPTION) { //The ISSUE is here
JFileChooser saveFile = new JFileChooser();
int saveOption = saveFile.showSaveDialog(frame);
if(saveOption == JFileChooser.APPROVE_OPTION) {
try {
BufferedWriter fileWriter = new BufferedWriter(new FileWriter(saveFile.getSelectedFile().getPath()));
fileWriter.write(textArea.getText());
fileWriter.close();
} catch(Exception ex) {
}
}
哇它的工作!我刚开始使用'showConfirmDialog',我没有得到它这么好,虽然我读的Javadoc。但是现在我的错误和你的解释清除了很多混乱。我会更多地玩这个游戏,看看我能想出什么。谢谢!!! – Sobiaholic 2011-12-31 16:24:00
@iMohammad,你为什么不读了Swing教程?本教程包含工作示例,针对您在过去几天中提出的所有问题。 – camickr 2011-12-31 16:41:30
链接camickr的建议:http://docs.oracle.com/javase/tutorial/uiswing/ – ziesemer 2011-12-31 16:43:23