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();
	}
	
}


链接:定时关机精灵下载

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值