采用Java Swing開發的一個定時關機的小工具源碼

jopen 12年前發布 | 35K 次閱讀 Swing Java開發 Java Swing

import java.awt.;
import java.awt.event.;
import java.io.IOException;
import java.util.Calendar;
import javax.swing.*;

public class CloseComputer extends JFrame implements ActionListener{ //創建成員變量 //創建實現BorderLayout布局的面板對象panelmain,用于放panelSubnorth面板和panelSubcenter面板 private JPanel panelMain; //創建實現FlowLayout布局的面板對象panelSubnorth,用于放tag提示標簽 private JPanel panelSubnorth; //創建實現FlowLayout布局的面板對象panelSubcenter,用于放3個按鈕 private JPanel panelSubcenter;

//創建三個按鈕
private Button countdown;
private Button time;
private Button cancel;
private String key;
//創建一個提示標簽
private JLabel tag;


public CloseComputer(){
    initComponents();
}

/**
 * 初始化組件信息
 */
public void initComponents() {

    panelMain=new JPanel(new BorderLayout(5, 10));

    panelSubnorth=new JPanel(new FlowLayout(3));
    panelSubcenter=new JPanel(new FlowLayout(1,5,5));

    countdown=new Button("倒計時關機");
    time=new Button("定時關機");
    cancel=new Button("取消關機");

    tag=new JLabel("請選擇關機方式");

    //將panelMain添加到窗體中
    this.getContentPane().add(panelMain);
    //添加對象panelSubnorth到對象panelMain窗口里
    panelMain.add(panelSubnorth, BorderLayout.NORTH);
    //添加對象panelSubcenter到對象panelMain窗口里
    panelMain.add(panelSubcenter, BorderLayout.CENTER);

    //添加標簽對象tag到對象panelSubnorth窗口里
    panelSubnorth.add(tag);

    //添加3個按鈕到對象panelSubcenter里
    panelSubcenter.add(countdown);
    panelSubcenter.add(time);
    panelSubcenter.add(cancel);

    //為3個按鈕注冊事件監聽器
    countdown.addActionListener(this);
    time.addActionListener(this);
    cancel.addActionListener(this);

    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.setResizable(false);
    this.setLocation(350, 350);
    this.setSize(400, 400);
    this.setTitle("關機工具");

    this.pack();
}

/**
 * 倒計時關機
 */
public void countdown() {
    //創建字符串面板對象
    key=JOptionPane.showInputDialog(this,"請輸入倒計時關機剩余的時間(秒)","輸入框",1);
    try {
        Runtime.getRuntime().exec("shutdown -s -t "+key);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
/**
 * 定時關機
 */
public void time(){
    //獲取當前系統的時間
    Calendar calendar=Calendar.getInstance();
    int h=calendar.get(Calendar.HOUR);      //獲取小時
    System.out.println(h);
    int m=calendar.get(Calendar.MINUTE);    //獲取分鐘
    int s=calendar.get(Calendar.SECOND);    //獲取秒
    String hourtmp,minutetmp,secordtmp;     //保存輸入的時間
    int hour,minute,secord;                 //保存轉換后的時間
    //輸入時間
    hourtmp=JOptionPane.showInputDialog(this,"請輸入關機的小時(12小時制)","輸入",1);
    minutetmp=JOptionPane.showInputDialog(this,"請輸入關機的分鐘","輸入",1);
    secordtmp=JOptionPane.showInputDialog(this,"請輸入關機的秒鐘","輸入",1);
    //轉換時間
    hour=Integer.parseInt(hourtmp);
    minute=Integer.parseInt(minutetmp);
    secord=Integer.parseInt(secordtmp);

    long setTime=timeSum(hour, minute, secord); //計算輸入時間的總和
    long currentlyTime=timeSum(h, m, s);        //計算當前系統時間的總和
    long discrepancyTime=setTime-currentlyTime; //獲取時間差
    if (discrepancyTime<0) {
        try {
            Runtime.getRuntime().exec("shutdown -s");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }else {
        try {
            Runtime.getRuntime().exec("shutdown -s  -t "+discrepancyTime);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        JOptionPane.showMessageDialog(this, "恭喜你,設置成功!", "確認", 2);
    }
}
/**
 * 計算出時間總和,并返回
 * @param h
 * @param m
 * @param s
 * @return
 */
public int timeSum(int h,int m,int s) {
    int sum=h*3600+m*60+s;
    return sum;
}

/**
 * 取消關機
 */
public void cancel() {
    JOptionPane.showMessageDialog(this, "你已經成功取消了關機操作!", "消息", 2);
    try {
        Runtime.getRuntime().exec("shutdown -a");
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

public static void main(String[] args) {
    java.awt.EventQueue.invokeLater(new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            new CloseComputer().setVisible(true);
        }
    });


}

@Override
public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
    if (e.getSource() == countdown) {
        countdown();
    }
    if (e.getSource() == time) {
        time();
    }
    if (e.getSource() == cancel) {
        cancel();
    }
}

}</pre>
來自:http://blog.csdn.net/hidelin/article/details/8201267

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!