java语句电脑定时关机_java 定时关机程序

package test;

import java.awt.*;

import java.awt.event.*;

import java.util.*;

import java.text.SimpleDateFormat;

import java.io.*;

class SD extends Frame implements ActionListener,Runnable{

Label text1;

TextField text2;

Label text3;

Label text4;

Button button1 ;

Button button2 ;

Dialog dlg;

Date nowtime;

String st;

boolean tf=false;

SD(String s)

{super(s);

setLayout(new GridLayout(4,2));

text1=new Label("请输入关机时间(如 06:20): ",text1.CENTER);

text2=new TextField(10);

text3=new Label("现在的时间是:",text3.CENTER);

nowtime=new Date();

SimpleDateFormat matter1=new SimpleDateFormat("HH 时 mm 分 ss秒 yyyy年 MM 月DD 日 E");

text4=new Label(matter1.format(nowtime));

button1=new Button("确定");

button2=new Button("取消");

add(text1);

add(text2);

add(text3);

add(text4);

add(button1);

add(button2);

setBounds(100,100,500,150);

button1.addActionListener(this);

button2.addActionListener(this);

addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){System.exit(0);}

});

setVisible(true);

validate();

}

public void run(){

while(true){

nowtime=new Date();

SimpleDateFormat matter2=new SimpleDateFormat("HH 时 mm 分 ss秒 yyyy年 MM 月DD 日 E");

text4.setText((matter2.format(nowtime)));

try{

Thread.sleep(1000);}

catch(Exception ex){}

}}

public void actionPerformed(ActionEvent e)

{

String command=e.getActionCommand();

if(command.equals("确定")){

st=text2.getText();

int tj=st.length();

if(st.equals("")||tj<5){

final Dialog d=new Dialog(this,"错误",true);

d.add(new Label("你输入的格式错误!请从新输入!例如08:30",Label.CENTER));

d.addWindowListener(new WindowAdapter()

{

public void windowClosing(WindowEvent e){d.dispose();}

});

d.setSize(300,100);

d.setVisible(true);

}else{

dlg=new Dialog(this,"关机确定",true);

Panel p=new Panel();

p.setLayout(new GridLayout(1,2));

Button button3=new Button("是");

Button button4=new Button("否");

p.add(button3);

p.add(button4);

button3.addActionListener(new ActionListener()

{public void actionPerformed(ActionEvent s){

try{

Runtime rt=Runtime.getRuntime();

String cmd= " C:/Windows/System32/at "+st+" shutdown.exe -s -t 20";

rt.exec(cmd);}catch(IOException e){}

dlg.dispose();}

});

button4.addActionListener(new ActionListener()

{public void actionPerformed(ActionEvent s){

dlg.dispose();}

});

dlg.addWindowListener(new WindowAdapter()

{

public void windowClosing(WindowEvent e){dlg.dispose();}

});

dlg.add(new Label("你确定在"+st+"时关机"),"Center");

dlg.add(p,"South");

dlg.setBounds(150,200,200,100);

dlg.setVisible(true);

}}

else if(command.equals("取消")){

this.dispose();

}

}

}

public class FrameSD{

public static void main(String arge[]){

SD win= new SD("定时关机程序");

Thread t=new Thread(win);

t.start();

}//main

}//FrameSD

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值