| ÎļþÃû´Ó src/xitongshezhi/Dingshidialog.java ÐÞ¸Ä |
| | |
| | | package xitongshezhi; |
| | | package dialog; |
| | | |
| | | import java.awt.BorderLayout; |
| | | import java.awt.Color; |
| | |
| | | */ |
| | | public class Dingshidialog { |
| | | |
| | | // æ·»å éææ å¿æ¥è·è¸ªå¼¹çªæ¾ç¤ºç¶æ |
| | | private static volatile boolean isDialogShowing = false; |
| | | |
| | | /** |
| | | * æ¾ç¤ºå®æ¶å
³éå¯¹è¯æ¡ |
| | | * @param parent ç¶çªå£ |
| | |
| | | * @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); |
| | | public static int showTimedDialog(Frame parent, int countdownTime, String message) { |
| | | // æ£æ¥æ¯å¦å·²æå¼¹çªå¨æ¾ç¤º |
| | | if (isDialogShowing) { |
| | | System.out.println("å·²æå¼¹çªå¨æ¾ç¤ºï¼å¿½ç¥æ°è¯·æ±"); |
| | | return 0; // è¿å失败 |
| | | } |
| | | |
| | | TimedDialog dialog = new TimedDialog(parent, countdownTime, message); |
| | | return dialog.showDialog(); |
| | | } |
| | | |
| | |
| | | * å
é¨å¯¹è¯æ¡ç±»ï¼ç¡®ä¿ä½¿ç¨åè½è¢«åå¾åæ¶ |
| | | */ |
| | | @SuppressWarnings("serial") |
| | | private static class TimedDialog extends JDialog { |
| | | 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) { |
| | | public TimedDialog(Frame parent, int countdownTime, String message) { |
| | | super(parent, "", true); |
| | | this.remainingTime = countdownTime; |
| | | initializeUI(message); |
| | | startCountdown(); |
| | | playAudio(audioFile); |
| | | } |
| | | |
| | | private void initializeUI(String message) { |
| | |
| | | messageLabel.setHorizontalAlignment(SwingConstants.CENTER); |
| | | |
| | | // å建åè®¡æ¶æ ç¾ |
| | | countdownLabel = new JLabel("å©ä½æ¶é´: " + remainingTime + "ç§"); |
| | | countdownLabel = new JLabel(remainingTime + "ç§åèªå¨å
³é"); |
| | | countdownLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 16)); |
| | | countdownLabel.setForeground(new Color(231, 76, 60)); |
| | | countdownLabel.setHorizontalAlignment(SwingConstants.CENTER); |
| | |
| | | public void windowClosing(java.awt.event.WindowEvent e) { |
| | | disposeDialog(); |
| | | } |
| | | |
| | | @Override |
| | | public void windowOpened(java.awt.event.WindowEvent e) { |
| | | // è®¾ç½®å¼¹çªæ¾ç¤ºæ å¿ |
| | | isDialogShowing = true; |
| | | } |
| | | |
| | | @Override |
| | | public void windowClosed(java.awt.event.WindowEvent e) { |
| | | // ç¡®ä¿å¼¹çªå
³éæ¶éç½®æ å¿ |
| | | isDialogShowing = false; |
| | | } |
| | | }); |
| | | } |
| | | |
| | |
| | | |
| | | SwingUtilities.invokeLater(() -> { |
| | | if (countdownLabel != null) { |
| | | countdownLabel.setText("å©ä½æ¶é´: " + remainingTime + "ç§"); |
| | | countdownLabel.setText( remainingTime + "ç§åèªå¨å
³é"); |
| | | } |
| | | }); |
| | | |
| | |
| | | // æ¸
çUIå¼ç¨ |
| | | countdownLabel = null; |
| | | |
| | | // éç½®å¼¹çªæ¾ç¤ºæ å¿ |
| | | isDialogShowing = false; |
| | | |
| | | dispose(); |
| | | } |
| | | |
| | |
| | | * æ¾ç¤ºå¯¹è¯æ¡ |
| | | */ |
| | | public int showDialog() { |
| | | // 忬¡æ£æ¥æ å¿ï¼é²æ¢ç«ææ¡ä»¶ |
| | | if (isDialogShowing) { |
| | | System.out.println("å·²æå¼¹çªå¨æ¾ç¤ºï¼åæ¶æ¾ç¤º"); |
| | | return 0; |
| | | } |
| | | |
| | | setVisible(true); |
| | | return result; |
| | | } |