//本程序利用Java程序向系统发送命令,倒计时关机或定时关机功能
//@author:张文宇
//使用JCreater编写代码
//倒计时关机即在用户输入的秒数后关机;定时关机即在用户输入的XX:XX:XX时关机
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.URL;
public class CloseMyComputer extends JFrame implements ActionListener
{
JLabel label,label1,label2;
JTextField text1,text2;
JButton button1,button2;
CloseMyComputer()
{
super("定时关机");
this.setBounds(500,200,218,500);
this.setLayout(null);
this.getContentPane().setBackground(new Color(100,149,237));
label=new JLabel();
URL url = getClass().getResource("img/close.png");
label.setIcon(new ImageIcon(url));
label.setBounds(0,0,200,200);
label1=new JLabel("倒计时关机时间(S)");
label1.setForeground(new Color(250,250,250));
label1.setBounds(20,230,150,20);
text1=new JTextField();
text1.setBounds(20,260,150,20);
label2=new JLabel("定时关机时间(S)");
label2.setForeground(new Color(250,250,250));
label2.setBounds(20,290,150,20);
text2=new JTextField();
text2.setBounds(20,320,150,20);
button1=new JButton("开始");
button1.setBounds(20,380,60,20);
button1.addActionListener(this);
button2=new JButton("取消");
button2.setBounds(110,380,60,20);
button2.addActionListener(this);
this.add(label);
this.add(label1);
this.add(text1);
this.add(label2);
this.add(text2);
this.add(button1);
this.add(button2);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e)
{
String string1,string2;
string1=text1.getText();
string2=text2.getText();
if(e.getSource()==button1)
{
if((string1.equals(""))&&(string2.equals("")))
{
javax.swing.JOptionPane.showMessageDialog(this,"请选择一种关机输入!");
}
else if(!(string1.equals(""))&&!(string2.equals("")))
{
javax.swing.JOptionPane.showMessageDialog(this,"只能选择一种关机方法!");
}
else if(!(string1.equals(""))&&(string2.equals("")))
{
//选择倒计时关机(时间单位是:分钟)
try{
//执行关机在XX分钟以后
String command="shutdown.exe -s -t "+string1;
Runtime r=Runtime.getRuntime();
//新建进程执行该命令
Process p=r.exec(command);
BufferedReader br=new BufferedReader(new InputStreamReader(p.getInputStream()));
StringBuffer sb=new StringBuffer();
String inline;
while(null!=(inline=br.readLine()))
{
sb.append(inline).append("\n");
}
System.out.println(sb.toString());
}
catch(IOException ee){}
}
else if((string1.equals(""))&&!(string2.equals("")))
{
//选择定时关机(时间格式:22:15:30)
try{
//命令:在XX:XX:XX执行关机
String command="at "+string2+" shutdown.exe -s";
Runtime r=Runtime.getRuntime();
Process p=r.exec(command);
BufferedReader br=new BufferedReader(new InputStreamReader(p.getInputStream()));
StringBuffer sb=new StringBuffer();
String inline;
while(null!=(inline=br.readLine()))
{
sb.append(inline).append("\n");
}
System.out.println(sb.toString());
}
catch(IOException ee){}
}
}
else if(e.getSource()==button2)
{
}
}
public static void main(String args[])
{
new CloseMyComputer();
}
}
指定的时间关机和延迟关机
3星 · 超过75%的资源 需积分: 31 15 浏览量
2014-07-11
00:08:25
上传
评论
收藏 78KB ZIP 举报
qq_17482915
- 粉丝: 0
- 资源: 6
最新资源
- NT98336 SDK 114版本
- torch-1.8.1+cu111-cp38-cp38-linux-x86-64.whl +torchvision
- 计算机设计 - VB+ACCESS网络计时管理系统设计(源代码+系统),保证可靠运行,毕业生可参考,免费资源下载
- 计算机设计 - vb+ACCESS身份证管理系统设计(LW+源代码),保证可靠运行,毕业生可参考,免费资源下载
- ARCMAP自动编号工具
- W801学习笔记十三:掌机系统-系统基础组件-PNG-loader
- studydatastruct3
- XHS链接提取/作品采集工具
- general-exporter windows
- kotlin语法快速入门文档
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈