Java 定时关机程序

 

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class ColockFrame extends JFrame implements Runnable, ActionListener {

 /**
  * main frame
  */
 private static final long serialVersionUID = 1L;
 public static Image[] numImage = new Image[10];
 public static Image back = null;

 private static JLabel one;
 private static JLabel two;
 private static JLabel three;
 private static JLabel four;
 private static JLabel five;
 private static JLabel six;
 private static JButton ok;
 private static JButton cancel;
 private static JButton exit;
 private static JTextField tField;
 private static Thread thread;
 private static boolean isAlive;
 public static JComboBox jcb;
 private static int bases;

 public ColockFrame() {

  super("Close Computer");
  setBases(1);
  setAlive(true);
  try {
   back = ImageIO.read(new File("image/back.jpg"));
   numImage[0] = ImageIO.read(new File("image/d0.gif"));
   numImage[1] = ImageIO.read(new File("image/d1.gif"));
   numImage[2] = ImageIO.read(new File("image/d2.gif"));
   numImage[3] = ImageIO.read(new File("image/d3.gif"));
   numImage[4] = ImageIO.read(new File("image/d4.gif"));
   numImage[5] = ImageIO.read(new File("image/d5.gif"));
   numImage[6] = ImageIO.read(new File("image/d6.gif"));
   numImage[7] = ImageIO.read(new File("image/d7.gif"));
   numImage[8] = ImageIO.read(new File("image/d8.gif"));
   numImage[9] = ImageIO.read(new File("image/d9.gif"));
  } catch (Exception e) {
   e.printStackTrace();
  }

  // time panel

  JPanel timePanel = new JPanel();
  one = new JLabel(new ImageIcon(numImage[0]));
  two = new JLabel(new ImageIcon(numImage[0]));
  three = new JLabel(new ImageIcon(numImage[0]));
  four = new JLabel(new ImageIcon(numImage[0]));
  five = new JLabel(new ImageIcon(numImage[0]));
  six = new JLabel(new ImageIcon(numImage[0]));
  JLabel unionJLabel = new JLabel(" SECOND(S)");
  JLabel remains = new JLabel("Now  Past ");
  timePanel.add(remains);
  timePanel.add(one);
  timePanel.add(two);
  timePanel.add(three);
  timePanel.add(four);
  timePanel.add(five);
  timePanel.add(six);
  timePanel.add(unionJLabel);
  timePanel.setBackground(Color.BLACK);
  timePanel.setSize(Size.WIDTH, 100);

  // time panel;

  // operation panel
  ok = new JButton("OK");
  ok.setBackground(Color.MAGENTA);
  ok.addActionListener(this);
  cancel = new JButton("CANCEL");
  cancel.setBackground(Color.PINK);
  cancel.addActionListener(this);
  exit = new JButton("EXIT");
  exit.setBackground(Color.PINK);
  exit.addActionListener(this);
  JLabel tip = new JLabel("请输入时间:");
  tField = new JTextField("1000");
  jcb = new JComboBox();
  jcb.setBackground(Color.RED);
  jcb.addItem("Second(s)");
  jcb.addItem("Min(s)");
  jcb.addItem("Hour(s)");
  jcb.addActionListener(new ActionListener() {

   @Override
   public void actionPerformed(ActionEvent e) {
    String tempString = (String) jcb.getSelectedItem();
    if (tempString.equals("Min(s)")) {
     setBases(60);
    } else if (tempString.equals("Hour(s)")) {
     setBases(3600);
    }

   }
  });
  JPanel operatioJPanel = new JPanel();
  operatioJPanel.setLayout(new GridLayout(2, 3));
  operatioJPanel.add(tip);
  operatioJPanel.add(tField);
  operatioJPanel.add(jcb);
  operatioJPanel.add(exit);
  operatioJPanel.add(ok);
  operatioJPanel.add(cancel);
  operatioJPanel.setBackground(Color.RED);
  operatioJPanel.setSize(Size.WIDTH, 200);

  // operation panel;

  // background

  JPanel backPanel = new JPanel();
  JLabel bJLabel = new JLabel(new ImageIcon(back));
  backPanel.add(bJLabel, BorderLayout.CENTER);

  this.setSize(Size.WIDTH, Size.HEGIHT);
  this.add(operatioJPanel, BorderLayout.NORTH);
  this.add(backPanel, BorderLayout.CENTER);
  this.add(timePanel, BorderLayout.SOUTH);

  this.setVisible(true);
  this.setResizable(false);
  this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

 }

 @Override
 public void run() {
  int op = 0;

  int a[] = new int[] { 0, 0, 0, 0, 0, 0, 0 };
  while (isAlive()) {
   String ss = Integer.toString(op);
   int index = 5;
   for (int i = ss.length() - 1; i >= 0; i--) {
    a[index--] = (int) (ss.charAt(i) - '0');
   }

   six.setIcon(new ImageIcon(numImage[a[5] % 10]));
   five.setIcon(new ImageIcon(numImage[a[4] % 10]));
   four.setIcon(new ImageIcon(numImage[a[3] % 10]));
   three.setIcon(new ImageIcon(numImage[a[2] % 10]));
   two.setIcon(new ImageIcon(numImage[a[1] % 10]));
   one.setIcon(new ImageIcon(numImage[a[0] % 10]));

   try {

    int op2 = Integer.parseInt(tField.getText()) * getBases();

    if (op2 == op) {
     System.out.println("equals");
     //Runtime runtime = Runtime.getRuntime();

     Runtime.getRuntime().exec(
       "shutdown.exe -s");
     setAlive(false);
    }
    op++;
    Thread.sleep(1000);

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

 }

 @Override
 public void actionPerformed(ActionEvent e) {

  if (e.getActionCommand() == "OK") {
   setAlive(true);
   thread = new Thread(this);
   thread.start();

  } else if (e.getActionCommand() == "CANCEL") {
   try {
    Runtime.getRuntime().exec(
    "shutdown.exe -a");
   } catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
   }
   setAlive(false);
  } else if (e.getActionCommand() == "EXIT") {
   System.exit(0);

  }

 }

 public static void setAlive(boolean isAlive) {
  ColockFrame.isAlive = isAlive;
 }

 public static boolean isAlive() {
  return isAlive;
 }

 public static void setBases(int bases) {
  ColockFrame.bases = bases;
 }

 public static int getBases() {
  return bases;
 }
}

 

  

public class Size {
 public static final int WIDTH=300;
 public static final int HEGIHT=450;

 

public class Main {

 
 public static void main(String[] args) {
     new ColockFrame();
            
 }

}


 

 资源下载地址:http://download.csdn.net/detail/rufuns/4312906

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值