【Java】定时关机源码

博主分享了一段简单的Java代码,利用Runtime.exec()方法执行系统关机命令,创建了一个基本的定时关机程序。虽然没有深入的技术探讨,但适合初学者了解如何在Java中调用系统命令。
摘要由CSDN通过智能技术生成

有几个月没写博客了,主要是实在没得写= =今天下午闲着无聊写个定时关机的程序发来玩玩,没什么技术含量...就是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();
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值