⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 shutdownwindowsmain.java

📁 设置关机和重启时间,程序自动操作来完成关机和重启你的电脑。
💻 JAVA
字号:
package ShutOrReset;

/**
 * 
 * @author Maple
 * @reference Internet
 * @date 6,7th March 2009
 * 
 */
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class ShutDownWindowsMain {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		ShutDownWindows window=new ShutDownWindows();
		//System.out.println(60002/60000);
	}	
}
class ShutDownWindows
{

	Frame frame=new Frame("自动关机——Maple Studio");
	Label Lwelcom=new Label("欢迎使用自动关机程序");
	//**********************************
	Label LnowTime=new Label("现在时间为:");  
	Label LshuTime=new Label("预关机时间:");
	//**********************************
	Label Lnull1=new Label(" ");
	Label Lnull2=new Label("    ");
	Label Lnull3=new Label("       ");
	//**********************************

	CheckboxGroup group = new CheckboxGroup();
	Checkbox guanJi = new Checkbox("关机", group, true);
	Checkbox chongQi=new Checkbox("重启", group, false);
	
	//**********************************
	Button btnyes=new Button("确定");
	Button btncancel=new Button("取消");
	//**********************************
	TextField Tnow=new TextField(18);
	TextField Ttime=new TextField(18);
//	**********************************
	Label Ldistance=new Label("离关机还有多少时间:");
	TextField Tds=new TextField(10);

	public ShutDownWindows()
	{
		//********************
		Panel panel=new Panel();
		panel.setLayout(new BorderLayout());
		panel.add(Lwelcom,BorderLayout.CENTER);
		Lwelcom.setFont(new Font("宋体",Font.BOLD,20));
		Lwelcom.setForeground(new Color(173,255,47));
		//********************
		Panel panel1=new Panel();                      //now time
		Tnow.setEnabled(false);
		
		panel1.setLayout(new BorderLayout());
		panel1.add(LnowTime,BorderLayout.WEST);
		panel1.add(Tnow,BorderLayout.EAST);
		//********************
		Panel panel2=new Panel();                      //shut time
		Ttime.setText(new SimpleDateFormat("yyyy年MM月dd日-HH:mm")
		.format(new Date()));
		//String stringTime=Ttime.getText();
		panel2.setLayout(new BorderLayout());
		panel2.add(LshuTime,BorderLayout.WEST);
		panel2.add(Ttime,BorderLayout.EAST);
		//********************
		Panel panel3=new Panel();                  //checkbox
		panel3.setLayout(new BorderLayout());
		panel3.add(guanJi,BorderLayout.WEST);
		panel3.add(Lnull2);
		panel3.add(chongQi,BorderLayout.EAST);
		//********************
		Panel panel4 =new Panel();                  //shut time distance
		panel4.setLayout(new BorderLayout());
		panel4.add(Ldistance,BorderLayout.WEST);
		panel4.add(Lnull1);
		Tds.setEditable(false);
		panel4.add(Tds,BorderLayout.EAST);
		//********************
		Panel panel5=new Panel();                 //Button
		panel5.setLayout(new BorderLayout());
		panel5.add(btnyes,BorderLayout.WEST);
		panel5.add(Lnull3);
		panel5.add(btncancel,BorderLayout.EAST);
		//*******************
		btncancel.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				System.exit(1);
			}
		}
		);
		//********************
		FlowLayout flow=new FlowLayout();
		frame.setLayout(flow);
		frame.add(panel);
		frame.add(panel1);	
		frame.add(panel2);
		frame.add(panel3);
		frame.add(panel4);
		frame.add(panel5);
		 frame.setBounds(350, 200, 300, 220);
		//********************
		//********************
		//frame.setSize(320, 250);
		frame.setBackground(new Color(72,188,255));
		//frame.setLocation(350,200);
		frame.setVisible(true);
        //********************************************
		frame.addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent e)
			{
				System.exit(1);
			}
		});
		//*********************Thread Control (1)**********************
		class ClockThread extends Thread
		{
			String nowtime;
			public void ClockThread(){}
			public void run()
			{
				while (true)
				{
					nowtime=new SimpleDateFormat("yyyy年MM月dd日-HH:mm:ss")
					.format(new Date());
					Tnow.setText(nowtime);
					try{
						Thread.sleep(1000);
					}catch(InterruptedException e){
						e.printStackTrace();
					}
				}
			}
		}
		new ClockThread().start();
	
		//*******************Control (1) End************************
		final boolean b=true;
		//*******************Confirm Button Event****************
		btnyes.addActionListener(new ActionListener()
		{
			
			public void actionPerformed(ActionEvent e)
			{
				//ShutDownWindows win1=new ShutDownWindows();
				String strtime=Ttime.getText();
				SimpleDateFormat df = new SimpleDateFormat(
				"yyyy年MM月dd日-HH:mm");
		Date date = null;
		final long lafter;
		try {
			date = df.parse(strtime);
		} catch (ParseException e1) {
			//Tds.setText("时间格式错误");
			JOptionPane.showMessageDialog(null, "时间格式错误,请重新设置(yyyy年MM月dd日-HH:mm)","error",JOptionPane.ERROR_MESSAGE);
			
			e1.printStackTrace();
			//System.out.println(win1.Ttime.getText());
		}
		Calendar cal=Calendar.getInstance();
		cal.setTime(date);
		lafter = cal.getTimeInMillis();
		//*******************Thread Control(2)************************
		class ClockThread extends Thread
		{
			String nowtime;
			public void ClockThread(){}
			public void run()
			{
				while(b)
				{
					Calendar nowget=Calendar.getInstance();
					nowget.setTime(new Date());
					long lafter2=nowget.getTimeInMillis();
					long distance=(lafter-lafter2)/60000;
					
					long distance1=((lafter-lafter2)/1000)-(distance*60);
					if(lafter<lafter2)
					{
						JOptionPane.showMessageDialog(null, "预设时间应大于当前时间!", "Waring", JOptionPane.WARNING_MESSAGE);
						break;
					}
					try{
						Thread.sleep(1000);
					}catch(InterruptedException e){
						e.printStackTrace();
					}
					//System.out.println(distance);
					
					Tds.setText(String.valueOf(distance)+"分"+String.valueOf(distance1)+"秒");
					//Tds.setEnabled(false);
					Tds.setEditable(false);
					//Tds.setBackground(Color.red);
					Tds.setForeground(Color.red);
					
					if(distance==0 && distance1==0){	//discover Checkbox
						if(guanJi.getState()==true)     //关机
						{
							System.out.println("laile");
							//break;
							  try{
								  Tds.setText("准备关机....");
								  Runtime.getRuntime().exec("shutdown -s ");
								  break;
							  } catch(IOException e1)
							  {
								  e1.printStackTrace();
							  }
							  
						}
						else if(chongQi.getState()==true){//重启
							System.out.println("lailedddd");
							try{
								  Tds.setText("准备关机,之后重启....");
								  Runtime.getRuntime().exec("shutdown -r ");
								  break;
							  } catch(IOException e1)
							  {
								  e1.printStackTrace();
							  }
						}												  								  							
					}
					
				}
			
			}
		}
		new ClockThread().start();
		//*******************Control (2) End**************************
			}
		});
		//********************Confirm Event End***********************
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -