-1
メニューバーでexitオプションを選択してアプリケーションを終了しようとしていますが、プログラムを閉じる前にプロンプトを表示する必要があります。JMenuItemからプロンプトを取得する
私はここで同様の質問を読んで答えを見つけようとしましたが、多くのソリューションはmenuItemのactionListenerで "System.exti(0)"で終わります。アプリケーションが終了する前にメッセージダイアログを取得する必要があります。ここで
私は、メニューバーを宣言している、あなたは出口のオプションを見ることができます:
JMenuBar menuBar = new JMenuBar();
JMenu archiveMenu = new JMenu("Archive");
menuBar.add(archiveMenu);
newMap = archiveMenu.add("New Map");
newMap.addActionListener(new ArchiveListener());
loadPlaces = archiveMenu.add("Load Places");
loadPlaces.addActionListener(new ArchiveListener());
save = archiveMenu.add("Save");
save.addActionListener(new ArchiveListener());
exit = archiveMenu.add("Exit");
exit.addActionListener(new ArchiveListener());
そして、ここではArchiveListenerから私のコードです。あなたは、私は非常に下部にあるコードで動作するように出口オプションを取得しようとしたことを確認することができますが、このソリューションは動作しません:
class ArchiveListener implements ActionListener {
public void actionPerformed(ActionEvent ave) {
if (ave.getSource() == newMap) {
int result = fc.showOpenDialog(null);
if (result != JFileChooser.APPROVE_OPTION) {
return;
} else {
File file = fc.getSelectedFile();
String filePath = file.getAbsolutePath();
display.setImage(filePath);
validate();
repaint();
}
} else if (ave.getSource() == loadPlaces) {
int result = fc.showOpenDialog(null);
if (result != JFileChooser.APPROVE_OPTION) {
return;
} else {
loadPlaces();
}
}
if (ave.getSource() == save) {
if (fc.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
// File file = fc.getSelectedFile();
}
try {
FileWriter outfile = new FileWriter("jarvafaltet.places.txt");
PrintWriter out = new PrintWriter(outfile);
for (Place p : positionList.values()) {
System.out.println(p);
out.println(p);
out.close();
}
outfile.close();
} catch (FileNotFoundException e) {
JOptionPane.showMessageDialog(null, "Filen kan ej hittas");
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "Fel" + e.getMessage());
}
}
if ("exit".equals(ave.getActionCommand())) {
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) {
System.exit(NORMAL);
}
}
}