import java.applet.Applet;
import java.awt.Graphics;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import sun.audio.*;
//Power By HenseJz @FJUT
public class Myclock extends JFrame implements ActionListener{
JPanel panel1,panel2;
JButton button1,button2,button3,button4;
Choice choice1,choice2,choice3;
JLabel label1,label2,label3;
Timer timer;
static boolean frozen=false;
public static int hour=-1,minute=-1,second=-1;
public Myclock(String s) {
super(s);
timer=new Timer(1000,this);
panel1=new JPanel();
panel1.setBackground(new Color(128,138,135));
button1=new JButton("设定闹钟");
button1.setBackground(new Color(176,224,230));
button2=new JButton("取消闹钟");
panel1.add(button1);
panel1.add(button2);
label1=new JLabel("小时:"); //设定 定闹钟面板
int i;
choice1=new Choice();
choice1.setBackground(new Color(255,255,255));
for(i=0;i<24;i++){
if(i<10) choice1.add("0"+i);
else choice1.add(i+"");
}
label2=new JLabel("分钟:");
choice2=new Choice();
choice2.setBackground(new Color(255,255,255));
for(i=0;i<60;i++){
if(i<10) choice2.add("0"+i);
else choice2.add(""+i);
}
label3=new JLabel("秒:");
choice3=new Choice();
choice3.setBackground(new Color(255,255,255));
for(i=0;i<60;i++){
if(i<10) choice3.add("0"+i);
else choice3.add(""+i);
}
button3=new JButton("添加");
button3.setBackground(new Color(176,224,230));
button4=new JButton("取消");
panel2=new JPanel();
panel2.setLayout(new FlowLayout());
panel2.setBackground(new Color(192,192,192));
panel2.setPreferredSize(new Dimension(120,70));
panel2.add(label1);panel2.add(choice1);
panel2.add(label2);panel2.add(choice2);
panel2.add(label3);panel2.add(choice3);
panel2.add(button4);
panel2.add(button3);
getContentPane().add(panel1,BorderLayout.NORTH);
getContentPane().add(panel2,BorderLayout.SOUTH);
panel2.setVisible(false);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);
ClockPaint cp = new ClockPaint(30,30,120);
this.add(cp,BorderLayout.CENTER);
this.setBounds(460, 220, 300,450);
this.setResizable(false); //设置窗口不能调整大小
this.setVisible(true);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e){
if(e.getSource() == button1){ //设定button1功能
if(frozen == true){
Toolkit.getDefaultToolkit().beep();
int n = JOptionPane.showConfirmDialog(this, "早已经设定了闹钟啦!当前闹铃时间为:\n"+Myclock.hour+"时"+Myclock.minute+"分"+Myclock.second+"秒\n"+"\t是否要重设?","已有闹钟",JOptionPane.YES_NO_OPTION);
if(n == JOptionPane.YES_OPTION){
Toolkit.getDefaultToolkit().beep();
panel2.setVisible(true);
choice1.select(Myclock.hour);
choice2.select(Myclock.minute);
choice3.select(Myclock.second);
button1.removeActionListener(this);
}
else if(n == JOptionPane.NO_OPTION){
Toolkit.getDefaultToolkit().beep();
}
}
else{
Toolkit.getDefaultToolkit().beep();
panel2.setVisible(true);
choice1.select("12");
choice2.select("30");
choice3.select("30");
//button1.removeActionListener(this); //添加会产生BUG,为在未设定闹铃时当点击button1后然后点击button2选择否再点击button1无效。
}
}
else if(e.getSource() == button2){ //设定button2功能
panel2.setVisible(false);
Toolkit.getDefaultToolkit().beep();
if(frozen == false){
int n = JOptionPane.showConfirmDialog(this,"对不起,当前您尚未设定闹钟,是否开始设定闹钟?","尚未设定闹钟",JOptionPane.YES_NO_OPTION);
if(n == JOptionPane.YES_OPTION){
Toolkit.getDefaultToolkit().beep();
panel2.setVisible(true);
choice1.select(Myclock.hour);
choice2.select(Myclock.minute);
choice3.select(Myclock.second);
//button1.removeActionListener(this);
}
else if(n == JOptionPane.NO_OPTION){
Toolkit.getDefaultToolkit().beep();
panel2.setVisible(false);
}
}
else{
Toolkit.getDefaultToolkit().beep();
frozen=false;
hour=-1;minute=-1;second=-1;
AudioPlayer.player.stop(ClockPaint.gg);
ClockPaint.nn=false;
JOptionPane.showMessageDialog(this,"闹钟已解除","提示对话框",JOptionPane.INFORMATION_MESSAGE);
}
}
else if(e.getSource()==button3){ //设定button3功能
int x1=-1,x2=-1,x3=-1;
Toolkit.getDefaultToolkit().beep();
try{
Toolkit.getDefaultToolkit().beep();
x1=Integer.parseInt(choice1.getSelectedItem());
x2=Integer.parseInt(choice2.getSelectedItem());
x3=Integer.parseInt(choice3.getSelectedItem());
}catch(Exception exc){
Toolkit.getDefaultToolkit().beep();
JOptionPane.showMessageDialog(this,"请输入合法时间","提示对话框",JOptionPane.INFORMATION_MESSAGE);
}
hour=x1;
minute=x2;
second=x3;
panel2.setVisible(false);
frozen=true;
button1.addActionListener(this);
Toolkit.getDefaultToolkit().beep();
JOptionPane.showMessageDialog(this,"闹钟设定完成!","提示对话框",JOptionPane.INFORMATION_MESSAGE);
}
else{ //设定button4功能
Toolkit.getDefaultToolkit().beep();
panel2.setVisible(false);
button1.addActionListener(this);
}
}
public static void main(String[] s) {
new Myclock("我的小闹钟");
}
}
class ClockPaint extends JPanel implements Runnable {
InputStream in;
AudioData data;
AudioStream as;
public static ContinuousAudioDataStream gg;
int x, y, r;
int h, m, s;// 小时,分钟,秒
final double rad = Math.PI/ 180;
public static boolean nn=false; //闹钟到了变为true
public ClockPaint(int x, int y, int r) {
this.x = x;
this.y = y;
this.r = r;
Thread t = new Thread(this);
t.start();
}
public void paint(Graphics g) {
Calendar now=new GregorianCalendar();
s = now.get(Calendar.SECOND) * 6;// 获得秒转换成度数
m = now.get(Calendar.MINUTE) * 6;// 获得分钟
h = (now.get(Calendar.HOUR_OF_DAY) - 12) * 30
+ now.get(Calendar.MINUTE) / 12 * 6;// 获得小时
// 清屏
super.paint(g);
g.setColor(Color.black);
g.fillRect(0,0, r * 4, r *4);
// 画圆
g.setColor(Color.black);
Graphics2D g2d=(Graphics2D)g;
//秒针末端的绿点
int x1 = (int) ((r - 5) * Math.sin(rad * s));
int y1 = (int) ((r - 5) * Math.cos(rad * s));
g.setColor(Color.green);
g.fillOval(x + r + x1- 1, y + r - y1- 1,4,4);
// 分针
Stroke stroke=new BasicStroke(0.0f);//设置线宽为0.0
g2d.setStroke(stroke);
g.setColor(Co