Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

A java program to shutdownxp that has a bug.Help  RSS feed

 
Lison Wei
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.awt.AWTException;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Frame;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.Enumeration;

import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.UIManager;

/**
* @date 2008-12-6
* @author 魏蕾航
* @describe 这个程序用来控制电脑的使用时间,当在指定的时间段内是,电脑能正常使用,超出正确时间段,那么
* 程序将使电脑被迫关机,除非您有密钥解密,否则电脑在非指定时间段将不可使用。本程序有10分钟的
* 误差时间,这是为拉节约cpu资源采用线程休眠的方法,由于线程休眠10分钟,所以可能造成在某个10分钟内
* 程序不能正确响应系统的时间来控制关机行为。
*
*/
@SuppressWarnings("serial")
public class ShutDownPC extends JFrame implements ActionListener, Runnable {

// swing 组建
JTextField jt1;
JLabel jl1;
JLabel jl2;
JButton jb1;
Thread thread;
String name;
// 托盘图标
private TrayIcon trayIcon;
// 系统托盘
private SystemTray systemTray;

// 默认构造函数,用来初始化swing组建、线程、界面大小及版式,还有设置监听实践。
public ShutDownPC() {

jt1 = new JTextField(10);
jl1 = new JLabel("您的电脑将在23:00-6:00之间内强迫关机!(本系统有5分钟误差)");
jl2 = new JLabel("输入您的密钥,终止自关机程序:");
jb1 = new JButton("确定");

this.add(jl1);
this.add(jl2);
this.add(jt1);
this.add(jb1);

// 托盘处理
systemTray = SystemTray.getSystemTray();// 获得系统托盘的实例
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
try {
trayIcon = new TrayIcon(ImageIO.read(ClassLoader
.getSystemResourceAsStream("shutxp.jpg")),"XP自关机程序");
systemTray.add(trayIcon);// 设置托盘的图标,0.gif与该类文件同一目录
} catch (IOException e1) {
e1.printStackTrace();
} catch (AWTException e2) {
e2.printStackTrace();
}

this.setVisible(true);
this.setLayout(new FlowLayout());
this.setSize(550, 150);
this.setTitle("XP系统自关机控制程序,请不要使用任务管理器强制终止,否则后果自负。");
this.setDefaultCloseOperation(0);

jb1.addActionListener(this);
jt1.addActionListener(this);

// 窗口最小化时dispose该窗口
this.addWindowListener(new WindowAdapter() {
public void windowIconified(WindowEvent e) {
dispose();
}
});

// 双击托盘窗口再现
trayIcon.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
setExtendedState(Frame.NORMAL);
setVisible(true);
}
}
});

if (thread == null) {
thread = new Thread(this);
thread.start();
}

}

// 更改xp自带强制关机命令名
public void reName() {
name = (int) (Math.random() * 10) + "shut";
File file = new File("C:/WINDOWS/system32/shutdown.exe");
file.renameTo(new File("C:/WINDOWS/system32/" + name + ".exe")); // 改名
System.out.println("rename is successfull!");
}

// 还原xp自带强制关机文件名
public void recoverName() {

File file = new File("C:/WINDOWS/system32/" + name + ".exe");
String name = "shutdown";
file.renameTo(new File("C:/WINDOWS/system32/" + name + ".exe")); // 改名
System.out.println("recover the shutdown process.");

}

// 启动调用xp系统本地Shutdown进程,依据时间条件进行关机。
@SuppressWarnings( { "deprecation", "static-access" })
public void run() {
Date d;

// 强制修改xp自带关机进程名
reName();
try {
while (true) {
d = new Date();
int hour = d.getHours();
if (hour > 22 || hour < 6) {

jl1.setText("您的计算机将在3分钟后关机,请保存好您的资料。如果有急需,请输入密钥!");
// 获得系统关机进程。
Runtime.getRuntime().exec(name + " -s -t 180");
// 休眠一段时间,来减少程序的运行次数。节约cpu资源。
thread.sleep(1000 * 60 * 5);
} else {
thread.sleep(1000 * 60 * 5);
continue;
}
}
} catch (Exception e) {
e.printStackTrace();
}

}

// 解密事件处理
@SuppressWarnings("deprecation")
public void actionPerformed(ActionEvent e) {

String source = jt1.getText();
if (source.equalsIgnoreCase("Elite")) {
try {
// 调用关闭程序进程。
Runtime.getRuntime().exec(name + " -a");
} catch (IOException e2) {
e2.printStackTrace();
}

// 恢复进程名
recoverName();

JOptionPane.showMessageDialog(null, "自关机程序终止");
thread.stop();
this.dispose();
System.exit(0);

} else {
JOptionPane.showMessageDialog(null, "密钥错误,您没有权限操作");
}

}

@SuppressWarnings("unchecked")
public static void main(String args[]) {

// 设置默认界面
// setDefaultLookAndFeelDecorated(true);
// 设置界面字体及风格
Font font = new Font("JFrame", Font.PLAIN, 14);
Enumeration keys = UIManager.getLookAndFeelDefaults().keys();
while (keys.hasMoreElements()) {
Object key = keys.nextElement();
if (UIManager.get(key) instanceof Font)
UIManager.put(key, font);
}
// 初始化类
new ShutDownPC();
}
}




Hello everyone ,
A trouble happenged in my little program which close computer with xp system.
When you open the task manager and force to close this program,you will can'nt close the computer normally enven you hava the key.
This program request you don'nt use task manager manage the program.
what should I do ,when i want to use task manager normally?


The program request upon Jdk1.6 .
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"weileihang",
Welcome to the ranch. You may not be aware of the ranch Naming Policy. Please read it carefully and change your name accordingly (you need to separate your first and last names). Thank you.

Also, the servlets forum is not the appropriate place to ask for this You should have asked in the Swing forum.

About your problem, you could try to use a WindowAdapter and override the windowClosing methodm, and register it in the frame via this.addWindowListener(windowEventHandler). I think this method will be called when killing the process from the task manager, so you could call your recoverName method in there.

By the way, you can use code tags to make your code look better
 
Campbell Ritchie
Sheriff
Posts: 53760
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe you have been warned before, but please go to "my private messages" and read the message I have just sent you.

CR
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!