利用java实现关机_java编写的关机程序

【实例简介】

注:运行需要配置jdk环境

【实例截图】

1ac6bbfb27ecbe3176fcd1ff1c265c7d.png

新用户求点积分下载资源,还请各位大佬多多支持

【核心代码】

package ui;

import fileoption.FileOption;

import java.awt.Color;

import java.awt.Font;

import java.awt.GridLayout;

import java.awt.Image;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.KeyEvent;

import java.awt.event.KeyListener;

import java.io.IOException;

import javax.imageio.ImageIO;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JTextField;

import javax.swing.border.TitledBorder;

import rbutton.RButton;

import thread.TimeCountThread;

public class UI extends JFrame

{

private static final long serialVersionUID = 1L;

private Integer time = null;

private JPanel panelChoose = new JPanel(new GridLayout(4, 1));

private JPanel panelFunc = new JPanel(new GridLayout(2, 1));

private JPanel countTime = new JPanel();

private static UI ui = new UI();

private FileOption fileoption = FileOption.initFileOption();

private boolean limit = true;

private int flag = 0;

private TimeCountThread thread = new TimeCountThread();

public static UI initUI() { return ui; }

private UI()

{

try

{

Image img = ImageIO.read(super.getClass().getResource("/img/1.png"));

setIconImage(img);

setIconImage(img); } catch (IOException e) {

e.printStackTrace();

}

setAlwaysOnTop(true);

setTitle("定时关机");

setSize(300, 200);

setResizable(false);

setLocationRelativeTo(null);

putsetPanel();

add(this.panelChoose, "West");

addFunc();

add(this.panelFunc, "Center");

setVisible(true);

setDefaultCloseOperation(3);

}

private void addBound(String title, JPanel panel, int fontSize)

{

TitledBorder titl = new TitledBorder(title);

titl.setTitleFont(new Font("宋体", 0, fontSize));

titl.setTitleColor(Color.GRAY);

panel.setBorder(titl);

}

private void putsetPanel()

{

addBound("请选择", this.panelChoose, 12);

RButton shutDown = new RButton("关 机");

boundPanel(shutDown);

shutDown.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e) {

UI.this.showScreen("即将关机..");

String content = "shutdown -s -t 0";

UI.this.fileoption.listenBtn(FileOption.shutdwnFile, content);

System.exit(0);

}

});

RButton reboot = new RButton("立即重启");

boundPanel(reboot);

reboot.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e) {

UI.this.showScreen("重启中..");

String content = "shutdown -r -t 0";

UI.this.fileoption.listenBtn(FileOption.rebootFile, content);

System.exit(0);

}

});

RButton sleepDown = new RButton("休 眠");

boundPanel(sleepDown);

sleepDown.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e) {

UI.this.showScreen("即将进入休眠状态..");

String content = "shutdown -h";

UI.this.fileoption.listenBtn(FileOption.sleepFile, content);

System.exit(0);

}

});

RButton cancelShut = new RButton("取消关机");

boundPanel(cancelShut);

cancelShut.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e) {

UI.this.thread.setFlagTrue();

if (UI.this.flag == 0) {

UI.this.showScreen("还没设置关机哟!(?????) ");

} else {

UI.this.showScreen("已取消!\\(^o^)/ ");

String content = "shutdown -a";

UI.this.fileoption.listenBtn(FileOption.cancelFile, content);

UI.this.setDefaultCloseOperation(3);

UI.this.limit = true;

UI.this.flag = 0;

}

}

});

}

private void showScreen(String msg)

{

this.countTime.removeAll();

JLabel label = new JLabel(msg);

label.setFont(getFont());

this.countTime.add(label);

this.countTime.validate();

this.countTime.repaint();

}

private void boundPanel(RButton button)

{

JPanel btn = new JPanel();

btn.add(button);

this.panelChoose.add(btn);

}

private void addFunc()

{

this.panelFunc.add(setShutDownTime());

setCountTime();

this.panelFunc.add(this.countTime);

}

private JPanel setShutDownTime()

{

JPanel set = new JPanel();

addBound("定时关机(s)", set, 10);

JTextField text = new JTextField(4);

JPanel panel = new JPanel();

panel.add(text);

set.add(panel);

text.addKeyListener(new KeyListener()

{

public void keyPressed(KeyEvent e) {

if (e.getKeyCode() == 10)

UI.this.packOption(text);

}

public void keyTyped(KeyEvent e)

{

}

public void keyReleased(KeyEvent e)

{

}

});

JLabel s = new JLabel("秒");

s.setFont(getFont());

panel = new JPanel();

panel.add(s);

set.add(panel);

RButton queRen = new RButton("确认");

});

panel = new JPanel();

panel.add(queRen);

set.add(panel);

return set;

}

private void packOption(JTextField text)

{

try

{

queRenBtnListen(text);

}

catch (Exception e1)

{

showScreen(e1.getMessage());

}

}

private void queRenBtnListen(JTextField text)

throws Exception

{

if (!this.limit) {

throw new Exception("请不要重复确认哦!(?ˉ?ˉ?) ");

}

if (this.limit) {

judgeText(text.getText());

int time = Integer.valueOf(text.getText()).intValue();

this.time = Integer.valueOf(time);

setDefaultCloseOperation(0);

String content = "shutdown -s -t " time;

this.fileoption.listenBtn(FileOption.shutdwnFile, content);

this.thread.ui(this);

if (!this.thread.isAlive()) {

this.thread.start();

}

synchronized (this.thread.syncObj) {

this.thread.time = this.time;

this.thread.setFlagFalse();

this.thread.syncObj.notify();

}

this.limit = false;

this.flag = 1;

}

}

private void judgeText(String text)

throws Exception

{

if (("".equals(text.replace(" ", ""))) || (text == null)) {

throw new Exception("输入不能为空哟!o( ̄ε ̄*) ");

}

String regex = "^\\d{1,4}";

if (!text.matches(regex))

throw new Exception("请输入4位数以内的数字!(?????) ");

}

private void setCountTime()

{

addBound("提示", this.countTime, 10);

}

private void showShutDownTime(Integer count)

{

JPanel panelTime = new JPanel();

JLabel label = new JLabel("还有 " count " s关机");

label.setFont(getFont());

this.countTime.removeAll();

panelTime.add(label);

this.countTime.add(panelTime);

this.countTime.validate();

this.countTime.repaint();

}

public void time(Integer count)

{

showShutDownTime(count);

}

public Integer getTime()

{

return this.time;

}

}

  • 0
    点赞
  • 0
    收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:深蓝海洋 设计师:CSDN官方博客 返回首页
评论

打赏作者

weixin_39834090

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值