急需要一个简单的JAVA闹钟程序

2020-10-27 教育 79阅读
你好,我费了一上午才给你写完,请看代码:
import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.awt.event.*;
import java.applet.AudioClip;
//
public class WakeUp extends JApplet implements Runnable, ActionListener {
DteTimePanel dtTimePane = new DteTimePanel();//时间面盘
JLabel lblWakeUpTime = new JLabel("唤醒时间");//唤醒时间
JTextField txtWakeUpTime = new JTextField("20:33", 5);//文本设置要唤醒的时刻和次数
JTextField txtSound = new JTextField("train.wav");//声音文件
JTextField txtSrchRadio =
new JTextField("http://www.radio-locator.com/");
JComboBox cboxPickRadio = new JComboBox();
JButton btnStartStop = new JButton("Start");
JButton btnSrchRadio = new JButton("Find");
JButton btnPlayRadio = new JButton("Play");
JTextField txtStatusInfo = new JTextField("状态消息");
AudioClip alarmSound;
Thread thrSound;
int ckHour;
int ckMin;
Calendar ckDate;
boolean doCheck = false;
public void init() {
alarmSound = getAudioClip(getCodeBase(), txtSound.getText());
JPanel panOuter = new JPanel();
GridLayout gridlay1 = new GridLayout(3, 1);
panOuter.setLayout(gridlay1);
panOuter.add(dtTimePane);
JPanel panWakeUpTime = new JPanel();
FlowLayout flow1 = new FlowLayout(FlowLayout.LEFT);
panWakeUpTime.setLayout(flow1);
panWakeUpTime.add(lblWakeUpTime);
panWakeUpTime.add(txtWakeUpTime);
JPanel panButtons = new JPanel();
GridLayout gridlay2 = new GridLayout(1, 3);
panButtons.setLayout(gridlay2);
panButtons.add(btnSrchRadio);
panButtons.add(btnPlayRadio);
btnStartStop.addActionListener(this);
panButtons.add(btnStartStop);
JPanel panSetAlarmTime = new JPanel();
GridLayout gridlay3 = new GridLayout(2, 2);
panSetAlarmTime.setLayout(gridlay3);
panSetAlarmTime.add(panWakeUpTime);
panSetAlarmTime.add(panButtons);
panSetAlarmTime.add(txtSound);
panSetAlarmTime.add(txtSrchRadio);
panOuter.add(panSetAlarmTime);
cboxPickRadio.addItem("radio station 1");
cboxPickRadio.addItem("radio station 2");
cboxPickRadio.addItem("radio station 3");
cboxPickRadio.setEditable(true);
JPanel panBottom = new JPanel();
GridLayout gridlay4 = new GridLayout(2, 1);
panBottom.setLayout(gridlay4);
panBottom.add(cboxPickRadio);
panBottom.add(txtStatusInfo);
panOuter.add(panBottom);
setContentPane(panOuter);
repaint();
}
public void start() {
String s1 = txtWakeUpTime.getText();
ckHour = Integer.parseInt(s1.substring(0, 2));
ckMin = Integer.parseInt(s1.substring(3, 5));
ckDate = new GregorianCalendar();
Calendar cal = new GregorianCalendar();
int mm = cal.get(Calendar.MONTH);
int dd = cal.get(Calendar.DAY_OF_MONTH);
int yy = cal.get(Calendar.YEAR);
ckDate.set(yy, mm, dd, ckHour, ckMin, 00);
if (ckDate.compareTo(cal) < 0) ckDate.add(Calendar.DATE, 1);
if (thrSound == null) {
thrSound = new Thread(this);
thrSound.start();
}
}
public void stop() {
doCheck = false;
if (thrSound != null) {
if (alarmSound != null)
alarmSound.stop();
thrSound = null;
}
}
public void run() {
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) { }
GregorianCalendar currtime = new GregorianCalendar();
if (doCheck)
if (ckDate.compareTo(currtime) < 0) {
runTheSound();
}
}
}
public void runTheSound() {
doCheck = false;
if (alarmSound != null)
alarmSound.loop();
}
public void actionPerformed(ActionEvent evt) {
if (evt.getSource().equals(btnStartStop)) {
if (btnStartStop.getText().equals("Start")) {
btnStartStop.setText("Stop");
txtStatusInfo.setText("Alarm process started");
doCheck = true;
start();
} else if (btnStartStop.getText().equals("Stop")) {
btnStartStop.setText("Start");
txtStatusInfo.setText("Alarm process has been stopped.");
stop();
}
}
}
}
class DteTimePanel extends JPanel {
String lastTime = "";
public void paint(Graphics screen) {
GregorianCalendar xday = new GregorianCalendar();
String xtime = xday.getTime().toString();
Color fgColor = new Color(40, 120, 160);
Font type = new Font("Monospaced", Font.BOLD, 20);
screen.setFont(type);
screen.setColor(getBackground());
screen.drawString(lastTime, 5, 15);
screen.setColor(fgColor);
screen.drawString(xtime, 5, 15);
lastTime = xtime;
repaint();
}
}
请记得采纳哦
声明:你问我答网所有作品(图文、音视频)均由用户自行上传分享,仅供网友学习交流。若您的权利被侵害,请联系fangmu6661024@163.com