代码复制就可运行,音频路径和文件更改为自己的即可。
import sun.audio.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.InputStream;
import java.util.Calendar;
public class JavaClock extends JFrame implements ActionListener, Runnable {
private static final long serialVersionUID = -7303693253632593767L;
Thread thread;
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
JButton button2 = new JButton("开闹铃");
JButton button1 = new JButton("确定");
JComboBox sComboBox, mComboBox, hComboBox;
JLabel label1 = new JLabel(":");
JLabel label2 = new JLabel(":");
JLabel label3 = new JLabel("设置闹铃时间:");
JLabel label4 = new JLabel("闹铃状态:关");
Clock clock = new Clock();
boolean flag = false;
int hour = 0, minute = 0, second = 0;
int nowsecond, nowminute, nowhour;
AudioData theData = null;
AudioDataStream nowPlaying = null;
public JavaClock() {
setTitle("闹钟");
setResizable(false);// 用户不能调整大小
setLocation(450, 100);// 在屏幕位置
setSize(400, 500);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);// 按窗口关闭按钮时退出程序
setLayout(new BorderLayout());
add(clock, BorderLayout.NORTH);
sComboBox = new JComboBox();
mComboBox = new JComboBox();
hComboBox = new JComboBox();
for (int i = 0; i < 60; i++) {
sComboBox.addItem(i);
mComboBox.addItem(i);
}
for (int i = 0; i < 24; i++) {
hComboBox.addItem(i);
}
button1.addActionListener(this);
button2.addActionListener(this);
label3.setFont(new Font("楷体_GB2312", Font.BOLD, 18));
panel1.add(label3);
panel1.add(hComboBox);
panel1.add(label1