张世豪
23 小时以前 03b0fb0ba2de86bcfff277778826547c0e37a93f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package xitongshezhi;
 
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Frame;
import java.io.File;
 
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
 
/**
 * 定时关闭对话框
 * 使用静态方法避免内存泄漏
 */
public class Dingshidialog {
    
    /**
     * 显示定时关闭对话框
     * @param parent 父窗口
     * @param countdownTime 倒计时时间(秒)
     * @param message 提示信息
     * @param audioFile MP3文件名(根目录下,无需扩展名)
     * @return 1-成功 0-失败
     */
    public static int showTimedDialog(Frame parent, int countdownTime, String message, String audioFile) {
        TimedDialog dialog = new TimedDialog(parent, countdownTime, message, audioFile);
        return dialog.showDialog();
    }
    
    /**
     * 内部对话框类,确保使用后能被垃圾回收
     */
    @SuppressWarnings("serial")
    private static class TimedDialog extends JDialog {
        private JLabel countdownLabel;
        private int remainingTime;
        private int result = 0;
        private Clip audioClip;
        private volatile boolean running = true;
        
        public TimedDialog(Frame parent, int countdownTime, String message, String audioFile) {
            super(parent, "", true);
            this.remainingTime = countdownTime;
            initializeUI(message);
            startCountdown();
            playAudio(audioFile);
        }
        
        private void initializeUI(String message) {
            // 设置对话框属性
            setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
            setResizable(false);
            
            // 创建主面板 - 使用不透明背景
            JPanel mainPanel = new JPanel(new BorderLayout(10, 10));
            mainPanel.setBackground(new Color(15, 28, 48));
            mainPanel.setBorder(new EmptyBorder(25, 30, 25, 30));
            mainPanel.setOpaque(true);
 
            // 创建标题区域
            JLabel titleLabel = new JLabel("");
            titleLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 20));
            titleLabel.setForeground(new Color(52, 152, 219));
            titleLabel.setHorizontalAlignment(SwingConstants.CENTER);
            titleLabel.setBorder(new EmptyBorder(0, 0, 15, 0));
 
            // 创建消息区域面板
            JPanel messagePanel = new JPanel(new BorderLayout(10, 10));
            messagePanel.setBackground(new Color(15, 28, 48));
            messagePanel.setOpaque(true);
            messagePanel.setBorder(new EmptyBorder(10, 0, 10, 0));
 
            // 创建消息标签
            JLabel messageLabel = new JLabel("<html><div style='text-align: center; width: 300px;'>" + message + "</div></html>");
            messageLabel.setFont(new Font("Microsoft YaHei", Font.PLAIN, 20));
            messageLabel.setForeground(new Color(52, 152, 219));
            messageLabel.setHorizontalAlignment(SwingConstants.CENTER);
 
            // 创建倒计时标签
            countdownLabel = new JLabel("剩余时间: " + remainingTime + "秒");
            countdownLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 16));
            countdownLabel.setForeground(new Color(231, 76, 60));
            countdownLabel.setHorizontalAlignment(SwingConstants.CENTER);
            countdownLabel.setBorder(new EmptyBorder(15, 0, 0, 0));
 
            // 组装消息区域
            messagePanel.add(messageLabel, BorderLayout.CENTER);
            messagePanel.add(countdownLabel, BorderLayout.SOUTH);
 
            // 创建按钮面板
            JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 0));
            buttonPanel.setBackground(new Color(15, 28, 48));
            buttonPanel.setOpaque(true);
            buttonPanel.setBorder(new EmptyBorder(15, 0, 0, 0));
 
            // 创建立即关闭按钮
            JButton closeButton = createStyledButton("立即关闭", new Color(231, 76, 60));
            buttonPanel.add(closeButton);
 
            // 组装主面板
            mainPanel.add(titleLabel, BorderLayout.NORTH);
            mainPanel.add(messagePanel, BorderLayout.CENTER);
            mainPanel.add(buttonPanel, BorderLayout.SOUTH);
 
            // 设置对话框内容
            getContentPane().setBackground(new Color(15, 28, 48));
            getContentPane().add(mainPanel);
            pack();
            setLocationRelativeTo(getParent());
            
            // 按钮事件处理
            closeButton.addActionListener(e -> {
                result = 1;
                disposeDialog();
            });
            
            // 对话框关闭事件
            addWindowListener(new java.awt.event.WindowAdapter() {
                @Override
                public void windowClosing(java.awt.event.WindowEvent e) {
                    disposeDialog();
                }
            });
        }
        
        /**
         * 开始倒计时
         */
        private void startCountdown() {
            Thread countdownThread = new Thread(() -> {
                try {
                    while (running && remainingTime > 0) {
                        Thread.sleep(1000);
                        remainingTime--;
                        
                        SwingUtilities.invokeLater(() -> {
                            if (countdownLabel != null) {
                                countdownLabel.setText("剩余时间: " + remainingTime + "秒");
                            }
                        });
                        
                        if (remainingTime <= 0) {
                            SwingUtilities.invokeLater(() -> {
                                result = 1;
                                disposeDialog();
                            });
                            break;
                        }
                    }
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
            });
            countdownThread.setDaemon(true);
            countdownThread.start();
        }
        
        /**
         * 播放音频文件
         */
        private void playAudio(String audioFileName) {
            if (audioFileName == null || audioFileName.trim().isEmpty()) {
                return;
            }
            
            try {
                String filePath = audioFileName.endsWith(".mp3") ? audioFileName : audioFileName + ".mp3";
                File audioFile = new File(filePath);
                
                if (!audioFile.exists()) {
                    System.err.println("音频文件不存在: " + audioFile.getAbsolutePath());
                    return;
                }
                
                AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(audioFile);
                audioClip = AudioSystem.getClip();
                audioClip.open(audioInputStream);
                audioClip.start();
                
            } catch (Exception e) {
                System.err.println("播放音频失败: " + e.getMessage());
                // 实际项目中应使用支持MP3的库
            }
        }
        
        /**
         * 创建样式化按钮
         */
        private JButton createStyledButton(String text, Color color) {
            JButton button = new JButton(text);
            button.setFont(new Font("Microsoft YaHei", Font.BOLD, 14));
            button.setBackground(color);
            button.setForeground(Color.WHITE);
            button.setFocusPainted(false);
            button.setBorder(BorderFactory.createEmptyBorder(10, 20, 10, 20));
            button.setCursor(new Cursor(Cursor.HAND_CURSOR));
 
            button.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseEntered(java.awt.event.MouseEvent evt) {
                    button.setBackground(brighterColor(color));
                }
 
                public void mouseExited(java.awt.event.MouseEvent evt) {
                    button.setBackground(color);
                }
            });
 
            return button;
        }
        
        private Color brighterColor(Color color) {
            int r = Math.min(255, color.getRed() + 30);
            int g = Math.min(255, color.getGreen() + 30);
            int b = Math.min(255, color.getBlue() + 30);
            return new Color(r, g, b);
        }
        
        /**
         * 清理资源
         */
        private void disposeDialog() {
            running = false;
            
            // 停止音频
            if (audioClip != null) {
                if (audioClip.isRunning()) {
                    audioClip.stop();
                }
                audioClip.close();
                audioClip = null;
            }
            
            // 清理UI引用
            countdownLabel = null;
            
            dispose();
        }
        
        /**
         * 显示对话框
         */
        public int showDialog() {
            setVisible(true);
            return result;
        }
    }    
    
}