• java开发_windows下面的关机,重启,注销小程序_源码下载


    项目结构:

    运行效果:

    ========================================================

    代码部分:

    ========================================================

    /shutdownApp/src/com/b510/examples/Shutdown.java

     1 package com.b510.examples;
     2 
     3 import com.b510.examples.window.dao.MyWindowDao;
     4 import com.b510.examples.window.impl.MyWindowImpl;
     5 
     6 /**
     7  * @author Hongten
     8  * 
     9  * @time 2011-9-3 2011
    10  */
    11 public class Shutdown {
    12     public static void main(String[] args) {
    13         @SuppressWarnings("unused")
    14         MyWindowDao dao=new MyWindowImpl("Hongten-v1.0.定时关机");
    15     }
    16 }

    /shutdownApp/src/com/b510/examples/window/component/MyBox.java

     1 package com.b510.examples.window.component;
     2 
     3 import javax.swing.Box;
     4 
     5 /**
     6  * @author Hongten
     7  *
     8  * @time 2011-9-5  2011
     9  */
    10 public class MyBox {
    11     Box left=Box.createVerticalBox();
    12     Box center=Box.createVerticalBox();
    13     Box right=Box.createVerticalBox();
    14     Box middle_part=Box.createHorizontalBox();
    15     /**
    16      * @return the left
    17      */
    18     public Box getLeft() {
    19         return left;
    20     }
    21     /**
    22      * @param left the left to set
    23      */
    24     public void setLeft(Box left) {
    25         this.left = left;
    26     }
    27     /**
    28      * @return the center
    29      */
    30     public Box getCenter() {
    31         return center;
    32     }
    33     /**
    34      * @param center the center to set
    35      */
    36     public void setCenter(Box center) {
    37         this.center = center;
    38     }
    39     /**
    40      * @return the right
    41      */
    42     public Box getRight() {
    43         return right;
    44     }
    45     /**
    46      * @param right the right to set
    47      */
    48     public void setRight(Box right) {
    49         this.right = right;
    50     }
    51     /**
    52      * @return the middle_part
    53      */
    54     public Box getMiddle_part() {
    55         return middle_part;
    56     }
    57     /**
    58      * @param middlePart the middle_part to set
    59      */
    60     public void setMiddle_part(Box middlePart) {
    61         middle_part = middlePart;
    62     }
    63     
    64     
    65 }

    /shutdownApp/src/com/b510/examples/window/component/MyButton.java

      1 package com.b510.examples.window.component;
      2 
      3 import javax.swing.JButton;
      4 
      5 /**
      6  * @author Hongten
      7  * 
      8  * @time 2011-9-5 2011
      9  */
     10 public class MyButton {
     11     JButton shutdown = new JButton("关机");
     12     JButton restart = new JButton("重启");
     13     JButton logout = new JButton("注销");
     14     JButton clear = new JButton("清零");
     15     JButton cancel = new JButton("取消");
     16     JButton immshutdown = new JButton("立刻关机");
     17 
     18     /**
     19      * @return the shutdown
     20      */
     21     public JButton getShutdown() {
     22         return shutdown;
     23     }
     24 
     25     /**
     26      * @param shutdown
     27      *            the shutdown to set
     28      */
     29     public void setShutdown(JButton shutdown) {
     30         this.shutdown = shutdown;
     31     }
     32 
     33     /**
     34      * @return the restart
     35      */
     36     public JButton getRestart() {
     37         return restart;
     38     }
     39 
     40     /**
     41      * @param restart
     42      *            the restart to set
     43      */
     44     public void setRestart(JButton restart) {
     45         this.restart = restart;
     46     }
     47 
     48     /**
     49      * @return the logout
     50      */
     51     public JButton getLogout() {
     52         return logout;
     53     }
     54 
     55     /**
     56      * @param logout
     57      *            the logout to set
     58      */
     59     public void setLogout(JButton logout) {
     60         this.logout = logout;
     61     }
     62 
     63     /**
     64      * @return the clear
     65      */
     66     public JButton getClear() {
     67         return clear;
     68     }
     69 
     70     /**
     71      * @param clear
     72      *            the clear to set
     73      */
     74     public void setClear(JButton clear) {
     75         this.clear = clear;
     76     }
     77 
     78     /**
     79      * @return the cancel
     80      */
     81     public JButton getCancel() {
     82         return cancel;
     83     }
     84 
     85     /**
     86      * @param cancel
     87      *            the cancel to set
     88      */
     89     public void setCancel(JButton cancel) {
     90         this.cancel = cancel;
     91     }
     92 
     93     /**
     94      * @return the immshutdown
     95      */
     96     public JButton getImmshutdown() {
     97         return immshutdown;
     98     }
     99 
    100     /**
    101      * @param immshutdown
    102      *            the immshutdown to set
    103      */
    104     public void setImmshutdown(JButton immshutdown) {
    105         this.immshutdown = immshutdown;
    106     }
    107 
    108 }

    /shutdownApp/src/com/b510/examples/window/component/MyLabel.java

      1 package com.b510.examples.window.component;
      2 
      3 import javax.swing.JLabel;
      4 
      5 /**
      6  * @author Hongten
      7  * 
      8  * @time 2011-9-5 2011
      9  */
     10 public class MyLabel {
     11     // 标签 现在时间 ,设置小时,设置分钟,设置秒钟,概念,工作室,剩余
     12     JLabel time_now = new JLabel();
     13     JLabel set_hour = new JLabel("设置小时");
     14     JLabel set_min = new JLabel("设置分钟");
     15     JLabel set_sec = new JLabel("设置秒钟");
     16     JLabel concept = new JLabel("Hongten-B510");
     17     JLabel workspace = new JLabel("Hongtenzone@foxmain.com");
     18     JLabel surplus = new JLabel();
     19 
     20     /**
     21      * @return the time_now
     22      */
     23     public JLabel getTime_now() {
     24         return time_now;
     25     }
     26 
     27     /**
     28      * @param timeNow
     29      *            the time_now to set
     30      */
     31     public void setTime_now(JLabel timeNow) {
     32         time_now = timeNow;
     33     }
     34 
     35     /**
     36      * @return the set_hour
     37      */
     38     public JLabel getSet_hour() {
     39         return set_hour;
     40     }
     41 
     42     /**
     43      * @param setHour
     44      *            the set_hour to set
     45      */
     46     public void setSet_hour(JLabel setHour) {
     47         set_hour = setHour;
     48     }
     49 
     50     /**
     51      * @return the set_min
     52      */
     53     public JLabel getSet_min() {
     54         return set_min;
     55     }
     56 
     57     /**
     58      * @param setMin
     59      *            the set_min to set
     60      */
     61     public void setSet_min(JLabel setMin) {
     62         set_min = setMin;
     63     }
     64 
     65     /**
     66      * @return the set_sec
     67      */
     68     public JLabel getSet_sec() {
     69         return set_sec;
     70     }
     71 
     72     /**
     73      * @param setSec
     74      *            the set_sec to set
     75      */
     76     public void setSet_sec(JLabel setSec) {
     77         set_sec = setSec;
     78     }
     79 
     80     /**
     81      * @return the concept
     82      */
     83     public JLabel getConcept() {
     84         return concept;
     85     }
     86 
     87     /**
     88      * @param concept
     89      *            the concept to set
     90      */
     91     public void setConcept(JLabel concept) {
     92         this.concept = concept;
     93     }
     94 
     95     /**
     96      * @return the workspace
     97      */
     98     public JLabel getWorkspace() {
     99         return workspace;
    100     }
    101 
    102     /**
    103      * @param workspace
    104      *            the workspace to set
    105      */
    106     public void setWorkspace(JLabel workspace) {
    107         this.workspace = workspace;
    108     }
    109 
    110     /**
    111      * @return the surplus
    112      */
    113     public JLabel getSurplus() {
    114         return surplus;
    115     }
    116 
    117     /**
    118      * @param surplus
    119      *            the surplus to set
    120      */
    121     public void setSurplus(JLabel surplus) {
    122         this.surplus = surplus;
    123     }
    124 
    125 }

    /shutdownApp/src/com/b510/examples/window/component/MyPanel.java

     1 package com.b510.examples.window.component;
     2 
     3 import javax.swing.JPanel;
     4 
     5 /**
     6  * @author Hongten
     7  * 
     8  * @time 2011-9-5 2011
     9  */
    10 public class MyPanel {
    11     JPanel buttom=new JPanel();
    12 
    13     /**
    14      * @return the buttom
    15      */
    16     public JPanel getButtom() {
    17         return buttom;
    18     }
    19 
    20     /**
    21      * @param buttom the buttom to set
    22      */
    23     public void setButtom(JPanel buttom) {
    24         this.buttom = buttom;
    25     }
    26     
    27 }

    /shutdownApp/src/com/b510/examples/window/component/MyTextField.java

     1 package com.b510.examples.window.component;
     2 
     3 import javax.swing.JTextField;
     4 
     5 /**
     6  * @author Hongten
     7  * 
     8  * @time 2011-9-5 2011
     9  */
    10 public class MyTextField {
    11     // 文本框 时,分,秒
    12     static JTextField hour = new JTextField(10);
    13     static JTextField min = new JTextField(10);
    14     static JTextField sec = new JTextField(10);
    15 
    16     /**
    17      * @return the hour
    18      */
    19     public  JTextField getHour() {
    20         return hour;
    21     }
    22 
    23     /**
    24      * @param hour
    25      *            the hour to set
    26      */
    27     public  void setHour(JTextField hour) {
    28         MyTextField.hour = hour;
    29     }
    30 
    31     /**
    32      * @return the min
    33      */
    34     public  JTextField getMin() {
    35         return min;
    36     }
    37 
    38     /**
    39      * @param min
    40      *            the min to set
    41      */
    42     public  void setMin(JTextField min) {
    43         MyTextField.min = min;
    44     }
    45 
    46     /**
    47      * @return the sec
    48      */
    49     public  JTextField getSec() {
    50         return sec;
    51     }
    52 
    53     /**
    54      * @param sec
    55      *            the sec to set
    56      */
    57     public  void setSec(JTextField sec) {
    58         MyTextField.sec = sec;
    59     }
    60 
    61 }

    /shutdownApp/src/com/b510/examples/window/dao/MyWindowDao.java

     1 package com.b510.examples.window.dao;
     2 
     3 /**
     4  * @author Hongten
     5  * 
     6  * @time 2011-9-5 2011
     7  */
     8 public interface MyWindowDao {
     9 
    10     /**
    11      * 得到系统时间并显示 设置在窗体头部的信息
    12      * 
    13      * @return
    14      */
    15     public String now();
    16 
    17     /**
    18      * 得到剩余时间并显示
    19      * 
    20      * @return
    21      */
    22     public String surplus_time();
    23 
    24     /**
    25      * 关机操作
    26      */
    27     public void shutDown();
    28 
    29     /**
    30      * 重启操作
    31      */
    32     public void restart();
    33 
    34     /**
    35      * 注销操作
    36      */
    37     public void logOut();
    38 
    39     /**
    40      * 取消操作
    41      */
    42     public void cancel();
    43 
    44     /**
    45      * 我们先设置好时间,一旦时间一到, 就调用此方法 该方法中可以实现关机,重启,注销等操作
    46      */
    47     public void thenExecutive();
    48 
    49     /**
    50      * 自己定义的监听器类, 监听我们设置的关机或重启或注销时间是否到时了 如果到时,就执行相应的动作(关机或重启或注销)
    51      */
    52     public void myListener();
    53 
    54     /**
    55      * 时间监听器,得到系统时间和设置好时间后,得到剩余时间
    56      */
    57     public void timeListener();
    58 
    59     /**
    60      * 要执行的动作
    61      */
    62     public void myAction();
    63 
    64     /**
    65      * 判断三个文本域(hour,min,sec)中的时间是否合法
    66      * 
    67      * @return
    68      */
    69 
    70     public boolean judgeTime();
    71 
    72     /**
    73      * 判断是否为纯数字,如果是返回true,否则返回false
    74      * 
    75      * @param str
    76      *            为一个String类型
    77      * @return
    78      */
    79     public boolean isAllNumber(String str);
    80 }

    /shutdownApp/src/com/b510/examples/window/dao/MyWindowLayoutDao.java

     1 package com.b510.examples.window.dao;
     2 
     3 /**
     4  * @author Hongten
     5  * 
     6  * @time 2011-9-5 2011
     7  */
     8 public interface MyWindowLayoutDao {
     9     /**
    10      * 所有文本域属性设置
    11      */
    12     public void setTextFieldAddListener();
    13 
    14     /**
    15      * 所有的JLabel属性设置
    16      */
    17     public void setLabel();
    18 
    19     /**
    20      * 所有的JButton属性设置
    21      */
    22     public void setButtonAddListener();
    23 
    24     /**
    25      * 窗体布局设置
    26      */
    27     public void setLayout();
    28 }

    /shutdownApp/src/com/b510/examples/window/dao/SetMyLayoutDao.java

     1 package com.b510.examples.window.dao;
     2 
     3 import com.b510.examples.window.component.MyLabel;
     4 import com.b510.examples.window.component.MyTextField;
     5 
     6 /**
     7  * @author Hongten
     8  *
     9  * @time 2011-9-5  2011
    10  */
    11 public interface SetMyLayoutDao {
    12     /**
    13      * 三个文本域(hour,min,sec)属性的相关设置
    14      */
    15     public void setTextField(MyTextField myTextField);
    16     /**
    17      * 所有的JLabel属性设置
    18      */
    19     public void setLabel(MyLabel myLabel);
    20 }

    /shutdownApp/src/com/b510/examples/window/impl/MyWindowImpl.java

    这个是小程序的主要功能类,不过在时间处理方面不是很完善

    不过在之后的小闹钟程序中就得以完善了,大家可以看看小闹钟程序的时间处理:

    http://www.cnblogs.com/hongten/archive/2012/06/02/java_alarm.html

      1 package com.b510.examples.window.impl;
      2 
      3 import java.awt.BorderLayout;
      4 import java.awt.Color;
      5 import java.awt.Container;
      6 import java.awt.Dimension;
      7 import java.awt.GridLayout;
      8 import java.awt.Toolkit;
      9 import java.awt.event.ActionEvent;
     10 import java.awt.event.ActionListener;
     11 import java.io.IOException;
     12 import java.util.Calendar;
     13 import java.util.GregorianCalendar;
     14 
     15 import javax.swing.Box;
     16 import javax.swing.JFrame;
     17 import javax.swing.JOptionPane;
     18 import javax.swing.event.DocumentEvent;
     19 import javax.swing.event.DocumentListener;
     20 
     21 import com.b510.examples.window.component.MyBox;
     22 import com.b510.examples.window.component.MyButton;
     23 import com.b510.examples.window.component.MyLabel;
     24 import com.b510.examples.window.component.MyPanel;
     25 import com.b510.examples.window.component.MyTextField;
     26 import com.b510.examples.window.dao.MyWindowDao;
     27 import com.b510.examples.window.dao.MyWindowLayoutDao;
     28 import com.b510.examples.window.dao.SetMyLayoutDao;
     29 
     30 /**
     31  * @author Hongten
     32  * 
     33  * @time 2011-9-3 2011
     34  */
     35 public class MyWindowImpl extends JFrame implements ActionListener,
     36         DocumentListener, MyWindowDao, MyWindowLayoutDao {
     37     private static final long serialVersionUID = -7018374117422042620L;
     38 
     39     MyTextField myTextField = new MyTextField();
     40     MyLabel myLabel = new MyLabel();
     41     MyButton myButton = new MyButton();
     42     MyBox myBox = new MyBox();
     43     MyPanel buttom = new MyPanel();
     44     Container container;
     45     int flag;
     46 
     47     public MyWindowImpl(String title) {
     48         super(title);// 设置标题
     49         init();
     50     }
     51 
     52     /**
     53      * 初始化窗体
     54      */
     55     public void init() {
     56         this.setVisible(true);
     57         this.setResizable(false);// 设置大小不可变
     58         this.setSize(525, 215);
     59         Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
     60         this.setLocation(screen.width - 525, 0);// 设置窗体的初始位置
     61         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 设置窗体可以关闭
     62         SetMyLayoutDao setMyLayout = new SetMyLayoutImpl();
     63         setLabel();// JLabel标签属性设置
     64         timeListener();// 系统时间监听器
     65         setMyLayout.setTextField(myTextField);
     66         setMyLayout.setLabel(myLabel);
     67         setTextFieldAddListener();// JTextField文本域属性设置
     68         setButtonAddListener();// JButton按钮属性设置
     69         setLayout();// 窗体布局设置
     70     }
     71 
     72     /**
     73      * 设置文本域的监听器
     74      */
     75     public void setTextFieldAddListener() {
     76         // 注入监听器
     77         myTextField.getHour().addActionListener(this);
     78         (myTextField.getHour().getDocument()).addDocumentListener(this);
     79 
     80         // 注入监听器
     81         myTextField.getMin().addActionListener(this);
     82         (myTextField.getMin().getDocument()).addDocumentListener(this);
     83 
     84         // 注入监听器
     85         myTextField.getSec().addActionListener(this);
     86         (myTextField.getSec().getDocument()).addDocumentListener(this);
     87 
     88     }
     89 
     90     /**
     91      * 所有的JLabel属性设置
     92      */
     93     public void setLabel() {
     94         // 现在时间 头部显示
     95         myLabel.getTime_now().setText("SystemTime:" + now());
     96     }
     97 
     98     /**
     99      * 所有的JButton属性设置
    100      */
    101     public void setButtonAddListener() {
    102         // 设置按钮的一些属性,可用,添加监听器
    103         myButton.getShutdown().setEnabled(false);// 关机
    104         myButton.getShutdown().addActionListener(this);
    105         myButton.getRestart().setEnabled(false);// 重启
    106         myButton.getRestart().addActionListener(this);
    107         myButton.getLogout().setEnabled(false);// 注销
    108         myButton.getLogout().addActionListener(this);
    109         myButton.getClear().setEnabled(false);// 清零
    110         myButton.getClear().addActionListener(this);
    111         myButton.getCancel().setEnabled(false);// 取消
    112         myButton.getCancel().addActionListener(this);
    113         myButton.getImmshutdown().addActionListener(this);// 立刻关机
    114     }
    115 
    116     /**
    117      * 窗体布局设置
    118      */
    119     public void setLayout() {
    120         // 左边布局
    121         myBox.getLeft().add(myLabel.getSet_hour());
    122         myBox.getLeft().add(Box.createVerticalStrut(20));
    123         myBox.getLeft().add(myLabel.getSet_min());
    124         myBox.getLeft().add(Box.createVerticalStrut(20));
    125         myBox.getLeft().add(myLabel.getSet_sec());
    126         // 中间布局
    127 
    128         myBox.getCenter().add(myTextField.getHour());
    129         myBox.getCenter().add(Box.createVerticalStrut(9));
    130         myBox.getCenter().add(myTextField.getMin());
    131         myBox.getCenter().add(Box.createVerticalStrut(9));
    132         myBox.getCenter().add(myTextField.getSec());
    133 
    134         // 右边布局
    135         myBox.getRight().add(myButton.getShutdown());
    136         myBox.getRight().add(Box.createVerticalStrut(10));
    137         myBox.getRight().add(myButton.getRestart());
    138         myBox.getRight().add(Box.createVerticalStrut(10));
    139         myBox.getRight().add(myButton.getLogout());
    140         validate();
    141         // 中部
    142         myBox.getMiddle_part().add(myBox.getLeft());
    143         myBox.getMiddle_part().add(Box.createHorizontalStrut(5));
    144         myBox.getMiddle_part().add(myBox.getCenter());
    145         myBox.getMiddle_part().add(Box.createHorizontalStrut(5));
    146         myBox.getMiddle_part().add(myBox.getRight());
    147         myBox.getMiddle_part().validate();
    148         // 底部
    149         buttom.getButtom().setLayout(new GridLayout(2, 3));// 采用网格布局 2行 ,3列
    150         buttom.getButtom().setBackground(Color.white);
    151         buttom.getButtom().add(myButton.getClear());
    152         buttom.getButtom().add(myButton.getCancel());
    153         buttom.getButtom().add(myButton.getImmshutdown());
    154         buttom.getButtom().add(myLabel.getConcept());
    155         buttom.getButtom().add(myLabel.getSurplus());
    156         buttom.getButtom().add(myLabel.getWorkspace());
    157         buttom.getButtom().validate();
    158         // 容器
    159         container = getContentPane();
    160         container.setLayout(new BorderLayout());
    161         container.add(myLabel.getTime_now(), BorderLayout.NORTH);
    162         container.add(myBox.getMiddle_part(), BorderLayout.CENTER);
    163         container.add(buttom.getButtom(), BorderLayout.SOUTH);
    164         container.setBackground(Color.white);
    165         container.validate();
    166     }
    167 
    168     /**
    169      * 得到系统时间并显示 设置在窗体头部的信息
    170      * 
    171      * @return
    172      */
    173     public String now() {
    174         Calendar cal = Calendar.getInstance();
    175         int year = cal.get(Calendar.YEAR);
    176         int month = cal.get(Calendar.MONTH) + 1;
    177         int day = cal.get(Calendar.DAY_OF_MONTH);
    178         int hour = cal.get(Calendar.HOUR_OF_DAY);
    179         int min = cal.get(Calendar.MINUTE);
    180         int sec = cal.get(Calendar.SECOND);
    181         String current = new String(year + "-" + month + "-" + day + " " + hour
    182                 + ":" + min + ":" + sec);
    183         myLabel.getTime_now().setText("SystemTime:" + current);
    184         return current;
    185     }
    186 
    187     /**
    188      * 得到剩余时间并显示
    189      * 
    190      * 由于我们的文本域(hour,min,sec)都有客户端直接输入,可能输入的不是纯数字 这样我们在监听的时候, 比如:Integer
    191      * int_hour = Integer.parseInt(hour.getText()); 就会报错,为了解决这个错误,我们制定了以下方法,
    192      * 判断文本域中的内容是否为纯数字,如果不是,则出现提示信息, 并且将文本内容设置为当前相应的时间
    193      * 
    194      * @return
    195      */
    196     public String surplus_time() {
    197         Calendar cal = Calendar.getInstance();// new GregorianCalendar();
    198         int sur_hour = cal.get(Calendar.HOUR_OF_DAY);
    199         int sur_min = cal.get(Calendar.MINUTE);
    200         int sur_sec = cal.get(Calendar.SECOND);
    201         if (!isAllNumber(myTextField.getHour().getText())) {
    202             JOptionPane.showMessageDialog(this,
    203                     "时间设置满足:0=<小时<24,0=<分钟<60,0=<秒<60!", "温馨提示",
    204                     JOptionPane.WARNING_MESSAGE);
    205             myTextField.getHour().setText(String.valueOf(sur_hour));// 设置当前小时
    206             myTextField.getHour().requestFocus(); // 文本框 时 获得焦点
    207             return null;
    208         } else if (!isAllNumber(myTextField.getMin().getText())) {
    209             JOptionPane.showMessageDialog(this,
    210                     "时间设置满足:0=<小时<24,0=<分钟<60,0=<秒<60!", "温馨提示",
    211                     JOptionPane.WARNING_MESSAGE);
    212             myTextField.getMin().setText(String.valueOf(sur_min));// 设置当前分钟
    213             myTextField.getMin().requestFocus(); // 文本框 时 获得焦点
    214             return null;
    215         } else if (!isAllNumber(myTextField.getSec().getText())) {
    216             JOptionPane.showMessageDialog(this,
    217                     "时间设置满足:0=<小时<24,0=<分钟<60,0=<秒<60!", "温馨提示",
    218                     JOptionPane.WARNING_MESSAGE);
    219             myTextField.getSec().setText(String.valueOf(sur_sec));// 设置当前秒钟
    220             myTextField.getSec().requestFocus(); // 文本框 时 获得焦点
    221             return null;
    222         } else {
    223             Integer int_hour = Integer
    224                     .parseInt(myTextField.getHour().getText());
    225             Integer int_min = Integer.parseInt(myTextField.getMin().getText());
    226             Integer int_sec = Integer.parseInt(myTextField.getSec().getText());
    227             int hour = int_hour.intValue() - sur_hour;
    228             int min = int_min.intValue() - sur_min;
    229             int sec = int_sec.intValue() - sur_sec;
    230             if (sec < 0) {
    231                 if (min > 0 && hour >= 0) {
    232                     sec += 60;
    233                     min = min - 1;
    234                 }
    235                 if (min == 0 && hour > 0) {
    236                     min += 60;
    237                     hour -= 1;
    238                 }
    239             } else if (min < 0) {
    240                 if (hour > 0) {
    241                     min += 60;
    242                     hour = hour - 1;
    243                 }
    244             } else if (hour == 0 && min == 0 && sec < 0) {
    245                 myLabel.getSurplus().setVisible(false);
    246             }
    247             String sur = new String("剩余:" + hour + "时" + min + "分" + sec + "秒");
    248             myLabel.getSurplus().setText(sur);
    249             return sur;
    250         }
    251 
    252     }
    253 
    254     /**
    255      * 重写DocumentListener的方法 该方法监听三个文本域(hour,min,sec)是否已经填写好
    256      * 如果填写好,一些按钮就会被触发可用,否则不可用
    257      */
    258     @Override
    259     public void changedUpdate(DocumentEvent e) {// 
    260         if (myTextField.getHour().getText().equals("")
    261                 || myTextField.getMin().getText().equals("")
    262                 || myTextField.getSec().getText().equals("")) {
    263             myButton.getShutdown().setEnabled(false);
    264             myButton.getRestart().setEnabled(false);
    265             myButton.getLogout().setEnabled(false);
    266             myButton.getClear().setEnabled(false);
    267         } else {
    268             myButton.getShutdown().setEnabled(true);
    269             myButton.getRestart().setEnabled(true);
    270             myButton.getLogout().setEnabled(true);
    271             myButton.getClear().setEnabled(true);
    272         }
    273     }
    274 
    275     /**
    276      * 重写DocumentListener的方法
    277      */
    278     @Override
    279     public void insertUpdate(DocumentEvent e) {
    280         changedUpdate(e);
    281 
    282     }
    283 
    284     /**
    285      * 重写DocumentListener的方法
    286      */
    287     @Override
    288     public void removeUpdate(DocumentEvent e) {
    289         changedUpdate(e);
    290     }
    291 
    292     /**
    293      * 关机操作
    294      */
    295     public void shutDown() {
    296         try {
    297             Runtime.getRuntime().exec("shutdown.exe -s -t 1");
    298         } catch (IOException e) {
    299             JOptionPane.showMessageDialog(this, "关机执行失败!");
    300         }
    301     }
    302 
    303     /**
    304      * 重启操作
    305      */
    306     public void restart() {
    307         try {
    308             Runtime.getRuntime().exec("shutdown.exe -r -t 1");
    309         } catch (IOException e) {
    310             JOptionPane.showMessageDialog(this, "重启执行失败!");
    311         }
    312     }
    313 
    314     /**
    315      * 注销操作
    316      */
    317     public void logOut() {
    318         try {
    319             Runtime.getRuntime().exec("shutdown.exe -l");
    320         } catch (IOException e) {
    321             JOptionPane.showMessageDialog(this, "注销执行失败!");
    322         }
    323     }
    324 
    325     /**
    326      * 取消操作
    327      */
    328     public void cancel() {
    329         try {
    330             Runtime.getRuntime().exec("shutdown.exe -a");
    331         } catch (IOException e) {
    332             JOptionPane.showMessageDialog(this, "取消执行失败!");
    333         }
    334     }
    335 
    336     /**
    337      * 我们先设置好时间,一旦时间一到, 就调用此方法 该方法中可以实现关机,重启,注销等操作
    338      */
    339     public void thenExecutive() {
    340         Calendar cal = new GregorianCalendar();
    341         int then_hour = cal.get(Calendar.HOUR_OF_DAY);
    342         int then_min = cal.get(Calendar.MINUTE);
    343         int then_sec = cal.get(Calendar.SECOND);
    344         // 这里要说明的是:
    345         // 由于我们的文本域(hour,min,sec)都有客户端直接输入,可能输入的不是纯数字
    346         // 这样我们在监听的时候,
    347         // 比如:Integer int_hour = Integer.parseInt(hour.getText());
    348         // 就会报错,为了解决这个错误,我们制定了以下方法,
    349         // 判断文本域中的内容是否为纯数字,如果不是,则出现提示信息,
    350         // 并且将文本内容设置为当前相应的时间
    351         if (!isAllNumber(myTextField.getHour().getText())) {
    352             JOptionPane.showMessageDialog(this,
    353                     "时间设置满足:0=<小时<24,0=<分钟<60,0=<秒<60!", "温馨提示",
    354                     JOptionPane.WARNING_MESSAGE);
    355             myTextField.getHour().setText(String.valueOf(then_hour));// 设置当前小时
    356             myTextField.getHour().requestFocus(); // 文本框 时 获得焦点
    357         } else if (!isAllNumber(myTextField.getMin().getText())) {
    358             JOptionPane.showMessageDialog(this,
    359                     "时间设置满足:0=<小时<24,0=<分钟<60,0=<秒<60!", "温馨提示",
    360                     JOptionPane.WARNING_MESSAGE);
    361             myTextField.getMin().setText(String.valueOf(then_min));// 设置当前分钟
    362             myTextField.getMin().requestFocus(); // 文本框 时 获得焦点
    363         } else if (!isAllNumber(myTextField.getSec().getText())) {
    364             JOptionPane.showMessageDialog(this,
    365                     "时间设置满足:0=<小时<24,0=<分钟<60,0=<秒<60!", "温馨提示",
    366                     JOptionPane.WARNING_MESSAGE);
    367             myTextField.getSec().setText(String.valueOf(then_sec));// 设置当前秒钟
    368             myTextField.getSec().requestFocus(); // 文本框 时 获得焦点
    369         } else {
    370             Integer int_hour = Integer
    371                     .parseInt(myTextField.getHour().getText());
    372             Integer int_min = Integer.parseInt(myTextField.getMin().getText());
    373             Integer int_sec = Integer.parseInt(myTextField.getSec().getText());
    374 
    375             if (then_hour == int_hour.intValue()
    376                     && then_min == int_min.intValue()
    377                     && then_sec == int_sec.intValue()) {
    378                 if (flag == 1) {
    379                     // 关机
    380                     shutDown();
    381                     myButton.getCancel().setEnabled(true);
    382                     myButton.getClear().setEnabled(false);
    383                 }
    384                 if (flag == 2) {
    385                     // 重启
    386                     restart();
    387                     myButton.getCancel().setEnabled(true);
    388                     myButton.getClear().setEnabled(false);
    389                 }
    390                 if (flag == 3) {
    391                     // 注销
    392                     logOut();
    393                     myButton.getCancel().setEnabled(true);
    394                     myButton.getClear().setEnabled(false);
    395                 }
    396             }
    397         }
    398     }
    399 
    400     /**
    401      * 重写ActionListener的方法 该方法中可以监听: 立刻关机,到时关机,重启,注销,取消,清零等动作
    402      */
    403     @Override
    404     public void actionPerformed(ActionEvent e) {
    405         int action_hour = Integer.parseInt(myTextField.getHour().getText());
    406         int action_min = Integer.parseInt(myTextField.getMin().getText());
    407         int action_sec = Integer.parseInt(myTextField.getSec().getText());
    408         // 立刻关机
    409         if (e.getSource() == myButton.getImmshutdown()) {
    410             shutDown();
    411         }
    412         // 到时候才关机
    413         if (e.getSource() == myButton.getShutdown()) {
    414             myButton.getShutdown().setEnabled(false);
    415             myButton.getRestart().setEnabled(false);
    416             myButton.getLogout().setEnabled(false);
    417             myButton.getClear().setEnabled(false);
    418             myButton.getCancel().setEnabled(true);
    419             myLabel.getSurplus().setVisible(true);
    420             myTextField.getHour().setEnabled(false);
    421             myTextField.getMin().setEnabled(false);
    422             myTextField.getSec().setEnabled(false);
    423             flag = 1;
    424             if (action_hour >= 0 && action_hour <= 23 && action_min >= 0
    425                     && action_min <= 59 && action_sec >= 0 && action_sec <= 59) {
    426                 myAction();
    427             } else {
    428                 JOptionPane.showMessageDialog(this,
    429                         "时间设置满足:0=<小时<24,0=<分钟<60,0=<秒<60!", "温馨提示",
    430                         JOptionPane.WARNING_MESSAGE);
    431                 myTextField.getHour().requestFocus(); // 文本框 时 获得焦点
    432             }
    433         }
    434         // 到时候才重启
    435         if (e.getSource() == myButton.getRestart()) {
    436             myButton.getShutdown().setEnabled(false);
    437             myButton.getRestart().setEnabled(false);
    438             myButton.getLogout().setEnabled(false);
    439             myButton.getClear().setEnabled(false);
    440             myButton.getCancel().setEnabled(true);
    441             myLabel.getSurplus().setVisible(true);
    442             myTextField.getHour().setEnabled(false);
    443             myTextField.getMin().setEnabled(false);
    444             myTextField.getSec().setEnabled(false);
    445             flag = 2;
    446             if (action_hour >= 0 && action_hour <= 23 && action_min >= 0
    447                     && action_min <= 59 && action_sec >= 0 && action_sec <= 59) {
    448                 myAction();
    449             } else {
    450                 JOptionPane.showMessageDialog(this,
    451                         "时间设置满足:0=<小时<24,0=<分钟<60,0=<秒<60!", "温馨提示",
    452                         JOptionPane.WARNING_MESSAGE);
    453                 myTextField.getHour().requestFocus(); // 文本框 时 获得焦点
    454             }
    455         }
    456         // 到时候才注销
    457         if (e.getSource() == myButton.getLogout()) {
    458             myButton.getShutdown().setEnabled(false);
    459             myButton.getRestart().setEnabled(false);
    460             myButton.getLogout().setEnabled(false);
    461             myButton.getClear().setEnabled(false);
    462             myButton.getCancel().setEnabled(true);
    463             myLabel.getSurplus().setVisible(true);
    464             myTextField.getHour().setEnabled(false);
    465             myTextField.getMin().setEnabled(false);
    466             myTextField.getSec().setEnabled(false);
    467             flag = 3;
    468             if (action_hour >= 0 && action_hour <= 23 && action_min >= 0
    469                     && action_min <= 59 && action_sec >= 0 && action_sec <= 59) {
    470                 myAction();
    471             } else {
    472                 JOptionPane.showMessageDialog(this,
    473                         "时间设置满足:0=<小时<24,0=<分钟<60,0=<秒<60!", "温馨提示",
    474                         JOptionPane.WARNING_MESSAGE);
    475                 myButton.getClear().setEnabled(true);
    476                 myTextField.getHour().requestFocus(); // 文本框 时 获得焦点
    477             }
    478         }
    479         // 取消
    480         if (e.getSource() == myButton.getCancel()) {
    481             cancel();
    482             myTextField.getHour().requestFocus();
    483             myButton.getShutdown().setEnabled(true);
    484             myButton.getRestart().setEnabled(true);
    485             myButton.getLogout().setEnabled(true);
    486             myButton.getClear().setEnabled(true);
    487             myButton.getCancel().setEnabled(false);
    488             myLabel.getSurplus().setVisible(false);
    489             myTextField.getHour().setEnabled(true);
    490             myTextField.getMin().setEnabled(true);
    491             myTextField.getSec().setEnabled(true);
    492         }
    493         // 清零
    494         if (e.getSource() == myButton.getClear()) {
    495             myTextField.getHour().setText("0");
    496             myTextField.getMin().setText("0");
    497             myTextField.getSec().setText("0");
    498             myTextField.getHour().requestFocus();
    499             myButton.getClear().setEnabled(false);
    500             myLabel.getSurplus().setVisible(false);
    501         }
    502     }
    503 
    504     /**
    505      * 自己定义的监听器类, 监听我们设置的关机或重启或注销时间是否到时了 如果到时,就执行相应的动作(关机或重启或注销)
    506      */
    507     public void myListener() {
    508         new Thread(new Runnable() {
    509             public void run() {
    510                 while (true) {
    511                     try {
    512                         Thread.sleep(1000);
    513                     } catch (Exception e) {
    514                         e.printStackTrace();
    515                     }
    516                     thenExecutive();// 关机或重启或注销
    517                 }
    518             }
    519         }).start();
    520     }
    521 
    522     /**
    523      * 时间监听器,得到系统时间和设置好时间后,得到剩余时间
    524      */
    525     public void timeListener() {
    526         new Thread(new Runnable() {// 设置一个线程
    527                     public void run() {
    528                         while (true) {
    529                             try {
    530                                 Thread.sleep(1000);
    531                             } catch (Exception e) {
    532                                 e.printStackTrace();
    533                             }
    534                             now();// 得到系统时间
    535                             surplus_time();// 得到剩余时间
    536                         }
    537                     }
    538                 }).start();
    539     }
    540 
    541     /**
    542      * 要执行的动作
    543      */
    544     public void myAction() {
    545         if (judgeTime() == false) {
    546             JOptionPane.showMessageDialog(this, "时间设定错误");
    547             myLabel.getSurplus().setVisible(false);
    548             myButton.getShutdown().setEnabled(true);
    549             myButton.getRestart().setEnabled(true);
    550             myButton.getLogout().setEnabled(true);
    551         } else {
    552             myListener();
    553         }
    554     }
    555 
    556     /**
    557      * 判断三个文本域(hour,min,sec)中的时间是否合法
    558      * 
    559      * @return
    560      */
    561     public boolean judgeTime() {
    562         boolean boo = true;
    563         Calendar cal = new GregorianCalendar();
    564         int judge_hour = cal.get(Calendar.HOUR_OF_DAY);
    565         int judge_min = cal.get(Calendar.MINUTE);
    566         int judge_sec = cal.get(Calendar.SECOND);
    567         int int_hour = Integer.parseInt(myTextField.getHour().getText());
    568         int int_min = Integer.parseInt(myTextField.getMin().getText());
    569         int int_sec = Integer.parseInt(myTextField.getSec().getText());
    570 
    571         if (int_hour < judge_hour) {
    572             boo = false;
    573         } else {
    574             if (int_hour == judge_hour) {
    575                 if (int_min < judge_min) {
    576                     boo = false;
    577                 } else if (int_min == judge_min) {
    578                     if (int_sec < judge_sec + 5) {
    579                         boo = false;
    580                     }
    581                 }
    582             }
    583         }
    584         return boo;
    585     }
    586 
    587     /**
    588      * 判断是否为纯数字,如果是返回true,否则返回false
    589      * 
    590      * @param str
    591      *            为一个String类型
    592      * @return
    593      */
    594     public boolean isAllNumber(String str) {
    595         if (str.matches("^[0-9]+$")) {
    596             return true;
    597         } else {
    598             return false;
    599         }
    600     }
    601 }

    /shutdownApp/src/com/b510/examples/window/impl/SetMyLayoutImpl.java

    package com.b510.examples.window.impl;
    
    import java.awt.Color;
    import java.awt.Font;
    import java.util.Calendar;
    
    import javax.swing.JFrame;
    
    import com.b510.examples.window.component.MyLabel;
    import com.b510.examples.window.component.MyTextField;
    import com.b510.examples.window.dao.SetMyLayoutDao;
    
    /**
     * @author Hongten
     * 
     * @time 2011-9-5 2011
     */
    public class SetMyLayoutImpl extends JFrame implements SetMyLayoutDao {
    
        private static final long serialVersionUID = -6414864615563143323L;
    
        /**
         * 三个文本域(hour,min,sec)属性的相关设置
         */
        public void setTextField(MyTextField myTextField) {
            // 小时文本域 位置:中间
            myTextField.getHour().setColumns(4);// 4列
            myTextField.getHour().setForeground(Color.blue);
            // 初始化小时文本域,设置为系统当前小时数
            myTextField.getHour().setText(
                    String
                            .valueOf(Calendar.getInstance().get(
                                    Calendar.HOUR_OF_DAY)));
            // 分钟文本域 位置:中间
            myTextField.getMin().setColumns(4);// 4列
            myTextField.getMin().setForeground(Color.blue);
            // 初始化分钟文本域,设置值为0
            myTextField.getMin().setText("0");
            // 秒钟文本域 位置:中间
            myTextField.getSec().setColumns(4);// 4列
            myTextField.getSec().setForeground(Color.blue);
            // 初始化秒钟文本域,设置为系统当前秒钟数
            myTextField.getSec().setText(
                    String.valueOf(Calendar.getInstance().get(Calendar.SECOND)));
        }
    
        /**
         * 所有的JLabel属性设置
         */
        public void setLabel(MyLabel myLabel) {
            // 现在时间 头部显示
            myLabel.getTime_now().setForeground(Color.red);
            myLabel.getTime_now().setFont(new Font("楷体", Font.BOLD, 20));
    
            myLabel.getSurplus().setForeground(Color.red);
            myLabel.getSurplus().setFont(new Font(null, Font.BOLD, 16));
            myLabel.getSurplus().setVisible(false);// 初始化是为不可见
    
            myLabel.getConcept().setForeground(Color.gray);
            myLabel.getWorkspace().setForeground(Color.gray);
    
        }
    }

    /shutdownApp/src/com/b510/examples/window/listener/MyButtonListener.java

     1 package com.b510.examples.window.listener;
     2 
     3 import java.awt.event.ActionEvent;
     4 import java.awt.event.ActionListener;
     5 
     6 /**
     7  * @author Hongten
     8  *
     9  * @time 2011-9-5  2011
    10  */
    11 public class MyButtonListener implements ActionListener{
    12 
    13     @Override
    14     public void actionPerformed(ActionEvent e) {
    15         
    16     }
    17 
    18 }

    源码下载:https://files.cnblogs.com/hongten/java_shutdownApp.zip

     I'm Hongten

  • 相关阅读:
    带返回值的多线程
    ORA-12516 "TNS监听程序找不到符合协议堆栈要求的可用处理程序" 解决方案
    Java后端WebSocket的Tomcat实现
    Jackson将对象转换为json字符串时,设置默认的时间格式
    java构造器和构建器
    java静态工厂
    离散-理解只有 才
    数据结构之链式表
    数据结构之顺序线性表
    期末作业验收
  • 原文地址:https://www.cnblogs.com/hongten/p/hongten_java_shutdown_process.html
Copyright © 2020-2023  润新知