java写的关机小程序(自用)
import java.awt.*;import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.swing.*;
public class Test
{
public static void main(String[] args)
{
Myframe mj = new Myframe("jichangxiu tools");
}
}
class Myframe extends JFrame
{
public Myframe(String str)
{
super(str);
this.setBounds(200, 200, 300, 130);
this.setLayout(new FlowLayout());
this.setResizable(false);
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
init();
this.setVisible(true);
}
private void init()
{
add(new JLabel("多长时间后关机:"));
JTextField jtf = new JTextField(10);
add(jtf);
add(new JLabel("(分钟)"));
JTextField jtf2 = new JTextField(25);
jtf2.setEditable(false);
add(jtf2);
JButton jb1 = new JButton("立即关机");
add(jb1);
JButton jb2 = new JButton("确认关机");
add(jb2);
JButton jb3 = new JButton("取消关机");
add(jb3);
jb1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
command.executeCmd("Shutdown.exe -s -t 0");
}catch(IOException e1)
{
jtf2.setText("关机错误! 可能导致的原因是系统不支持!");
}
}
});
jb2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String text = jtf.getText();
try
{
int time = Integer.parseInt(text);
// System.out.println("Shutdown.exe -s -t " + time*60);
command.executeCmd("Shutdown.exe -s -t " + time*60);
jtf2.setText("成功设置! 系统将在:" + time + "分钟后关机");
}catch(NumberFormatException e1)
{
jtf2.setText("输入非法! 必须输入数字!");
}catch(IOException e1)
{
jtf2.setText("设置错误! 可能导致的原因是系统不支持!");
}
}
});
jb3.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
command.executeCmd("shutdown -a");
jtf2.setText("成功取消关机!");
}catch(IOException e1)
{
jtf2.setText("取消关机错误! 可能导致的原因是系统不支持!");
}
}
});
}
}
class command
{
public static String executeCmd(String command) throws IOException
{
// System.out.println("Execute command : " + command);
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec("cmd /c " + command);
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream(), "UTF-8"));
String line = null;
StringBuilder build = new StringBuilder();
while ((line = br.readLine()) != null)
{
// System.out.println(line);
build.append(line);
}
return build.toString();
}
}
程序打包:
链接: https://pan.baidu.com/s/1saJH_miEKqQOHab4WJX0cQ 密码: jz3m
注意:这是64位的.32位机器打不开.
32位的自己copy代码,编译运行吧.
向往的歌 发表于 2018-7-20 23:30
不错!楼主再来一个关闭屏幕的(而不是关机的)……
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.swing.*;
public class Test
{
public static void main(String[] args)
{
Myframe mj = new Myframe("jichangxiu tools");
}
}
class Myframe extends JFrame
{
private command com = new command();
public Myframe(String str)
{
super(str);
this.setBounds(200, 200, 400, 130);
this.setLayout(new FlowLayout());
this.setResizable(false);
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
init();
this.setVisible(true);
}
private void init()
{
add(new JLabel("多长时间后关机:"));
JTextField jtf = new JTextField(10);
add(jtf);
add(new JLabel("(分钟)"));
JTextField jtf2 = new JTextField(30);
jtf2.setEditable(false);
add(jtf2);
JButton jb1 = new JButton("立即关机");
add(jb1);
JButton jb2 = new JButton("确认关机");
add(jb2);
JButton jb3 = new JButton("取消关机");
add(jb3);
JButton jb4 = new JButton("关闭显示器");
add(jb4);
jb1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
com.executeCmd("Shutdown.exe -s -t 0");
}catch(IOException e1)
{
jtf2.setText("关机错误! 可能导致的原因是系统不支持!");
}
}
});
jb2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String text = jtf.getText();
try
{
int time = Integer.parseInt(text);
// System.out.println("Shutdown.exe -s -t " + time*60);
com.executeCmd("Shutdown.exe -s -t " + time*60);
jtf2.setText("成功设置! 系统将在:" + time + "分钟后关机");
}catch(NumberFormatException e1)
{
jtf2.setText("输入非法! 必须输入数字!");
}catch(IOException e1)
{
jtf2.setText("设置错误! 可能导致的原因是系统不支持!");
}
}
});
jb3.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
com.executeCmd("shutdown -a");
jtf2.setText("成功取消关机!");
}catch(IOException e1)
{
jtf2.setText("取消关机错误! 可能导致的原因是系统不支持!");
}
}
});
jb4.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
com.executeCmd("%systemroot%\\system32\\scrnsave.scr /s");
}catch(IOException e1)
{
jtf2.setText("关闭显示器错误! 可能导致的原因是系统不支持!");
}
}
});
}
private class command
{
private String executeCmd(String command) throws IOException
{
// System.out.println("Execute command : " + command);
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec("cmd /c " + command);
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream(), "UTF-8"));
String line = null;
StringBuilder build = new StringBuilder();
while ((line = br.readLine()) != null)
{
// System.out.println(line);
build.append(line);
}
return build.toString();
}
}
}
你自己编译一下吧 向往的歌 发表于 2018-7-21 08:14
下载下来打不开——不懂java……
https://blog.csdn.net/qq_35749683/article/details/53582896
对了,是不是没有环境变量 感谢楼主分享,! 小巧 好用 谢谢楼主正好需要录屏关机 不错 谢谢楼主 谢谢楼主 不错!楼主再来一个关闭屏幕的(而不是关机的)…… 感谢楼主的无私奉献! 公子语凉 发表于 2018-7-21 06:08
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.eve ...
{:1_908:}不会编译!正在学习中…… 向往的歌 发表于 2018-7-21 07:42
不会编译!正在学习中……
链接: https://pan.baidu.com/s/1WgCIzVoO_03AH_Yxnw9-HQ 密码: r2is