1. 程式人生 > >java swing做的定時關機精靈

java swing做的定時關機精靈

連結:定時關機精靈下載

上班閒暇時做的swing小程式,僅供java初學者和愛好者參考學習! 原始碼:

package shutdown;

import java.awt.Color;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class ShutDownWindows extends JFrame implements ActionListener{

	
	private static final long serialVersionUID = 1L;
	private JPanel jp;
	private JComboBox<Integer> comboBoxHour;
	private JComboBox<Integer> comboBoxMinute;
	private JComboBox<Integer> comboBoxSecond;
	private JLabel jlMinute;
	private JLabel jlHour;
	private JLabel jlSecond;
	private static JButton jRestTime;
	private JButton j1;
	private JButton j2;
	public static boolean stop = false;
	
	static int hourTime;
	static int minuteTime;
	static int secondTime;
	
	public ShutDownWindows(){
		setSize(300,210);
		setTitle("定時關機精靈");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		int screenX = Toolkit.getDefaultToolkit().getScreenSize().width;
		int screenY = Toolkit.getDefaultToolkit().getScreenSize().height;
		int frameX = this.getSize().width;
		int frameY = this.getSize().height;
		setLocation(screenX-frameX, screenY-frameY-40);
		setAlwaysOnTop(true);
		//setLocationRelativeTo(null);
		setResizable(false);
		jp = new JPanel();
		jp.setBackground(new Color(180,245,212));
		init();
		add(jp);
		setVisible(true);
	}
	/*
	 * 初始化函式
	 */
	public void init(){
		comboBoxHour = new JComboBox<Integer>();
		for(int i=0; i<=23; i++){
			comboBoxHour.addItem(i);
		}
		jlHour = new JLabel("小時");
		comboBoxMinute = new JComboBox<Integer>();
		for(int i=0; i<=59; i++){
			comboBoxMinute.addItem(i);
		}
		jlMinute = new JLabel("分");
		comboBoxSecond = new JComboBox<Integer>();
		for(int i=1; i<=59; i++){
			comboBoxSecond.addItem(i);
		}
		jlSecond = new JLabel("秒");
		
		j1 = new JButton("定時關機");
		j2 = new JButton("取消關機");
		j1.addActionListener(this);
		j2.addActionListener(this);
		j1.setEnabled(true);
		j2.setEnabled(false);
		
		jRestTime = new JButton("     00:00:00     ");
		jRestTime.setBorder(null);
		jRestTime.setFont(new Font("宋體",Font.BOLD,60));
		jRestTime.setBackground(new Color(97,199,147));
		
		JLabel jl = new JLabel("距離關機還有:       ");
		jl.setFont(new Font("宋體",Font.BOLD,26));
		
		JLabel jSetTime = new JLabel("設定時間:");
		jp.add(jSetTime);
		jp.add(comboBoxHour);
		jp.add(jlHour);
		jp.add(comboBoxMinute);
		jp.add(jlMinute);
		jp.add(comboBoxSecond);
		jp.add(jlSecond);
		jp.add(jl);
		jp.add(jRestTime);
		jp.add(j1);
		jp.add(j2);
		
	}
	
	public void ChooseTime(){
		hourTime = (int)comboBoxHour.getSelectedItem()*3600;
		minuteTime = (int)comboBoxMinute.getSelectedItem()*60;
		secondTime = (int)comboBoxSecond.getSelectedItem();
		int totalTime = hourTime+minuteTime+secondTime;
		
		Runtime runtime = Runtime.getRuntime();
		try {
			runtime.exec("cmd /c start shutdown.exe -s -t "+totalTime+"");
			ShutDownWindows.CountDown();
			stop = false;
			/*// 設定視窗狀態(最小化至托盤)  
            setExtendedState(JFrame.ICONIFIED);*/
		} catch (Exception e) {
			System.out.println("Error!");
		}
	}
	public void CannelPlan(){
		Runtime runtime = Runtime.getRuntime();
		try {
			runtime.exec("cmd /c start shutdown -a");
			stop = true;
		} catch (Exception e) {
			System.out.println("Error!");
		}
	}
	public static void CountDown(){
		new Thread(){
			public void run(){
				int hour = hourTime/3600;
				int minute = minuteTime/60;
				while(secondTime != 0){
					try {
						Thread.sleep(1000);
						secondTime--;
						if(stop){
							Thread.sleep(999999999);
						}
						if(secondTime == 0 && minute != 0){
							secondTime = 59;
							minute -= 1;
						}else if(secondTime == 0 && minute == 0 && hour != 0){
							secondTime = 59;
							minute = 59;
							hour -= 1;
						}else if(secondTime == 0 && minute == 0 &&  hour == 0){
							jRestTime.setText("     "+hour+":"+minute+":"+secondTime+"     ");
							break;
						}
						
						jRestTime.setText("     "+hour+":"+minute+":"+secondTime+"     ");
						
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					
				}
			}
		}.start();
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==j1){
			ChooseTime();
			j1.setEnabled(false);
			j2.setEnabled(true);
		}
		if(e.getSource()==j2){
			CannelPlan();
			j1.setEnabled(true);
			j2.setEnabled(false);
		}
		
	}
	public static void main(String[] args) {
		new ShutDownWindows();
	}
	
}


連結:定時關機精靈下載