package www.test.com;
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.TimeZone;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
// 这是功能键的添加及窗体的设置
public class Timezone extends JFrame implements ActionListener {
private static final long serialVersionUID = 2143849358452364853L;
private JMenuBar bar = new JMenuBar();
private JMenu menu1 = new JMenu("功能");
private JMenu menu2 = new JMenu("关于");
private JMenuItem item1 = new JMenuItem("设定时间");
private JMenuItem item2 = new JMenuItem("闹钟");
private JMenuItem item3 = new JMenuItem("日历");
private JMenuItem item5 = new JMenuItem("记事本");
private JMenuItem item6 = new JMenuItem("退出");
private ClockPanel cPanel = null;
public Timezone() {
cPanel = new ClockPanel();
this.add(cPanel);
this.setTitle("经典时钟");
this.setSize(600, 400);
this.setVisible(true);
this.setJMenuBar(bar);
bar.add(menu1);
bar.add(menu2);
menu1.add(item1);
menu1.add(item2);
menu1.add(item3);
menu1.add(item5);
menu1.add(item6);
item1.addActionListener(this);
item2.addActionListener(this);
item3.addActionListener(this);
item5.addActionListener(this);
item6.addActionListener(this);
}
public static void main(String[] args) {
Timezone dlg = new Timezone();
dlg.setLocation(300, 130);// 桌面位置
dlg.setSize(1000, 450);// 长宽
dlg.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
dlg.setVisible(true);
dlg.setResizable(false);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(item1)) {
// System.out.println("点击鼠标,设置时间!");
SetupDialog setupdialog = new SetupDialog(cPanel);
setupdialog.setEnabled(true);//.show();
// JFrame frame = new JFrame("Calendar Application");
// frame.show();
} else if (e.getSource().equals(item2)) {
new Alarm(cPanel).setVisible(true);
} else if (e.getSource().equals(item3)) {
}
else if (e.getSource().equals(item5)) {
} else if (e.getSource().equals(item6)) {
System.exit(0);
}
}
}
class ClockPanel extends JPanel implements Runnable {
private static final long serialVersionUID = -2341365795573288466L;
private Point one = new Point(150, 200);// 设置圆心点的坐标
private Point origin = new Point(500, 200);// 设置圆心点的坐标
private Point three = new Point(850, 200);// 设置圆心点的坐标
private int archCN, arcmCN, arcsCN;// 定义了三个角度的变量
private int archUS, arcmUS, arcsUS;
private int archUK, arcmUK, arcsUK;
private final static String CN_TIME_ZONE = "Asia/Shanghai";
private final static String US_TIME_ZONE = "America/Chicago";//"America/New_York"
private final static String UK_TIME_ZONE = "Europe/London";
/**
* 定义了 hour, minute, second三个变量, 从arcs = 360 - (second - 15) * 6; arcm = 360
* - (minute - 15) * 6; arch = (360 - (hour * 5 - 15) * 6)-(minute/6)*3;
* 可以判断出是为了把系统当前时间初始化给这三个变量, 并转化成角度
*/
int hour, minute, second;
Calendar cal = null;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
/**
* 构造方法c是为了实现获取系统当前时间, 并分别赋值给 hour, minute, second 三个变量,然后转换成角度 (总的是为了实现角度和
* hour, minute, second的用处)
*/
public ClockPanel() {
/**
* 获取系统当前时间的年月日,小时,分钟,秒,并复制给变量
*/
cal = Calendar.getInstance();// 用于表示日历的设置
/**
* 创建线程,线程是为了实现钟表里的针 每个多久转一下
*/
Thread thread = new Thread(this);
/**
* 把三个变量hour, minute, second转化成角度
*/
cal.setTimeZone(TimeZone.getTimeZone(CN_TIME_ZONE));
hour = cal.get(Calendar.HOUR); // 获取日历上的小时
minute = cal.get(Calendar.MINUTE);
second = cal.get(Calendar.SECOND);
arcsCN = 360 - (second - 15) * 6; // 15秒时等于0度
arcmCN = 360 - (minute - 15) * 6;
archCN = (360 - (hour * 5 - 15) * 6) - (minute / 6) * 3;
cal.setTimeZone(TimeZone.getTimeZone(US_TIME_ZONE));
hour = cal.get(Calendar.HOUR); // 获取日历上的小时
minute = cal.get(Calendar.MINUTE);
second = cal.get(Calendar.SECOND);
arcsUS = 360 - (second - 15) * 6; // 15秒时等于0度
arcmUS = 360 - (minute - 15) * 6;
archUS = (360 - (hour * 5 - 15) * 6) - (minute / 6) * 3;
cal.setTimeZone(TimeZone.getTimeZone(UK_TIME_ZONE));
hour = cal.get(Calendar.HOUR); // 获取日历上的小时
minute = cal.get(Calendar.MINUTE);
second = cal.get(Calendar.SECOND);
arcsUK = 360 - (second - 15) * 6; // 15秒时等于0度
arcmUK = 360 - (minute - 15) * 6;
archUK = (360 - (hour * 5 - 15) * 6) - (minute / 6) * 3;
thread.start();// 开始线程
}
public void setTime(Calendar cal) {
if (cal == null)
return;
this.cal = cal;
cal.setTimeZone(TimeZone.getTimeZone(CN_TIME_ZONE));
hour = cal.get(Calendar.HOUR); // 获取日历上的小时
minute = cal.get(Calendar.MINUTE);
second = cal.get(Calendar.SECOND);
arcsCN = 360 - (second - 15) * 6; // 15秒时等于0度
arcmCN = 360 - (minute - 15) * 6;
archCN = (360 - (hour * 5 - 15) * 6) - (minute / 6) * 3;
cal.setTimeZone(TimeZone.getTimeZone(US_TIME_ZONE));
hour = cal.get(Calendar.HOUR); // 获取日历上的小时
minute = cal.get(Calendar.MINUTE);
second = cal.get(Calendar.SECOND);
arcsUS = 360 - (second - 15) * 6; // 15秒时等于0度
arcmUS = 360 - (minute - 15) * 6;
archUS = (360 - (hour * 5 - 15) * 6) - (minute / 6) * 3;
cal.setTimeZone(TimeZone.getTimeZone(UK_TIME_ZONE));
hour = cal.get(Calendar.HOUR); // 获取日历上的小时
minute = cal.get(Calendar.MINUTE);
second = cal.get(Calendar.SECOND);
arcsUK = 360 - (second - 15) * 6; // 15秒时等于0度
arcmUK = 360 - (minute - 15) * 6;
archUK = (360 - (hour * 5 - 15) * 6) - (minute / 6) * 3;
this.repaint();
}
public Calendar getCurrentTime() {
cal.setTimeZone(TimeZone.getTimeZone(CN_TIME_ZONE));
return cal;
}
/**
* 重写窗体
*/
@Override
/**
* 实现画线,画刻度(一个钟,
* 要有刻度,针,圆等,这些需要画上的)
*/
protected void paintComponent(Graphics g) {
super.paintComponent(g);
this.setBackground(Color.GRAY);
g.drawOval(725, 75, 250, 250);
g.drawOval(725, 76, 251, 250);
g.drawOval(725, 77, 252, 250);
g.drawOval(25, 75, 250, 250);
g.drawOval(25, 76, 251, 250);
g.drawOval(25, 77, 251, 250);
g.drawOval(375, 75, 250, 250);
g.drawOval(375, 76, 251, 250);
g.drawOval(375, 77, 252, 250);
for (int i = 0; i < 60; i++) {
/**
* 几点与几点之间的刻度划分
*/
if (i % 5 == 0)
g.setColor(Color.RED);// 设置秒针的颜色
/**
* 画秒针的线
*/
g.drawLine(
origin.x
+ ((int) (0.95 * 120 * Math.cos((6 * i) * Math.PI
/ 180))),
origin.y
+ ((int) (-120 * 0.95 * Math.sin((6 * i) * Math.PI
/ 180))),
origin.x
+ ((int) (120 * 1.05 * Math.cos((6 * i) * Math.PI
/ 180))),
origin.y
+ ((int) (-120 * 1.05 * Math.sin((6 * i) * Math.PI
/ 180))));
g.setColor(Color.BLACK);
g.drawLine(
one.x
+ ((int) (0.95 * 120 * Math.cos((6 * i) * Math.PI
/ 180))),
one.y
+ ((int) (-120 * 0.95 * Math.sin((6 * i) * Math.PI
/ 180))),
one.x
+ ((int) (120 * 1.05 * Math.cos((6 * i) * Math.PI
/ 180))),
one.y
+ ((int) (-120 * 1.05 * Math.sin((6 * i) * Math.PI
/ 180))));
g.setColor(Color.BLACK);
g.drawLine(
three.x
+ ((int) (0.95 * 120 * Math.cos((6 * i) * Math.PI
/ 180))),
three.y
+ ((int) (-120 * 0.95 * Math.sin((6 * i) * Math.PI
/ 180))),
three.x
+ ((int) (120 * 1.05 * Math.cos((6 * i) * Math.PI
/ 180))),
three.y
+ ((int) (-120 * 1.05 * Math.sin((6 * i) * Math.PI
/ 180))));
g.setColor(Color.BLACK);
}
g.fillRect(
one.x + ((int) (0.95 * 120 * Math.cos(90 * Math.PI / 180) - 3)),
one.y
+ ((int) (-120 * 0.92 * Math.sin(90 * Math.PI / 180) - 16)),
6, 18);
g.fillRect(one.x
+ ((int) (0.95 * 120 * Math.cos(270 * Math.PI / 180) - 3)),
one.y + ((int) (-120 * 0.92 * Math.sin(270 * Math.PI / 180))),
6, 18);
g.fillRect(
one.x + ((int) (0.92 * 120 * Math.cos(0 * Math.PI / 180))),
one.y + ((int) (-120 * 0.97 * Math.sin(0 * Math.PI / 180) - 3)),
18, 6);
g.fillRect(
one.x// 0.92是移动刻度的位置
+ ((int) (0.92 * 120 * Math.cos(180 * Math.PI / 180) - 16)),
one.y
+ ((int) (-120 * 0.97 * Math.sin(180 * Math.PI / 180) - 3)),
18, 6);
// g.setFont(new Font("宋体", Font.BOLD, 20));
g.drawString(
"12",
one.x + ((int) (0.8 * 120 * Math.cos(90 * Math.PI / 180) - 12)),
one.y + ((int) (-120 * 0.8 * Math.sin(90 * Math.PI / 180))));
g.drawString("3",
one.x + ((int) (0.8 * 120 * Math.cos(0 * Math.PI / 180))),
one.y + ((int) (-120 * 0.8 * Math.sin(0 * Math.PI / 180) +
));
g.drawString(
"6",
one.x + ((int) (0.8 * 120 * Math.cos(270 * Math.PI / 180) - 6)),
one.y
+ ((int) (-120 * 0.8 * Math.sin(270 * Math.PI / 180) + 10)));
g.drawString(
"9",
one.x + ((int) (0.8 * 120 * Math.cos(180 * Math.PI / 180) -
),
one.y
+ ((int) (-120 * 0.8 * Math.sin(180 * Math.PI / 180) +
));
g.setColor(Color.RED);
g.drawLine(one.x, one.y,
one.x + ((int) (120 * Math.cos((arcsUS) * Math.PI / 180))),
one.y + ((int) (-120 * Math.sin((arcsUS) * Math.PI / 180))));
g.setColor(Color.GREEN);
g.drawLine(one.x, one.y,
one.x + ((int) (90 * Math.cos((arcmUS) * Math.PI / 180))),
one.y + ((int) (-90 * Math.sin((arcmUS) * Math.PI / 180))));
g.setColor(Color.BLACK);
g.drawLine(one.x, one.y,
one.x + ((int) (60 * Math.cos((archUS) * Math.PI / 180))),
one.y + ((int) (-60 * Math.sin((archUS) * Math.PI / 180))));
format.setTimeZone(TimeZone.getTimeZone(US_TIME_ZONE));
g.drawString("美国 : " + format.format(cal.getTime()), 50, 350);// 文字的显示
g.fillRect(
origin.x
+ ((int) (0.95 * 120 * Math.cos(90 * Math.PI / 180) - 3)),
origin.y
+ ((int) (-120 * 0.92 * Math.sin(90 * Math.PI / 180) - 16)),
6, 18);
g.fillRect(
origin.x
+ ((int) (0.95 * 120 * Math.cos(270 * Math.PI / 180) - 3)),
origin.y
+ ((int) (-120 * 0.92 * Math.sin(270 * Math.PI / 180))),
6, 18);
g.fillRect(origin.x
+ ((int) (0.92 * 120 * Math.cos(0 * Math.PI / 180))), origin.y
+ ((int) (-120 * 0.97 * Math.sin(0 * Math.PI / 180) - 3)), 18,
6);
g.fillRect(
origin.x// 0.92是移动刻度的位置
+ ((int) (0.92 * 120 * Math.cos(180 * Math.PI / 180) - 16)),
origin.y
+ ((int) (-120 * 0.97 * Math.sin(180 * Math.PI / 180) - 3)),
18, 6);
// g.setFont(new Font("宋体", Font.BOLD, 20));
g.drawString(
"12",
origin.x
+ ((int) (0.8 * 120 * Math.cos(90 * Math.PI / 180) - 12)),
origin.y + ((int) (-120 * 0.8 * Math.sin(90 * Math.PI / 180))));
g.drawString(
"3",
origin.x + ((int) (0.8 * 120 * Math.cos(0 * Math.PI / 180))),
origin.y
+ ((int) (-120 * 0.8 * Math.sin(0 * Math.PI / 180) +
));
g.drawString(
"6",
origin.x
+ ((int) (0.8 * 120 * Math.cos(270 * Math.PI / 180) - 6)),
origin.y
+ ((int) (-120 * 0.8 * Math.sin(270 * Math.PI / 180) + 10)));
g.drawString(
"9",
origin.x
+ ((int) (0.8 * 120 * Math.cos(180 * Math.PI / 180) -
),
origin.y
+ ((int) (-120 * 0.8 * Math.sin(180 * Math.PI / 180) +
));
g.setColor(Color.RED);
g.drawLine(origin.x, origin.y,
origin.x + ((int) (120 * Math.cos((arcsCN) * Math.PI / 180))),
origin.y + ((int) (-120 * Math.sin((arcsCN) * Math.PI / 180))));
g.setColor(Color.GREEN);
g.drawLine(origin.x, origin.y,
origin.x + ((int) (90 * Math.cos((arcmCN) * Math.PI / 180))),
origin.y + ((int) (-90 * Math.sin((arcmCN) * Math.PI / 180))));
g.setColor(Color.BLACK);
g.drawLine(origin.x, origin.y,
origin.x + ((int) (60 * Math.cos((archCN) * Math.PI / 180))),
origin.y + ((int) (-60 * Math.sin((archCN) * Math.PI / 180))));
format.setTimeZone(TimeZone.getTimeZone(CN_TIME_ZONE));
g.drawString("中国 : " + format.format(cal.getTime()), 400, 350);// 文字的显示
g.fillRect(
three.x
+ ((int) (0.95 * 120 * Math.cos(90 * Math.PI / 180) - 3)),
three.y
+ ((int) (-120 * 0.92 * Math.sin(90 * Math.PI / 180) - 16)),
6, 18);
g.fillRect(
three.x
+ ((int) (0.95 * 120 * Math.cos(270 * Math.PI / 180) - 3)),
three.y + ((int) (-120 * 0.92 * Math.sin(270 * Math.PI / 180))),
6, 18);
g.fillRect(
three.x + ((int) (0.92 * 120 * Math.cos(0 * Math.PI / 180))),
three.y
+ ((int) (-120 * 0.97 * Math.sin(0 * Math.PI / 180) - 3)),
18, 6);
g.fillRect(
three.x// 0.92是移动刻度的位置
+ ((int) (0.92 * 120 * Math.cos(180 * Math.PI / 180) - 16)),
three.y
+ ((int) (-120 * 0.97 * Math.sin(180 * Math.PI / 180) - 3)),
18, 6);
// g.setFont(new Font("宋体", Font.BOLD, 20));
g.drawString(
"12",
three.x
+ ((int) (0.8 * 120 * Math.cos(90 * Math.PI / 180) - 12)),
three.y + ((int) (-120 * 0.8 * Math.sin(90 * Math.PI / 180))));
g.drawString(
"3",
three.x + ((int) (0.8 * 120 * Math.cos(0 * Math.PI / 180))),
three.y
+ ((int) (-120 * 0.8 * Math.sin(0 * Math.PI / 180) +
));
g.drawString(
"6",
three.x
+ ((int) (0.8 * 120 * Math.cos(270 * Math.PI / 180) - 6)),
three.y
+ ((int) (-120 * 0.8 * Math.sin(270 * Math.PI / 180) + 10)));
g.drawString(
"9",
three.x
+ ((int) (0.8 * 120 * Math.cos(180 * Math.PI / 180) -
),
three.y
+ ((int) (-120 * 0.8 * Math.sin(180 * Math.PI / 180) +
));
g.setColor(Color.RED);
g.drawLine(three.x, three.y,
three.x + ((int) (120 * Math.cos((arcsUK) * Math.PI / 180))),
three.y + ((int) (-120 * Math.sin((arcsUK) * Math.PI / 180))));
g.setColor(Color.GREEN);
g.drawLine(three.x, three.y,
three.x + ((int) (90 * Math.cos((arcmUK) * Math.PI / 180))),
three.y + ((int) (-90 * Math.sin((arcmUK) * Math.PI / 180))));
g.setColor(Color.BLACK);
g.drawLine(three.x, three.y,
three.x + ((int) (60 * Math.cos((archUK) * Math.PI / 180))),
three.y + ((int) (-60 * Math.sin((archUK) * Math.PI / 180))));
format.setTimeZone(TimeZone.getTimeZone(UK_TIME_ZONE));
g.drawString("英国 : " + format.format(cal.getTime()), 750, 350);// 文字的显示
}
/**
* 重写接口,实现钟表的转动
*/
@Override
/**
*普通方法, 实现钟表的转动
*/
public void run() {
/**
* while循环的作用, 打个比方:时钟上的0点就是12点,同时是为了实现归0
*/
while (true) {
if (arcsCN == 0)
arcsCN = 360;
if (arcmCN == 0)
arcmCN = 360;
if (archCN == 0)
archCN = 360;
if (arcsUK == 0)
arcsUK = 360;
if (arcmUK == 0)
arcmUK = 360;
if (archUK == 0)
archUK = 360;
if (arcsUS == 0)
arcsUS = 360;
if (arcmUS == 0)
arcmUS = 360;
if (archUS == 0)
archUS = 360;
/**
* try是为了实现钟表转动的核心代码
*/
try {
/**
* 线程每1秒,停一下,等于秒针转动一下
*/
Thread.sleep(1000);
cal.set(Calendar.SECOND, cal.get(Calendar.SECOND) + 1);
/**
* 实现时分秒三个针的走动
*/
arcsCN = Math.abs(arcsCN - 6) % 360;// 走到下一刻的角度位置
if (arcsCN == 90)
arcmCN = Math.abs(arcmCN - 6) % 360;
if ((arcmCN - 54) % 36 == 0 && arcsCN == 90)
archCN = Math.abs(archCN - 3) % 360;
arcsUK = Math.abs(arcsUK - 6) % 360;// 走到下一刻的角度位置
if (arcsUK == 90)
arcmUK = Math.abs(arcmUK - 6) % 360;
if ((arcmUK - 54) % 36 == 0 && arcsUK == 90)
archUK = Math.abs(archUK - 3) % 360;
arcsUS = Math.abs(arcsUS - 6) % 360;// 走到下一刻的角度位置
if (arcsUS == 90)
arcmUS = Math.abs(arcmUS - 6) % 360;
if ((arcmUS - 54) % 36 == 0 && arcsUS == 90)
archUS = Math.abs(archUS - 3) % 360;
}
/**
* 处理异常问题的代码
*/
catch (InterruptedException e) {
e.printStackTrace();
}
this.repaint();// 异常处理完后,重亲执行核心代码和重画
}
}
}
class Alarm extends JDialog implements ActionListener {
private static final long serialVersionUID = 1L;
JLabel ri, shi, fen, miao, dangqian;
JButton queding, dakai;
JTextField music, RI, SHI, FEN, MIAO;
int h = 0, f = 0, m = 0, r = 0;
boolean fo = false;
public AudioClip soumd1;
ClockPanel cPanel = null;
public Alarm(ClockPanel cPanel) {
this.cPanel = cPanel;
Container c = getContentPane();
c.setLayout(new GridLayout(3, 1));
JPanel jp = new JPanel();
dangqian = new JLabel();
jp.add(dangqian);
c.add(jp);
JPanel jp1 = new JPanel();
music = new JTextField(20);
dakai = new JButton("选择闹铃音乐");
jp1.add(music);
jp1.add(dakai);
c.add(jp1);
ri = new JLabel("日");
RI = new JTextField(4);
shi = new JLabel("时");
SHI = new JTextField(4);
fen = new JLabel("分");
FEN = new JTextField(4);
miao = new JLabel("秒");
MIAO = new JTextField(4);
JPanel jp2 = new JPanel();
jp2.add(ri);
jp2.add(RI);
jp2.add(shi);
jp2.add(SHI);
jp2.add(fen);
jp2.add(FEN);
jp2.add(miao);
jp2.add(MIAO);
queding = new JButton("确定");
jp2.add(queding);
c.add(jp2);
setSize(400, 130);
setVisible(true);
dakai.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
JFileChooser fileChooser = new JFileChooser(); // 实例化文件选择器
fileChooser
.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); // 设置文件选择模式,此处为文件和目录均可
fileChooser.setCurrentDirectory(new File(".")); // 设置文件选择器当前目录
fileChooser
.setFileFilter(new javax.swing.filechooser.FileFilter() {
public boolean accept(File file) { // 可接受的文件类型
String name = file.getName().toLowerCase();
return name.endsWith(".wav")
|| name.endsWith(".au")
|| file.isDirectory();
}
public String getDescription() { // 文件描述
return "音乐文件(*.au)";
}
});
if (fileChooser.showOpenDialog(Alarm.this) == JFileChooser.APPROVE_OPTION) { // 弹出文件选择器,并判断是否点击了打开按钮
String fileName = fileChooser.getSelectedFile()
.getAbsolutePath(); // 得到选择文件或目录的绝对路径
music.setText(fileName);
}
}
});
queding.addActionListener(new ActionListener() {
Task task = null;
public void actionPerformed(ActionEvent event) {
if (queding.getText().equals("确定")) {
try {
r = Integer.parseInt(RI.getText());
h = Integer.parseInt(SHI.getText());
f = Integer.parseInt(FEN.getText());
m = Integer.parseInt(MIAO.getText());
System.out.println("闹钟时间 : " + r + "日," + h + ":" + f
+ ":" + m);
if (1 <= r && r <= 31 && 0 <= h && h <= 23 && 0 <= f
&& f <= 59 && 0 <= m && m <= 59) {
fo = true;
soumd1 = Applet.newAudioClip(new File(music
.getText()).toURI().toURL()); // 播放音乐
task = new Task();
task.start();
} else
JOptionPane.showMessageDialog(null, "输入时间错误");
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "请输入正确的时间");
}
} else {
task.shutdown();
fo = false;
RI.setEditable(true);
SHI.setEditable(true);
FEN.setEditable(true);
MIAO.setEditable(true);
queding.setText("确定");
soumd1.stop();
}
}
});
}
private class Task extends Thread {
boolean isShutDown = false;
public void run() {
RI.setEditable(false);
SHI.setEditable(false);
FEN.setEditable(false);
MIAO.setEditable(false);
queding.setText("关闭");
while (!isShutDown) {
Calendar curTime = cPanel.getCurrentTime();
dangqian.setText("当前时间: " + curTime.getTime().toString());
if (fo) {
int riqi = curTime.get(Calendar.DAY_OF_MONTH); // 获取日期
int shizhong = curTime.get(Calendar.HOUR_OF_DAY); // 获取小时
int fenzhong = curTime.get(Calendar.MINUTE); // 获取分钟
int miaozhong = curTime.get(Calendar.SECOND); // 获取秒钟
System.out.println("当前时间: " + riqi + "日" + shizhong + ":"
+ fenzhong + ":" + miaozhong + ", 闹钟时间 : " + r
+ "日" + h + ":" + f + ":" + m);
if (riqi == r && shizhong == h && fenzhong == f
&& miaozhong == m) // 判断条件
{
try {
System.out.println("闹铃开始...");
soumd1.loop(); // 我设置的是循环播放..这样不起床都不行..
fo = false;
} catch (Exception e) {
e.printStackTrace();
}
}
}
try {
Thread.sleep(1000);
} catch (InterruptedException ie) {
}
}
}
public void shutdown() {
isShutDown = true;
}
}
@Override
public void actionPerformed(ActionEvent arg0) {
}
}
class SetupDialog extends JDialog implements ActionListener {
private static final long serialVersionUID = 1222513399477180578L;
private JPanel panel1 = new JPanel();
private JPanel panel2 = new JPanel();
private JLabel label1 = new JLabel("时钟:");
private JLabel label2 = new JLabel("分钟:");
private JLabel label3 = new JLabel("秒钟:");
private JTextField hour = new JTextField("");
private JTextField minute = new JTextField("");
private JTextField second = new JTextField("");
static JButton button1 = new JButton("确定");
static JButton button2 = new JButton("取消");
private ClockPanel cPanel = null;
SetupDialog(ClockPanel cPanel) {
this.iuit();
this.setTitle("设置");
this.setSize(300, 100);
this.setVisible(true);
this.cPanel = cPanel;
button1.addActionListener(this);
button2.addActionListener(this);
}
public void iuit() {
this.setLayout(new BorderLayout());
this.add(panel1, BorderLayout.CENTER);
this.add(panel2, BorderLayout.SOUTH);
panel1.setLayout(new GridLayout(2, 3));
panel1.add(label1);
panel1.add(label2);
panel1.add(label3);
panel1.add(hour);
panel1.add(minute);
panel1.add(second);
panel2.add(button1);
panel2.add(button2);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(button1)) {
this.setVisible(true);
if ((hour.getText().equals("") || minute.getText().equals("") || second
.getText().equals(""))) {
JOptionPane.showMessageDialog(null, "你没输入时间,请输入时间");
} else {
int h = Integer.parseInt(hour.getText());
int m = Integer.parseInt(minute.getText());
int s = Integer.parseInt(second.getText());
Calendar curTime = SetupDialog.this.cPanel.getCurrentTime();
curTime.set(curTime.get(Calendar.YEAR),
curTime.get(Calendar.MONTH),
curTime.get(Calendar.DAY_OF_MONTH), h, m, s);
SetupDialog.this.cPanel.setTime(curTime);
}
} else if (e.getSource().equals(button2)) {
this.setVisible(false);
SetupDialog.this.hour.setText("");
SetupDialog.this.minute.setText("");
SetupDialog.this.second.setText("");
}
}
}