java 编写关机小程序_定时关机小程序(JAVA)

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);

}

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值