package Timer;
import java.awt.*;
import javax.swing.*;
//import java.util.*;
//import java.io.*;
import java.awt.event.*;
import sun.audio.*;
public class time extends JFrame {
private static final long serialVersionUID = -2397593626990759111L;
JLabel jhour, jminute, jseconds;
JTextField jhouri, jminutei, jsecondsi;
Checkbox jmemo, jshutdown, jsleep;
CheckboxGroup jg;
JTextArea memoinput;
JButton jok;
PopupMenu jp;
MenuItem jshow, jexit;
TrayIcon trayicon;
SystemTray tray;
JMenuBar jmb;
JMenu menu, help;
JMenuItem jquit, jhelp;
int ms;
int type = 1;
public time() {
try {
UIManager
.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (Exception e) {
e.getStackTrace();
}
setTitle("定时器");
setSize(300, 200);
setLocationRelativeTo(null);
setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
addWindowListener(new WindowDestroyer());
java.net.URL img = time.class.getResource("time1.gif");
setIconImage(new ImageIcon(img).getImage());
setResizable(false);
jhour = new JLabel("小时 :", SwingConstants.CENTER);
jminute = new JLabel("分钟 :", SwingConstants.CENTER);
jseconds = new JLabel("秒钟 :", SwingConstants.CENTER);
jhouri = new JTextField(4);
jminutei = new JTextField(4);
jsecondsi = new JTextField(4);
jhouri.setText("0");
jsecondsi.setText("0");
jminutei.setText("0");
jg = new CheckboxGroup();
jmemo = new Checkbox("备忘", true, jg);
jshutdown = new Checkbox("关机", false, jg);
jsleep = new Checkbox("休眠", false, jg);
memoinput = new JTextArea(3, 15);
memoinput.setLineWrap(true);
jok = new JButton("确定");
jp = new PopupMenu();
jshow = new MenuItem("显示窗口");
jexit = new MenuItem("退出");
jp.add(jshow);
jp.add(jexit);
tray = SystemTray.getSystemTray();
trayicon = new TrayIcon(new ImageIcon(img).getImage(), "计时器", jp);
jmb = new JMenuBar();
menu = new JMenu("菜单");
help = new JMenu("帮助");
jquit = new JMenuItem("关闭任务并退出");
jhelp = new JMenuItem("有关说明");
JMenu about = new JMenu("关于");
JMenuItem jabout = new JMenuItem("关于定时器");
about.add(jabout);
menu.add(jquit);
help.add(jhelp);
jmb.add(menu);
jmb.add(help);
jmb.add(about);
setJMenuBar(jmb);
JScrollPane js = new JScrollPane(memoinput);
Container con = getContentPane();
Box horbox1 = Box.createHorizontalBox();
horbox1.add(Box.createHorizontalStrut(10));
horbox1.add(jhour);
horbox1.add(Box.createHorizontalStrut(70));
horbox1.add(jminute);
horbox1.add(Box.createHorizontalStrut(60));
horbox1.add(jseconds);
horbox1.add(Box.createHorizontalStrut(10));
Box horbox2 = Box.createHorizontalBox();
horbox2.add(Box.createHorizontalStrut(10));
horbox2.add(jmemo);
horbox2.add(Box.createHorizontalStrut(10));
horbox2.add(jshutdown);
horbox2.add(Box.createHorizontalStrut(10));
horbox2.add(jsleep);
horbox2.add(Box.createHorizontalStrut(10));
Box horbox3 = Box.createHorizontalBox();
horbox3.add(Box.createHorizontalStrut(10));
horbox3.add(js);
horbox3.add(Box.createHorizontalStrut(10));
Box horbox4 = Box.createHorizontalBox();
horbox4.add(Box.createHorizontalStrut(10));
horbox4.add(jhouri);
horbox4.add(Box.createHorizontalStrut(30));
horbox4.add(jminutei);
horbox4.add(Box.createHorizontalStrut(30));
horbox4.add(jsecondsi);
horbox4.add(Box.createHorizontalStrut(10));
Box horbox5 = Box.createHorizontalBox();
horbox5.add(Box.createHorizontalStrut(10));
horbox5.add(jok);
Box verbox = Box.createVerticalBox();
verbox.add(Box.createVerticalStrut(5));
verbox.add(horbox1);
verbox.add(Box.createVerticalStrut(5));
verbox.add(horbox4);
verbox.add(Box.createVerticalStrut(5));
verbox.add(horbox2);
verbox.add(Box.createVerticalStrut(5));
verbox.add(horbox3);
verbox.add(Box.createVerticalStrut(5));
verbox.add(horbox5);
con.add(verbox);
con.setBackground(new Color(192,192,192));
//
//
jmemo.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
type = 1;
}
});
jshutdown.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
type = 2;
memoinput.setText("");
memoinput.setEditable(false);
}
});
jsleep.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
type = 3;
memoinput.setText("");
memoinput.setEditable(false);
}
});
//
jok.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
ms = Integer.parseInt(jhouri.getText().trim()) * 60 * 60
* 1000
+ Integer.parseInt(jminutei.getText().trim()) * 60
* 1000
+ Integer.parseInt(jsecondsi.getText().trim())
* 1000;
new count(ms, type).start();
jok.setEnabled(false);
jhouri.setEditable(false);
jminutei.setEditable(false);
jsecondsi.setEditable(false);
if (type == 1) {
String info = "铃声将在" + jhouri.getText() + "小时"
+ jminutei.getText() + "分"
+ jsecondsi.getText() + "秒后响起";
JOptionPane.showMessageDialog(null, info);
}
if (type == 2) {
}
if (type == 3) {
String info = "计算机将在" + jhouri.getText() + "小时"
+ jminutei.getText() + "分"
+ jsecondsi.getText() + "秒后休眠或者待机";
JOptionPane.showMessageDialog(null, info);
}
} catch (Exception e1) {
JOptionPane.showMessageDialog(null, "时间输入错误!");
}
}
});
trayicon.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
tray.remove(trayicon);
setVisible(true);
}
}
});
jshow.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tray.remove(trayicon);
setVisible(true);
}
});
jexit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
jquit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
jhelp.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Help h = new Help();
h.setVisible(true);
}
});
jabout.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
About a = new About();
a.setVisible(true);
}
});
}
class count extends Thread {
private int innerms, innertype;
public count(int a, int b) {
innerms = a;
innertype = b;
}
public void run() {
if (innertype == 1) {
try {
sleep(innerms);
java.net.URL sou = count.class.getResource("ringin.wav");
AudioStream as = new AudioStream(sou.openStream());
AudioData asi = as.getData();
ContinuousAudioDataStream p = new ContinuousAudioDataStream(
asi);
AudioPlayer.player.start(p);
} catch (Exception e) {
e.getStackTrace();
}
} else if (innertype == 2) {
try {
String exe = "shutdown -s -t " + innerms / 1000;
Runtime.getRuntime().exec(exe);
} catch (Exception e) {
}
} else if (innertype == 3) {
try {
sleep(innerms);
Runtime.getRuntime().exec(
"rundll32 powrprof.dll, SetSuspendState");
} catch (Exception e) {
e.getStackTrace();
}
}
}
}
class WindowDestroyer extends WindowAdapter {
public void windowClosing(WindowEvent e) {
try {
tray.add(trayicon);
setVisible(false);
} catch (Exception e1) {
}
}
}
public static void main(String args[]) {
time t = new time();
t.setVisible(true);
}
}