现在的位置: 首页 > 综合 > 正文

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

2018年05月23日 ⁄ 综合 ⁄ 共 2569字 ⁄ 字号 评论关闭

        这两天半夜总是开着电脑发无线,到后半夜忘记关闭,这样会伤害电脑,所以我就着手于编写一个软件,让它自动关闭。本软件采用的是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");

}
}

抱歉!评论已关闭.