import java.awt.*;
import java.awt.event.*;
import java.text.*;
import java.util.*;
import java.util.Timer;
import javax.swing.*;
public class Clock extends JFrame{
public static int hour = 0 ;
public static int min = 0 ;
public static int sec = 0 ;
Choice ChoiceHour;
Choice ChoiceMin;
Choice ChoiceSec;
JLabel labelHour ;
JLabel labelMin ;
JLabel labelSec ;
JLabel labelHour_1 ;
JLabel labelMin_1 ;
JLabel labelSec_1 ;
JButton button_O;
JButton button_C;
JButton button_R;
public Clock(){
super("定时关机小程序");
final Timer time = new Timer();
Container con = getContentPane();
ChoiceHour = new Choice();
ChoiceMin = new Choice();
ChoiceSec = new Choice();
labelHour = new JLabel("小时(H)" , JLabel.CENTER);
labelMin = new JLabel("分钟(M)" , JLabel.CENTER);
labelSec = new JLabel("秒(S)" , JLabel.CENTER);
labelHour_1 = new JLabel("参考时间 :" , JLabel.CENTER);
labelMin_1 = new JLabel("00:00:00" , JLabel.CENTER);
labelMin_1.addMouseListener(new MouseAdapter(){
public void mouseEntered(MouseEvent e){
labelMin_1.setForeground(Color.blue);
labelMin_1.setText("此处为当前时间");
labelMin_1.setCursor(new Cursor(Cursor.HAND_CURSOR));
}
public void mouseExited(MouseEvent e){
labelMin_1.setForeground(Color.DARK_GRAY);
labelMin_1.setText("00:00:00");
}
});
labelSec_1 = new JLabel("00:00:00" , JLabel.CENTER);
labelSec_1.addMouseListener(new MouseAdapter(){
public void mouseEntered(MouseEvent e){
labelSec_1.setForeground(Color.blue);
labelSec_1.setText("此处为剩余时间");
labelSec_1.setCursor(new Cursor(Cursor.HAND_CURSOR));
}
public void mouseExited(MouseEvent e){
labelSec_1.setText("00:00:00");
labelSec_1.setForeground(Color.DARK_GRAY);
}
});
button_O = new JButton("确定");
button_O.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
time.schedule(new Refresh(), 1000 , 1000);
}
});
button_C = new JButton("取消");
button_C.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
System.exit(0);
}
});
button_R = new JButton("重置");
button_R.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
time.cancel();
ChoiceHour.select(0);
ChoiceMin.select(0);
ChoiceSec.select(0);
labelMin_1.setText("00:00:00");
labelSec_1.setText("00:00:00");
}
});
for(int i = 0 ; i < 24 ; i++){
ChoiceHour.add(Integer.toString(i));
}
for(int i = 0 ; i < 60 ; i++){
ChoiceMin.add(Integer.toString(i));
}
for(int i = 0 ; i < 60 ; i++){
ChoiceSec.add(Integer.toString(i));
}
ChoiceHour.addItemListener(new CAL());
ChoiceMin.addItemListener(new CAL());
ChoiceSec.addItemListener(new CAL());
con.setLayout(new GridLayout(4,3));
//将上述可视化组件添加到容器中
con.add(labelHour);
con.add(labelMin);
con.add(labelSec);
con.add(ChoiceHour);
con.add(ChoiceMin);
con.add(ChoiceSec);
con.add(labelHour_1);
con.add(labelMin_1 );
con.add(labelSec_1 );
con.add(button_C);
con.add(button_R);
con.add(button_O);
}
public static void main(String[] args){
Clock frame =new Clock();
ImageIcon ii = new ImageIcon("65.gif");
frame.setIconImage(ii.getImage());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(400, 300, 320, 130);
frame.setVisible(true);
frame.setResizable(false);
}
class CAL implements ItemListener{
public void itemStateChanged(ItemEvent ie) {
if(ie.getSource()==ChoiceHour){
hour = Integer.parseInt(ie.getItem().toString());
}
if(ie.getSource()==ChoiceMin){
min = Integer.parseInt(ie.getItem().toString());
}
if(ie.getSource()==ChoiceSec){
sec = Integer.parseInt(ie.getItem().toString());
}
}
}
class Refresh extends TimerTask {
public Refresh() {
super();
}
public void run() {
Date date = new Date();
int hour1 =hour - date.getHours() ;
int min1 = min - date.getMinutes() ;
int sec1 = sec - date.getSeconds() ;
if(sec1>0){
sec1--;
}
else{
if( min1 <= 0 && hour1>0 ){
sec1 +=59 ;
hour1--;
min1 +=59;
}
else{
sec1 +=59 ;
min--;
}
}
DateFormat df = new SimpleDateFormat("hh:mm:ss");
if(hour == date.getHours()&&min == date.getMinutes()&&sec == date.getSeconds()){
try {
String Command = "shutdown -s -t 0";
Runtime.getRuntime().exec(Command);
} catch (Exception e) {}
}
else{
labelMin_1.setText(df.format(date));
labelSec_1.setText(hour1+" : "+min1+":"+sec1);
}
}
}
}
定时关机小程序(JAVA)
最新推荐文章于 2021-02-25 01:45:37 发布