JAVA编写的自动定时关机软件适用于windows下面

        这两天半夜总是开着电脑发无线,到后半夜忘记关闭,这样会伤害电脑,所以我就着手于编写一个软件,让它自动关闭。本软件采用的是JAVA  swing 技术,尽量做到代码扩展性高点。希望给新手以借鉴,大神勿喷,可以交流。


代码:

package com.auto.main;
import com.auto.views.MainView;
public class MainWelcome {

public static void main(String[] args) {
MainView mainView = new MainView();
mainView.view();
}
}


package com.auto.views;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeListener;


import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;


import com.auto.tools.Tools;


//主界面视图
public class MainView extends JFrame implements ActionListener{


private JTextField textField = null;
private JButton button = null;
private JButton about = null;
private final int width = 400;         //图形界面的宽
private final int length = 200;        //图形界面的高
private final int locationWidth = 380;  //图形位置的宽
private final int locationLength = 200; //图形位置的高
private final String  strButtonString = "确定"; //按钮的文本内容
private final String aboutStr = "关于";
private final String strTextfield = "";
private final int intTextfield = 15;    //文本框的长度
private String strText2 = "";           //所获取的文本框值
Tools tools = new Tools();

public MainView(){
textField = new JTextField(intTextfield);
button = new JButton(strButtonString);
textField.setText(strTextfield);
about = new JButton(aboutStr);
}


//界面视图
public void view(){
setTitle("定时自动关机软件--分钟");
setSize(width, length);
setLocation(locationWidth, locationLength);;
setLayout(new FlowLayout());
add(textField);
add(button);
add(about);
button.addActionListener(this);
about.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
JOptionPane.showMessageDialog(null, "本软件作者是:Huu   QQ:1097067196 欢迎大家互相交流");
}
});
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}


public void actionPerformed(ActionEvent e) {
try {
strText2 = textField.getText();
int count = Integer.parseInt(strText2);
if(e.getSource() == button){
tools.tools(count);
}
} catch (Exception e2) {
JOptionPane.showMessageDialog(null, "文本框内只能输入数值");
}
}
}



package com.auto.tools;

import java.io.IOException;
import java.util.concurrent.Callable;
import javax.swing.JOptionPane;


//调用cmd命令时候需要使用的类Runtime

public class Tools {

private String textCmd = "";       //cmd命令变量

public void tools(int value) {
try {
textCmd =  "cmd /k shutdown -s -t "+value*60;
Runtime.getRuntime().exec(textCmd);
JOptionPane.showMessageDialog(null, "设定成功,"+value*60+"分钟后将自动关机!");
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "您输入的命令不正确");
}
}

public static void main(String[] args) throws Throwable {
//Tools tools = new Tools();
//tools.tools(1);
int value = 60;
Runtime.getRuntime().exec("cmd /k shutdown -s -t "+value );
Runtime.getRuntime().exec("cmd /k start notepad");

}
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

南方小糊涂

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值