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
java 定时关机程序
阅读:6561 输入:2017-10-02 22:25:56