有几个月没写博客了,主要是实在没得写= =今天下午闲着无聊写个定时关机的程序发来玩玩,没什么技术含量...就是Runtime类的exec()方法调用系统关机命令 实际代码就2行 其他大部分都是GUI的。。。
PS.不会用EXE4J 不知道怎么转换成EXE文件执行
程序主界面
package TreeTools;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
public class TimingShutdown {
public Frame f = new Frame("TimingShutdown");
public Panel p = new Panel();
public Button setButton = new Button("ShutDown");
public Button cancelButton = new Button("Cancel");
public TextField tf = new TextField("input the timing(min)",20);
public Runtime rt = Runtime.getRuntime();
//creat window
public void getFrame(){
p.add(tf);
p.add(setButton);
p.add(cancelButton);
f.add(p);
f.pack();
f.setVisible(true);
}
//process event
public void init(){
f.addWindowListener(new fFrameListener());
setButton.addActionListener(new setButtonListener());
cancelButton.addActionListener(new cancelButtonListener());
}
//creat Window's Listener to shutdown Window
class fFrameListener implements WindowListener{
public void windowClosing(WindowEvent we){
System.exit(0);
}
public void windowDeactivated(WindowEvent we){}
public void windowActivated(WindowEvent we){}
public void windowDeiconified(WindowEvent we){}
public void windowIconified(WindowEvent we){}
public void windowClosed(WindowEvent we){}
public void windowOpened(WindowEvent we){}
}
//creat setButton's Listener to shutdown pc
class setButtonListener implements ActionListener{
public void actionPerformed(ActionEvent e){
String setTiming = tf.getText();
int timing = new Integer(setTiming);
timing *= 60;
setTiming = "shutdown.exe -s -t " + timing;
try{
rt.exec(setTiming);
}
catch(IOException exc){
;
}
}
}
//creat cancelButton's Listener to cancel showdown plan
class cancelButtonListener implements ActionListener{
public void actionPerformed(ActionEvent e){
try{
rt.exec("shutdown.exe -a");
}
catch(IOException exc){
;
}
}
}
public static void main(String[] args) throws Exception{
TimingShutdown ts = new TimingShutdown();
ts.init();
ts.getFrame();
}
}