1.运行截图
2.代码实现
Game类
package com.zzu.Demo1;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
public class Game {
//创建CountDown对象
CountDown cd = new CountDown();
//创建标签
JLabel jLabel = new JLabel("电脑将在" + cd.close.time + "秒后关机,输入”" + cd.close.input + "“则取消!");
static JLabel label = new JLabel();
static JFrame MyjFrame = new JFrame("shutdownApp1.0");
//创建两个按钮
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();
//创建Close对象
Close cs = new Close();
public Game() {
//上传logo
ImageIcon icon = new ImageIcon("520.jpg");
Image image = icon.getImage();
MyjFrame.setIconImage(image);
//设置窗口大小位置
MyjFrame.setSize(500, 500);
MyjFrame.setLocation(400, 100);
//设置关闭窗口之后程序随之关闭
MyjFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//响应回车键
MyjFrame.getRootPane().setDefaultButton(ok);
//设置标签字体格式、大小、颜色
jLabel.setFont(new java.awt.Font("行楷", 1, 16));
jLabel.setForeground(Color.BLUE);
label.setFont(new java.awt.Font("行楷", 1, 24));
label.setForeground(Color.PINK);
edit.setFont(new java.awt.Font("行楷", 1, 18));
//添加布局
jp01.add(jLabel);
jp02.add(edit);
jp03.add(label);
jp04.add(ca);
jp04.add(ok);
MyjFrame.add(jp01);
MyjFrame.add(jp02);
MyjFrame.add(jp03);
MyjFrame.add(jp04);
//设置布局风格
MyjFrame.setLayout(new GridLayout(5, 1));
cs.close();
MyjFrame.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 Game();
}
}
CountDown类
package com.zzu.Demo1;
import javax.swing.*;
import java.awt.*;
public class CountDown extends Thread{
Close close = new Close();
String str;
/***
*书写线程要执行的代码
*/
public void run() {
while (true) {
try {//让线程休眠指定的时间,单位为毫秒
Thread.sleep(1000);
} catch (InterruptedException e) {
}
close.time--;
if (close.time == 10) {//当只剩下10秒时
JLabel sb = new JLabel("倒计时十秒");
sb.setFont(new java.awt.Font("隶书", 1, 24));
sb.setBackground(Color.GREEN);
JPanel pa = new JPanel();
pa.add(sb);
Game.MyjFrame.add(pa);
Game.MyjFrame.setVisible(true);
Game.edit.setVisible(false);
Game.ca.setEnabled(false);
Game.ok.setEnabled(false);
}
str = "倒计时:" + close.time;
Game.label.setText(str);
Game.jp03.add(Game.label);
if (close.time == 0) {
System.exit(0);
}
}
}
}
Close类
package com.zzu.Demo1;
/**
*shutdown: 关机
*加上参数才能执行
*-s: 默认在一分钟之后关机
*-s -t 指定时间:指定关机时间
*-a: 取消关机操作
*-r: 关机并重启
*/
public class Close {
Runtime rTime = Runtime.getRuntime();
int time = 60;//设置关机时间
String input = "我就是笨蛋";
public void close() {
try {//运行cmd命令
rTime.exec("shutdown -s -t " + time);
} catch (Exception e) {
}
}
public void cancel() {
try {//运行cmd命令
rTime.exec("shutdown -a");//取消关机
} catch (Exception e) {
}
}
}