Java实现电脑关机恶作剧(多线程和swing结合)

CloseSystem.java:

public class CloseSystem {
Runtime rTime = Runtime.getRuntime();
int time = 50;//设置关机时间
String input = "我是猪";
public void close() {
try {
rTime.exec("shutdown -s -t "+time);
}
catch(Exception e) { }
}
public void cancel() {
try {
rTime.exec("shutdown -a");//取消关机
}
catch(Exception e){ }
}

}

 

Countdown.java:

import java.awt.Color;


import javax.swing.JLabel;
import javax.swing.JPanel;


public class Countdown extends Thread {
CloseSystem cs = new CloseSystem();
String afx; 
public void run() {
while(true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) { }
cs.time--;
if(cs.time==5) {//当只剩下5秒时
   JLabel sb = new JLabel("傻逼,你就静静等着电脑关机吧!");
   sb.setFont(new java.awt.Font("隶书",1,24));
   sb.setBackground(Color.GREEN);
   JPanel pa = new JPanel();
   pa.add(sb);
   MyFrame.myJF.add(pa);
   MyFrame.myJF.setVisible(true);
MyFrame.edit.setVisible(false);
MyFrame.ca.setEnabled(false);
MyFrame.ok.setEnabled(false);
}
afx = "倒计时:"+cs.time;
MyFrame.label.setText(afx);
MyFrame.jp03.add(MyFrame.label);
if(cs.time==0) {
System.exit(0);
}
}
}

}

 

MyFrame.java:

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class MyFrame {
Countdown cd = new Countdown();
JLabel caption = 
new  JLabel("电脑将在"+cd.cs.time+"秒后关机,输入”"+cd.cs.input+"“则取消!");
static JLabel label = new JLabel();
static JFrame myJF = new JFrame("关机程序");
static JButton ok = new JButton("确定");
static JButton ca = new JButton("退出");
static JTextField edit = new JTextField(8);
JPanel jp01 = new JPanel();
JPanel jp02 = new JPanel();
static JPanel jp03 = new JPanel();
JPanel jp04 = new JPanel();
CloseSystem cs = new CloseSystem();
public MyFrame() {
myJF.setSize(500,500);
myJF.setLocation(400,100);
myJF.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myJF.getRootPane().setDefaultButton(ok);//响应回车键
caption.setFont(new java.awt.Font("行楷",1,16));
caption.setForeground(Color.BLUE);
label.setFont(new java.awt.Font("行楷",1,24));
label.setForeground(Color.RED);
edit.setFont(new java.awt.Font("行楷",1,18));
jp01.add(caption);
jp02.add(edit);
jp03.add(label);
jp04.add(ca);
jp04.add(ok);
myJF.add(jp01);
myJF.add(jp02);
myJF.add(jp03);
myJF.add(jp04);
myJF.setLayout(new GridLayout(5,1));
cs.close();
myJF.setVisible(true);

cd.start();

ok.addActionListener(new ActionListener() {
@SuppressWarnings("deprecation")
public void actionPerformed(ActionEvent e) {
if(edit.getText().equals(cs.input)) {
cs.cancel();
JOptionPane.showMessageDialog(null, "关机已取消!", 
"", JOptionPane.INFORMATION_MESSAGE);
cd.stop();        //线程停止
edit.setEnabled(false);
ok.setEnabled(false);
ca.setEnabled(false);
}
else {
JOptionPane.showMessageDialog(null, "输入错误!", 
"提示", JOptionPane.ERROR_MESSAGE);
edit.setText("");
}
}
});
ca.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "确定要退出吗?电脑依然会关机的!", 
"提示", JOptionPane.ERROR_MESSAGE);
}
});
}

public static void main(String[] args) {
new MyFrame();
}

}

 

Java实现电脑关机恶作剧(多线程和swing结合)_第1张图片

你可能感兴趣的:(Java)