java中的关机工具原理:
图片如下:
主类:
- package com.feng.shutdown;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import java.util.Timer;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
- public class CloseComputer extends JFrame implements ActionListener {
- private static final long serialVersionUID = 1L;
- private String key;
- private JLabel showTime; //显示倒计时
- private Timer timer; //延时时间
- private boolean time_show = true; //线程显示时间标示
- private JButton btn_3;
- public CloseComputer() {
- getContentPane().setLayout(null);
-
- JLabel label = new JLabel("请选择关机的方式");
- label.setBounds(34, 9, 128, 15);
- getContentPane().add(label);
-
- JButton btn_1 = new JButton("倒计时关机");
- btn_1.setBounds(10, 112, 120, 23);
- btn_1.addActionListener(this);
- getContentPane().add(btn_1);
-
- JButton btn_2 = new JButton("定时关机");
- btn_2.setBounds(180, 112, 96, 23);
- btn_2.addActionListener(this);
- getContentPane().add(btn_2);
-
- btn_3 = new JButton("取消关机");
- btn_3.setEnabled(false);
- btn_3.setBounds(328, 112, 96, 23);
- btn_3.addActionListener(this);
- getContentPane().add(btn_3);
-
- showTime = new JLabel("");
- showTime.setBounds(130, 65, 158, 15);
- getContentPane().add(showTime);
-
- }
- //点击事件
- @Override
- public void actionPerformed(ActionEvent e) {
- // TODO Auto-generated method stub
- String ActionCommand = e.getActionCommand(); //获取按钮的名称
- btn_3.setEnabled(true);
- if(e.getSource() instanceof JButton) {
- if("倒计时关机".equals(ActionCommand)) {
- timer = new Timer();//类初始化的时候就实例化
- time_show = true;
- cutDown();
- }
- if("定时关机".equals(ActionCommand)) {
- timer = new Timer();//类初始化的时候就实例化
- timing();
-
- }
- if("取消关机".equals(ActionCommand)) {
- System.out.println("取消关机");
- btn_3.setEnabled(false);
- timer.cancel(); //取消
- time_show = false;
- //System.out.println(Thread.currentThread().getName());
- }
-
- }
-
- }
- private void cutDown() {
- // TODO Auto-generated method stub
- key = JOptionPane.showInputDialog(this, "请输入倒计时关机的时间(m)", "输入框", 1);
- boolean b = (key != null && isNum(key));
- if(b) {
- final Long second = Long.valueOf(key);
- //如何动态的改变JLabel上面的内容 线程
- new Thread(new Runnable() {
-
- @Override
- public void run() {
- // TODO Auto-generated method stub
- Long i = second;
- while(i > 0 && time_show) {
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- showTime.setText("倒计时" + i + "s关机");
- i--;
- //System.out.println(i);
- }
- }
- }).start();;
- CountTimeTool.delayTime(second, timer);
- } else {
- JOptionPane.showMessageDialog(null,"必须输入整形数字");
- }
- }
- /**
- * 定时工具
- */
- public void timing() {
- key = JOptionPane.showInputDialog("请输入关机时间(格式xx:xx):");
- boolean b = (key != null && vaildate(key));
- if(!b) {
- JOptionPane.showMessageDialog(this,
- "定时时间不对", "系统信息", JOptionPane.ERROR_MESSAGE);
- return;
- }
- String time = getSystemTime();
-
- long second = getSecond(time, key);
- if(second <= 0) {
- JOptionPane.showMessageDialog(this,
- "定时时间不对", "系统信息", JOptionPane.ERROR_MESSAGE);
- return;
- }
- showTime.setText("关机时间:" + key);
- System.out.println(second);
- CountTimeTool.delayTime(second, timer);
- //System.out.println(second);
- }
- public boolean isNum(String str) {
- return str.matches("[0-9]+");
- }
- public static void main(String[] args) {
- SwingConsole.run(new CloseComputer(), "关机工具", 446, 450);
- }
-
- /**
- *
- * @param date
- * @return
- */
- public boolean vaildate(String date) {
- boolean b = false;
- String reg = "[0-9]{2}:[0-9]{2}";
- Pattern p = Pattern.compile(reg);
- Matcher m = p.matcher(date);
- b = m.matches();
-
- return b;
- }
- /**
- * 获取系统当前时间
- * @return
- */
- public String getSystemTime() {
- String time = "";
- Date date = new Date();
- // System.out.println(date);
- SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
- time = simpleDateFormat.format(date);
- //System.out.println(time);
- return time;
- }
- /**
- * 获取两个时间差 以m为单位
- * @param time1 系统时间
- * @param tim2 输入的时间
- * @return
- */
- public long getSecond(String time1, String time2) {
- long second = 0l;
-
- String[] time1_s = time1.split(":");
- String[] time2_s = time2.split(":");
-
- long hourse = Long.valueOf(time2_s[0]) - Long.valueOf(time1_s[0]);
- long minute = Long.valueOf(time2_s[1]) - Long.valueOf(time1_s[1]);
-
- if(hourse < 0) {
- return 0;
- }
-
- second = hourse * 3600 + (minute>0?minute*60:0);
- //System.out.println(hourse + ":::" + minute);
-
- return second;
- }
- }
复制代码
延时和关机类:
- package com.feng.shutdown;
- import java.util.Timer;
- import java.util.TimerTask;
- public class CountTimeTool {
- public static void delayTime(Long second,Timer timer) {
- // TODO Auto-generated method stub
- Long delay = 1000l;
- CutDownTool cut = new CutDownTool();
- //第一个参数为执行函数
- timer.schedule(cut, second*delay);
- }
- }
复制代码- package com.feng.shutdown;
- import java.io.IOException;
- import java.util.TimerTask;
- public class CutDownTool extends TimerTask {
- @Override
- public void run() {
- // TODO Auto-generated method stub
- //System.out.println("执行了关机············");
-
- try {
- Runtime.getRuntime().exec("shutdown -s"); //关机程序
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
复制代码
swing工具类:
- package com.feng.shutdown;
- import javax.swing.JFrame;
- import javax.swing.SwingUtilities;
- public class SwingConsole {
- public static void run(final JFrame frame, final String title, final int width, final int height) {
- //一个子线程
- SwingUtilities.invokeLater(new Runnable() {
-
- @Override
- public void run() {
- // TODO Auto-generated method stub
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.pack();
- frame.setTitle(title);
- frame.setSize(width, height);
-
- //显示
- frame.setVisible(true);
-
- }
- });
- };
- }
复制代码
我把swing程序到出,用bat批处理过,所以安装了jdk jre环境的电脑可以直接双击bat文件可以打开。
代码哪里不懂 可以问群主····
下章:秒表(事件加线程)
|
|