增加退出保存确认

This commit is contained in:
2025-10-16 01:59:27 +08:00
parent 390fc7375a
commit b5b689740e
+28 -2
View File
@@ -30,10 +30,18 @@ public class ImageDotMarker extends JFrame {
public ImageDotMarker() { public ImageDotMarker() {
setTitle("图片标点工具"); setTitle("图片标点工具");
setSize(1200, 800); setSize(1200, 800);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); // 先不自动退出
setLayout(new BorderLayout()); setLayout(new BorderLayout());
// 设置窗口在屏幕中心显示 // 设置窗口在屏幕中心显示
setLocationRelativeTo(null); setLocationRelativeTo(null);
// 添加窗口关闭监听器
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
confirmExit();
}
});
// 只保留工具栏 // 只保留工具栏
createToolbar(); createToolbar();
@@ -104,7 +112,7 @@ public class ImageDotMarker extends JFrame {
JButton exitBtn = new JButton("退出"); JButton exitBtn = new JButton("退出");
exitBtn.setToolTipText("退出程序"); exitBtn.setToolTipText("退出程序");
exitBtn.addActionListener(e -> System.exit(0)); exitBtn.addActionListener(e -> confirmExit());
toolBar.add(helpBtn); toolBar.add(helpBtn);
toolBar.add(aboutBtn); toolBar.add(aboutBtn);
@@ -237,6 +245,24 @@ public class ImageDotMarker extends JFrame {
updateStatusBar("已添加标记点: " + points.size()); updateStatusBar("已添加标记点: " + points.size());
} }
private void confirmExit() {
// 检查是否有已标记的点
if (!points.isEmpty()) {
int confirm = JOptionPane.showConfirmDialog(this,
"您有未保存的标记点。确定要退出程序吗?",
"确认退出",
JOptionPane.YES_NO_OPTION,
JOptionPane.WARNING_MESSAGE);
if (confirm == JOptionPane.YES_OPTION) {
System.exit(0);
}
} else {
// 如果没有标记点,直接退出
System.exit(0);
}
}
private void updateStatusBar(String message) { private void updateStatusBar(String message) {
// 直接使用类成员变量,避免查找和类型转换错误 // 直接使用类成员变量,避免查找和类型转换错误
JLabel statusLabel = (JLabel) statusPanel.getComponent(0); JLabel statusLabel = (JLabel) statusPanel.getComponent(0);