[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代码,编译运行吧.
|