日期:2014-05-20  浏览次数:20910 次

swing自动关机器一点小问题
昨晚想电脑处理东西想半夜5点关机 但是用XP的dos shutdown - s -t + 秒数还得自己计算太麻烦了,于是产生了下面这堆东西...学生写的比较垃圾..给有同样需求的人用用或看看吧..相信这样东西网上一搜索也是一大把.但是还是自己动手写的用起来比较有成就感..
Java code
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class ShutFrame extends JFrame implements ActionListener, KeyListener,
        WindowListener{
    final int limit = 2;
    String time = null;
    boolean flag = true;
    private JLabel label_1 = null;
    private JLabel label_2 = null;
    private JLabel overTime = null;
    private JTextField HH = null;
    private JTextField mm = null;
    private JTextField ss = null;
    private JButton exit = null;
    private JButton hide = null;
    private JButton start = null;

    public ShutFrame() {
        this.setTitle("关机小程序");
        label_1 = new JLabel("请输入时,分,秒!");
        label_2 = new JLabel("距离关机时间还有:");
        overTime = new JLabel("在这里显示时间");
        HH = new JTextField(3);
        mm = new JTextField(3);
        ss = new JTextField(3);
        
        exit = new JButton("退出");
        hide = new JButton("马上关机");
        start = new JButton("开始");
        JPanel jp1 = new JPanel();
        JPanel jp2 = new JPanel();
        JPanel jp3 = new JPanel();
        jp1.add(label_1);
        jp1.add(HH);
        jp1.add(mm);
        jp1.add(ss);
        jp2.add(label_2);
        jp2.add(overTime);
        jp3.add(exit);
        jp3.add(hide);
        jp3.add(start);
        this.setLayout(new GridLayout(3, 1));
        this.add(jp1);
        this.add(jp2);
        this.add(jp3);
        this.setBounds(200, 200, 250, 200);
        this.setVisible(true);
        hide.addActionListener(this);
        this.addWindowListener(this);
        exit.addActionListener(this);
        this.addKeyListener(this);
        start.addActionListener(this);
        HH.addKeyListener(this);
        mm.addKeyListener(this);
        ss.addKeyListener(this);
    }

    public void actionPerformed(ActionEvent e) {
        flag = true;
        if(e.getSource() == exit){
            System.exit(0);
        }
        // this.setVisible(false);
        if(e.getSource() == hide){
            Runtime run = Runtime.getRuntime();
            Process p = null;
                try {
                    p = run.exec("shutdown -s");
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
        }
        if (e.getSource() == start) {
            if (HH.getText().trim().equals("")
                    || mm.getText().trim().equals("")
                    || ss.getText().trim().equals("")) {
                System.out.println("请输入完整!");
            } else {
                String H = HH.getText().trim();
                String m = mm.getText().trim();
                String s = ss.getText().trim();
                
                    if(Integer.parseInt(H)>24 || Integer.parseInt(m)>60 || Integer.parseInt(s)>60){
                        overTime.setText("输入数字不符合实际!");
                        return;
                    }
                    System.out.println("++++++++++++");
                time = H + ":" + m + ":" + s;
                System.out.println("" + time);
                new Thread() {
                    public void run() {
                        while (flag){
                            try {
                                Date d = new Date();
                                SimpleDateFormat df = new SimpleDateFormat(
                                        "HH:mm:ss");
                                String now = df.format(d);
                                long CloseTime = df.parse(time).getTime();
                                long NowTime = df.parse(now).getTime();
                                long second = (CloseTime - NowTime) / 1000;
                                System.out.println("shut time is : "
                                        + CloseTime);
                                System.out.println("shut time is : " + NowTime);
                                System.out.println("away with shut time is : "
                                        + second);
                                overTime.setText("" + second);
                                overTime.setFont(new Font("Dialog",1,18));
                                overTime.setForeground(Color.BLUE);
                                if (second == 0) {
                                    System.out.println("computer is shut down!");
                                    Runtime run = Runtime.getRuntime();
                                    Process p = null;
                                        p = run.exec("shutdown -s");
                                    break;
                                }
                                Thread.sleep(1000);
                            }catch(Exception e){
                                flag = false;
                                //System.out.println("45456465");
                                overTime.setText("出错了!");
                                //e.printStackTrace();
                            }
                        }
                    }
                }.start();
            }
        }
        // System.out.println(HH.getText().trim().equals(""));
    }

    public void keyTyped(KeyEvent e) {
        if(e.getSource() == HH){
            if(HH.getText().length() >=limit){
                   getToolkit().beep();
                   e.consume();
            }
            if(HH.getText().length()==limit-1){
                mm.requestFocusInWindow();
            }
        }
        if(e.getSource() == mm){
                if(mm.getText().length() >=limit){
                    getToolkit().beep();
                    e.consume();
                }
                if(mm.getText().length()==limit-1){
                    ss.requestFocusInWindow();
                }
            }
        if(e.getSource() == ss){
            if(ss.getText().length() >=limit){
                getToolkit().beep();
                e.consume();
            }
        }
        
    }

    public void keyPressed(KeyEvent e) {
        //System.out.println(e.getKeyCode());
    }

    public void keyReleased(KeyEvent e) {
    }

    public static void main(String[] args) {
        ShutFrame s = new ShutFrame();
    }

    public void windowActivated(WindowEvent arg0) {
        // TODO Auto-generated method stub

    }

    public void windowClosed(WindowEvent arg0) {
        // TODO Auto-generated method stub

    }

    public void windowClosing(WindowEvent arg0) {
        System.exit(-1);

    }

    public void windowDeactivated(WindowEvent arg0) {
        // TODO Auto-generated method stub

    }

    public void windowDeiconified(WindowEvent arg0) {
        // TODO Auto-generated method stub

    }

    public void windowIconified(WindowEvent arg0) {
        // TODO Auto-generated method stub

    }

    public void windowOpened(WindowEvent arg0) {
        // TODO Auto-generated method stub

    }
}