Java实现定时关机

/*
 * MainFrame.java
 *
 * Created on __DATE__, __TIME__
 */

package com.main;

import java.awt.AWTException;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.logging.Logger;

import javax.swing.ImageIcon;
import javax.swing.JFrame;

/**
 *
 * @author __USER__
 */
public class MainFrame extends javax.swing.JFrame implements Runnable {

 private String dateTime;

 private SystemTray systemTray;

 private ImageIcon icon;

 /** Creates new form MainFrame */
 public MainFrame() {

  initComponents();
  
  initDateTime();
 }

 private void initDateTime(){
  
  Calendar calendar = Calendar.getInstance();
  
  int year = calendar.get(Calendar.YEAR);
  
  int month = calendar.get(Calendar.MONTH) + 1;
  
  int day = calendar.get(Calendar.DAY_OF_MONTH);
  
  int hour = calendar.get(Calendar.HOUR_OF_DAY);
  
  int minute = calendar.get(Calendar.MINUTE);
  
  int second = calendar.get(Calendar.SECOND);
  
  this.jTextFieldYear.setText(Integer.toString(year));
  
  this.jTextFieldMonth.setText(Integer.toString(month));
  
  this.jTextFieldDay.setText(Integer.toString(day));
  
  this.jTextFieldHour.setText(Integer.toString(hour));
  
  this.jTextFieldMinute.setText(Integer.toString(minute));
  
  this.jTextFieldSecond.setText(Integer.toString(second));
 }
 /**
  * This method is called from within the constructor to initialize the form.
  * WARNING: Do NOT modify this code. The content of this method is always
  * regenerated by the Form Editor.
  */
 // GEN-BEGIN:initComponents
 // <editor-fold defaultstate="collapsed" desc="Generated Code">
 private void initComponents() {

  jLabelYear = new javax.swing.JLabel();
  jTextFieldYear = new javax.swing.JTextField();
  jLabelMonth = new javax.swing.JLabel();
  jTextFieldMonth = new javax.swing.JTextField();
  jLabelDay = new javax.swing.JLabel();
  jTextFieldDay = new javax.swing.JTextField();
  jLabelMonth2 = new javax.swing.JLabel();
  jTextFieldHour = new javax.swing.JTextField();
  jTextFieldMinute = new javax.swing.JTextField();
  jLabelMinute = new javax.swing.JLabel();
  jTextFieldSecond = new javax.swing.JTextField();
  jLabelSecond = new javax.swing.JLabel();
  jButtonSet = new javax.swing.JButton();
  jLabelSetDateTime = new javax.swing.JLabel();

  setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);

  jLabelYear.setText("/u5e74/uff1a");

  jLabelMonth.setText("/u6708/uff1a");

  jLabelDay.setText("/u65e5/uff1a");

  jLabelMonth2.setText("/u65f6/uff1a");

  jLabelMinute.setText("/u5206/uff1a");

  jLabelSecond.setText("/u79d2/uff1a");

  jButtonSet.setText("/u8bbe/u7f6e/u5173/u673a/u65f6/u95f4");
  jButtonSet.addActionListener(new java.awt.event.ActionListener() {
   public void actionPerformed(java.awt.event.ActionEvent evt) {
    jButtonSetActionPerformed(evt);
   }
  });

  javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
    getContentPane());
  getContentPane().setLayout(layout);
  layout
    .setHorizontalGroup(layout
      .createParallelGroup(
        javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(
        layout
          .createSequentialGroup()
          .addGroup(
            layout
              .createParallelGroup(
                javax.swing.GroupLayout.Alignment.LEADING)
              .addGroup(
                layout
                  .createSequentialGroup()
                  .addGap(
                    36,
                    36,
                    36)
                  .addComponent(
                    jButtonSet)
                  .addGap(
                    52,
                    52,
                    52)
                  .addComponent(
                    jLabelSetDateTime))
              .addGroup(
                layout
                  .createSequentialGroup()
                  .addGap(
                    100,
                    100,
                    100)
                  .addGroup(
                    layout
                      .createParallelGroup(
                        javax.swing.GroupLayout.Alignment.TRAILING)
                      .addGroup(
                        layout
                          .createSequentialGroup()
                          .addComponent(
                            jLabelYear)
                          .addPreferredGap(
                            javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                          .addComponent(
                            jTextFieldYear,
                            javax.swing.GroupLayout.PREFERRED_SIZE,
                            87,
                            javax.swing.GroupLayout.PREFERRED_SIZE))
                      .addGroup(
                        layout
                          .createSequentialGroup()
                          .addComponent(
                            jLabelMonth2)
                          .addPreferredGap(
                            javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                          .addComponent(
                            jTextFieldHour,
                            javax.swing.GroupLayout.PREFERRED_SIZE,
                            87,
                            javax.swing.GroupLayout.PREFERRED_SIZE)))
                  .addGap(
                    63,
                    63,
                    63)
                  .addGroup(
                    layout
                      .createParallelGroup(
                        javax.swing.GroupLayout.Alignment.TRAILING)
                      .addGroup(
                        layout
                          .createSequentialGroup()
                          .addComponent(
                            jLabelMonth)
                          .addPreferredGap(
                            javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                          .addComponent(
                            jTextFieldMonth,
                            javax.swing.GroupLayout.PREFERRED_SIZE,
                            87,
                            javax.swing.GroupLayout.PREFERRED_SIZE))
                      .addGroup(
                        layout
                          .createSequentialGroup()
                          .addComponent(
                            jLabelMinute)
                          .addPreferredGap(
                            javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                          .addComponent(
                            jTextFieldMinute,
                            javax.swing.GroupLayout.PREFERRED_SIZE,
                            87,
                            javax.swing.GroupLayout.PREFERRED_SIZE)))
                  .addGap(
                    45,
                    45,
                    45)
                  .addGroup(
                    layout
                      .createParallelGroup(
                        javax.swing.GroupLayout.Alignment.TRAILING)
                      .addGroup(
                        layout
                          .createSequentialGroup()
                          .addComponent(
                            jLabelDay)
                          .addPreferredGap(
                            javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                          .addComponent(
                            jTextFieldDay,
                            javax.swing.GroupLayout.PREFERRED_SIZE,
                            87,
                            javax.swing.GroupLayout.PREFERRED_SIZE))
                      .addGroup(
                        layout
                          .createSequentialGroup()
                          .addComponent(
                            jLabelSecond)
                          .addPreferredGap(
                            javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                          .addComponent(
                            jTextFieldSecond,
                            javax.swing.GroupLayout.PREFERRED_SIZE,
                            87,
                            javax.swing.GroupLayout.PREFERRED_SIZE)))))
          .addContainerGap(141, Short.MAX_VALUE)));
  layout
    .setVerticalGroup(layout
      .createParallelGroup(
        javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(
        layout
          .createSequentialGroup()
          .addGap(54, 54, 54)
          .addGroup(
            layout
              .createParallelGroup(
                javax.swing.GroupLayout.Alignment.LEADING)
              .addGroup(
                layout
                  .createSequentialGroup()
                  .addGap(
                    2,
                    2,
                    2)
                  .addGroup(
                    layout
                      .createParallelGroup(
                        javax.swing.GroupLayout.Alignment.BASELINE)
                      .addComponent(
                        jLabelMonth)
                      .addComponent(
                        jTextFieldMonth,
                        javax.swing.GroupLayout.PREFERRED_SIZE,
                        javax.swing.GroupLayout.DEFAULT_SIZE,
                        javax.swing.GroupLayout.PREFERRED_SIZE)
                      .addComponent(
                        jLabelYear)
                      .addComponent(
                        jTextFieldYear,
                        javax.swing.GroupLayout.PREFERRED_SIZE,
                        javax.swing.GroupLayout.DEFAULT_SIZE,
                        javax.swing.GroupLayout.PREFERRED_SIZE))
                  .addGap(
                    37,
                    37,
                    37)
                  .addGroup(
                    layout
                      .createParallelGroup(
                        javax.swing.GroupLayout.Alignment.BASELINE)
                      .addComponent(
                        jLabelMinute)
                      .addComponent(
                        jTextFieldMinute,
                        javax.swing.GroupLayout.PREFERRED_SIZE,
                        javax.swing.GroupLayout.DEFAULT_SIZE,
                        javax.swing.GroupLayout.PREFERRED_SIZE)
                      .addComponent(
                        jLabelMonth2)
                      .addComponent(
                        jTextFieldHour,
                        javax.swing.GroupLayout.PREFERRED_SIZE,
                        javax.swing.GroupLayout.DEFAULT_SIZE,
                        javax.swing.GroupLayout.PREFERRED_SIZE)))
              .addGroup(
                layout
                  .createSequentialGroup()
                  .addGroup(
                    layout
                      .createParallelGroup(
                        javax.swing.GroupLayout.Alignment.BASELINE)
                      .addComponent(
                        jLabelDay)
                      .addComponent(
                        jTextFieldDay,
                        javax.swing.GroupLayout.PREFERRED_SIZE,
                        javax.swing.GroupLayout.DEFAULT_SIZE,
                        javax.swing.GroupLayout.PREFERRED_SIZE))
                  .addGap(
                    37,
                    37,
                    37)
                  .addGroup(
                    layout
                      .createParallelGroup(
                        javax.swing.GroupLayout.Alignment.BASELINE)
                      .addComponent(
                        jLabelSecond)
                      .addComponent(
                        jTextFieldSecond,
                        javax.swing.GroupLayout.PREFERRED_SIZE,
                        javax.swing.GroupLayout.DEFAULT_SIZE,
                        javax.swing.GroupLayout.PREFERRED_SIZE))))
          .addGap(37, 37, 37)
          .addGroup(
            layout
              .createParallelGroup(
                javax.swing.GroupLayout.Alignment.BASELINE)
              .addComponent(
                jButtonSet)
              .addComponent(
                jLabelSetDateTime))
          .addContainerGap(227, Short.MAX_VALUE)));

  if (SystemTray.isSupported()) {

   systemTray = SystemTray.getSystemTray();

  }

  
  
  icon = new ImageIcon(this.getClass().getResource("trayIcon.png"));

  if (systemTray != null) {

   initSystemTray();

  }

  this.setLocation(100, 200);

  pack();
 }// </editor-fold>

 // GEN-END:initComponents

 /**
  * 初始化系统托盘的方法
  */
 private void initSystemTray() {

  TrayIcon trayIcon = new TrayIcon(icon.getImage());
  trayIcon.setImageAutoSize(true);

  PopupMenu popupMenu = new PopupMenu("托盘菜单");

  // 创建显示主窗体菜单项
  MenuItem showMenuItem = new MenuItem("显示主窗体");
  showMenuItem.addActionListener(new ActionListener() {
   @Override
   public void actionPerformed(ActionEvent e) {
    MainFrame.this.setExtendedState(JFrame.NORMAL);
    MainFrame.this.setVisible(true);
   }
  });

  // 创建退出菜单项
  MenuItem exitMenuItem = new MenuItem("退出");
  exitMenuItem.addActionListener(new ActionListener() {
   @Override
   public void actionPerformed(ActionEvent e) {
    System.exit(0);
   }
  });

  popupMenu.add(showMenuItem);
  popupMenu.addSeparator();
  popupMenu.add(exitMenuItem);

  trayIcon.setPopupMenu(popupMenu);
  try {
   systemTray.add(trayIcon);
  } catch (AWTException e) {
   e.printStackTrace();
  }
 }

 private void jButtonSetActionPerformed(java.awt.event.ActionEvent evt) {

  dateTime = "";

  dateTime += this.jTextFieldYear.getText().trim();

  dateTime += "-";

  dateTime += this.jTextFieldMonth.getText().trim();

  dateTime += "-";

  dateTime += this.jTextFieldDay.getText().trim();

  dateTime += " ";

  dateTime += this.jTextFieldHour.getText().trim();

  dateTime += ":";

  dateTime += this.jTextFieldMinute.getText().trim();

  dateTime += ":";

  dateTime += this.jTextFieldSecond.getText().trim();

  this.jLabelSetDateTime.setText(dateTime);

  new Thread(this).start();

 }

 /**
  * @param args
  *            the command line arguments
  */
 public static void main(String args[]) {
  java.awt.EventQueue.invokeLater(new Runnable() {
   public void run() {
    new MainFrame();
   }
  });
 }

 // GEN-BEGIN:variables
 // Variables declaration - do not modify
 private javax.swing.JButton jButtonSet;
 private javax.swing.JLabel jLabelDay;
 private javax.swing.JLabel jLabelMinute;
 private javax.swing.JLabel jLabelMonth;
 private javax.swing.JLabel jLabelMonth2;
 private javax.swing.JLabel jLabelSecond;
 private javax.swing.JLabel jLabelSetDateTime;
 private javax.swing.JLabel jLabelYear;
 private javax.swing.JTextField jTextFieldDay;
 private javax.swing.JTextField jTextFieldHour;
 private javax.swing.JTextField jTextFieldMinute;
 private javax.swing.JTextField jTextFieldMonth;
 private javax.swing.JTextField jTextFieldSecond;
 private javax.swing.JTextField jTextFieldYear;

 // End of variables declaration//GEN-END:variables

 private void shutdown() throws IOException {

  Runtime.getRuntime().exec("shutdown -s -t 00");
 }

 private void Restart() throws IOException {

  Runtime.getRuntime().exec("shutdown -r -t 00");
 }

 private void logoff() throws IOException {

  Runtime.getRuntime().exec("shutdown -l");
 }

 @Override
 public void run() {

  Logger.getLogger("com.main").info("Thread begin...");

  try {
   SimpleDateFormat dateFormat = new SimpleDateFormat(
     "yyyy-MM-dd HH:mm:ss");

   Date now = Calendar.getInstance().getTime();

   Date set = dateFormat.parse(dateTime);

   long nowTime = now.getTime();

   long setTime = set.getTime();

   while (nowTime < setTime) {

    nowTime = Calendar.getInstance().getTime().getTime();
    
    Thread.sleep(5000);

    Logger.getLogger("com.main").info("nowTime < setTime");

   }

   shutdown();

  } catch (Exception e) {
   // TODO: handle exception
  }
 }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值