📄 exercise21_5.java
字号:
import java.awt.*;import java.awt.event.*;import java.applet.*;import javax.swing.*;import java.net.URL;import java.util.*;public class Exercise21_5 extends JApplet { boolean isStandalone = false; ClockWithAlarm clockWithAlarm1 = new ClockWithAlarm(); // Declare audio clip for alarm sound AudioClip alarmSound; /**Initialize the applet*/ public void init() { this.setSize(new Dimension(400,300)); clockWithAlarm1.addActionListener( new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { clockWithAlarm1_actionPerformed(e); } }); clockWithAlarm1.setTimeStyle(0); clockWithAlarm1.setAlarmTime("9,57,23"); this.getContentPane().add(clockWithAlarm1, BorderLayout.CENTER); // Get the URL for the file name URL url = this.getClass().getResource("alarmSound.au"); // Get the audio clip alarmSound = Applet.newAudioClip(url); } /**Main method*/ public static void main(String[] args) { Exercise21_5 applet = new Exercise21_5(); applet.isStandalone = true; JFrame frame = new JFrame(); frame.setTitle("Exercise21_5"); frame.getContentPane().add(applet, BorderLayout.CENTER); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); applet.init(); applet.start(); frame.setSize(400,320); Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); frame.setLocation((d.width - frame.getSize().width) / 2, (d.height - frame.getSize().height) / 2); frame.setVisible(true); } void clockWithAlarm1_actionPerformed(ActionEvent e) { // Play alarm sound // alarmSound.play(); new PlayAlarmSound().start(); }}class PlayAlarmSound extends Thread { // Declare audio clip for alarm sound AudioClip alarmSound; //Construct the applet public PlayAlarmSound() { // Get the URL for the file name URL url = this.getClass().getResource("alarmSound.au"); // Get the audio clip alarmSound = Applet.newAudioClip(url); } public void run() { alarmSound.play(); }}class ClockWithAlarm extends Clock { private java.lang.String alarmDate; private java.lang.String alarmTime; private transient Vector actionListeners; public ClockWithAlarm() { super(); // Get initial date and time GregorianCalendar cal = new GregorianCalendar(tz); // Initialize alarm date, which is consists of // year, month, and date, separated by commas. alarmDate = cal.get(cal.YEAR) + "," + cal.get(cal.MONTH) + "," + cal.get(cal.DATE); // Initialize alarm time, which is consists of // hour, minute, and second, separated by commas. alarmTime = cal.get(cal.HOUR) + "," + cal.get(cal.MINUTE) + "," + cal.get(cal.SECOND); } public void run() { while (true) { try { thread.sleep(1000); } catch (InterruptedException e) {} repaint(); // Fire tick event if the current time matches alarm time GregorianCalendar rightNow = new GregorianCalendar(tz); String rightNowDate = rightNow.get(rightNow.YEAR) + "," + rightNow.get(rightNow.MONTH) + "," + rightNow.get(rightNow.DATE); String rightNowTime = rightNow.get(rightNow.HOUR) + "," + rightNow.get(rightNow.MINUTE) + "," + rightNow.get(rightNow.SECOND); if (rightNowDate.equals(alarmDate) && rightNowTime.equals(alarmTime)) fireActionPerformed(new ActionEvent (this, ActionEvent.ACTION_PERFORMED, null)); } } public void setAlarmDate(java.lang.String newAlarmDate) { alarmDate = newAlarmDate; } public java.lang.String getAlarmDate() { return alarmDate; } public void setAlarmTime(java.lang.String newAlarmTime) { alarmTime = newAlarmTime; } public java.lang.String getAlarmTime() { return alarmTime; } // Generated by JBuilder public synchronized void removeActionListener(ActionListener l) { if (actionListeners != null && actionListeners.contains(l)) { Vector v = (Vector) actionListeners.clone(); v.removeElement(l); actionListeners = v; } } // Generated by JBuilder public synchronized void addActionListener(ActionListener l) { Vector v = actionListeners == null ? new Vector(2) : (Vector) actionListeners.clone(); if (!v.contains(l)) { v.addElement(l); actionListeners = v; } } // Generated by JBuilder protected void fireActionPerformed(ActionEvent e) { if (actionListeners != null) { Vector listeners = actionListeners; int count = listeners.size(); for (int i = 0; i < count; i++) ((ActionListener) listeners.elementAt(i)).actionPerformed(e); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -