分享一个自己做得关机软件,希望大家给点建议和意见

Sauron1 2014-04-16 06:18:02
内容分为1.效果图。2.出现问题及我的解决办法(希望大神们给点其他思路,意见和建议)3.源代码。

一.效果图:


主界面:

设置成功1:

设置成功2:

设置错误:

不设置了:
...全文
211 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
92浩 2014-04-16
  • 打赏
  • 举报
回复
试了,不行啊
92浩 2014-04-16
  • 打赏
  • 举报
回复
楼主贴完了吗?我去试试
whc748227431 2014-04-16
  • 打赏
  • 举报
回复
三.源代码 5.

import java.awt.Label;
import java.io.File;
import java.io.IOException;

public class RunExeThread extends Thread{
	
	private int set_hour;
	private int set_minute;
	private int system_hour;
	private int system_minute;
	
	private Label yoursettime;
	private Label remaintime;
	
	private boolean stop = false;
	
	public void run()
	{
		yoursettime.setText("你设置的关机时间为:" + set_hour + ":" + set_minute);
		
		long total_left_second = ( (set_hour - system_hour) * 60 + 
				set_minute - system_minute ) * 60; 
		
		String text = new String();
		long cycle = total_left_second;
		
		//输出倒计时
		for(long i = 0; i < cycle; ++i)
		{
			if(stop) break;
			
			text = getShowRemainTimeText(total_left_second);
			remaintime.setText(text);
			
			total_left_second--;
			
			try 
			{
				Thread.sleep(1000);
			} 
			catch (InterruptedException e) 
			{
				e.printStackTrace();
				stop = true;
			}
		}
		if(!stop)
			RunExe();
		else ;
	}
	
	//得到倒计时的输出文本
	public String getShowRemainTimeText(long total_left_second)
	{
		int left_hour;
		int left_minute;
		int left_second;
		
		if(total_left_second < 3600) left_hour = 0;
		else left_hour = (int) (total_left_second / 3600);
		
		if(total_left_second < 60) left_minute = 0;
		else left_minute = (int) ((total_left_second - left_hour * 3600) / 60);
		
		left_second = (int) (total_left_second - left_hour * 3600 - left_minute * 60);
		
		return ("倒计时:" + left_hour + ":" + left_minute + ":" + left_second);
		
	}
	
	public void RunExe()
	{
		Runtime rt;
		rt = Runtime.getRuntime();
		
		File exeFile = new File("C:\\Windows\\System32\\","shutdown.exe");
		
		try 
		{
			rt.exec(exeFile.getAbsolutePath());
		}
		catch (IOException e) 
		{
			e.printStackTrace();
		}
	}
	
	public void setWaittime(int set_hour,int set_minute,
			int system_hour,int system_minute)
	{
		this.set_hour = set_hour;
		this.set_minute = set_minute;
		this.system_hour = system_hour;
		this.system_minute = system_minute;
	}
	
	public void setYoursettime(Label yoursettime)
	{
		this.yoursettime = yoursettime;
	}
	public void setRemaintime(Label remaintime)
	{
		this.remaintime = remaintime;
	}
}
25K纯帅 2014-04-16
  • 打赏
  • 举报
回复
三.源代码 3.ItemListener监视器

import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JComboBox;

class MyItemListener implements ItemListener{
	
	private JComboBox<String> hour;
	private JComboBox<String> minute;
	private MyActionListener myactionlistener;
	
	@Override
	public void itemStateChanged(ItemEvent arg0) {
		myactionlistener.setHour(hour.getSelectedItem().toString());
		myactionlistener.setMinute(minute.getSelectedItem().toString());
	}
	
	
	public void setActionlistener(MyActionListener myactionlistener)
	{
		this.myactionlistener = myactionlistener;
	}
	
	public void setHour(JComboBox<String> hour)
	{
		this.hour = hour;
	}
	
	public void setMinute(JComboBox<String> minute)
	{
		this.minute = minute;
	}
}

25K纯帅 2014-04-16
  • 打赏
  • 举报
回复
三.源代码 3.ActionListener监视器

import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import javax.swing.JOptionPane;

public class MyActionListener implements ActionListener{
	
	private String hour;
	private String minute;
	
	private Label yoursettime;
	private Label remaintime;
	
	private int i = 0; 
	private RunExeThread thread_first = new RunExeThread();
	private RunExeThread thread_second = new RunExeThread();
	
	@Override
	public void actionPerformed(ActionEvent arg0) 
	{
		if(arg0.getActionCommand() == "不设置了")
		{
			deleteTheOtherThread(thread_first);
			deleteTheOtherThread(thread_second);
			
			this.remaintime.setText("倒计时:0");
			this.yoursettime.setText("你还没设置关机时间!");
			
			createExitDialog();
		}
		else
		{
			Date date = new Date();
			int system_hour = date.getHours();
			int system_minute = date.getMinutes();
			int set_hour = Integer.parseInt(hour);
			int set_minute = Integer.parseInt(minute);
		
			if(HaveSetCorrectTime(set_hour,set_minute,system_hour,system_minute))
				launchShutdownExeNormally(set_hour,set_minute,system_hour,system_minute);
			else
				createErrorDialog();
		}	
	}
	
	/*
	 * 判断设置的时间是否正确合适
	 */
	public boolean HaveSetCorrectTime(int set_hour,int set_minute,
			int system_hour,int system_minute)
	{
		if(set_hour < system_hour)
			return false;
		
		if(set_hour == system_hour)
		{
			if(set_minute <= system_minute)
				return false;
			else
				return true;
		}
	
		if(set_hour > system_hour)
			return true;

		return false;
	}
	
	/*
	 * 正常设定定时关机
	 */
	public void launchShutdownExeNormally(int set_hour,int set_minute,
			int system_hour,int system_minute)
	{	
		JOptionPane.showMessageDialog(null, "设置成功,关机时间定为:" + set_hour + ":" +
				set_minute + "。可以去干别的事了你", "设置成功", JOptionPane.WARNING_MESSAGE);
		
		if(i % 2 == 0)
		{
			thread_first = new RunExeThread();
			thread_first.setWaittime(set_hour,set_minute,system_hour,system_minute);
			thread_first.setYoursettime(yoursettime);
			thread_first.setRemaintime(remaintime);
			thread_first.start();
			
			deleteTheOtherThread(thread_second);
		}
		if(i % 2 == 1)
		{
			thread_second = new RunExeThread();
			thread_second.setWaittime(set_hour,set_minute,system_hour,system_minute);
			thread_second.setYoursettime(yoursettime);
			thread_second.setRemaintime(remaintime);
			
			thread_second.start();
			
			deleteTheOtherThread(thread_first);
		}
		
		i++;
	}
	
	/*
	 * 重新设置时终止前一个进程
	 */
	public void deleteTheOtherThread(RunExeThread deletethread)
	{
		if(deletethread.isAlive())
			deletethread.interrupt();
	}
	
	/*
	 * 输出错误对话框
	 */
	public void createErrorDialog()
	{
		JOptionPane.showMessageDialog(null, "设置时间出错,设置的时间比当前时间还早,不可能!", 
				"设置时间有个错误", JOptionPane.WARNING_MESSAGE);
	}
	
	/*
	 * 点击不设置了出现的提示对话框
	 */
	public void createExitDialog()
	{
		JOptionPane.showMessageDialog(null, "好吧!之前的设置都取消了", 
				"放弃设置", JOptionPane.INFORMATION_MESSAGE);
	}
	
	public void setHour(String hour) {
		this.hour = hour;
	}

	public void setMinute(String minute) {
		this.minute = minute;
	}
	
	public void setYoursettime(Label yoursettime)
	{
		this.yoursettime = yoursettime;
	}
	public void setRemaintime(Label remaintime)
	{
		this.remaintime = remaintime;
	}
	
}
25K纯帅 2014-04-16
  • 打赏
  • 举报
回复
帮自己顶顶,一个用户最多回复3次,无语了!!
Sauron1 2014-04-16
  • 打赏
  • 举报
回复
三.源代码 2.建立窗口

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Label;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;

public class CreateWindow extends JFrame{
	
	private Box basebox;
	private Box box_setTime;
	private Box box_button;
	private Box box_yoursettime;
	private Box box_remaintime;
	
	private JComboBox<String> combobox_hour;
	private JComboBox<String> combobox_minute;
	
	private JButton Okbutton;
	private JButton Resetbutton;
	private JButton Deletebutton;
	
	private Label yoursettime;
	private Label remaintime;
	
	CreateWindow(){}
	CreateWindow(String title)
	{
		init(title);
		this.setVisible(true);
		this.setSize(400,200);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	}
	
	public void init(String title)
	{
		basebox = Box.createVerticalBox();
		box_setTime = Box.createHorizontalBox();
		box_button = Box.createHorizontalBox();
		box_yoursettime = Box.createHorizontalBox();
		box_remaintime = Box.createHorizontalBox();
		
		combobox_hour = new JComboBox<String>();
		combobox_hour.setPreferredSize(new Dimension(60,30));
		combobox_minute = new JComboBox<String>();
		combobox_minute.setPreferredSize(new Dimension(60,30));
		Okbutton = new JButton("确定时间");
		Resetbutton = new JButton("重新设置");
		Deletebutton = new JButton("不设置了");
		
		yoursettime = new Label("");
		remaintime = new Label("");
		
		setcombobox_hourAndminute(combobox_hour,combobox_minute);
		
		this.setTitle(title);
		this.setLayout(new FlowLayout());
		
		this.box_setTime.add(new Label("设置关机时间:"));
		this.box_setTime.add(combobox_hour);
		this.box_setTime.add(Box.createHorizontalStrut(8));
		this.box_setTime.add(new Label("点"));
		this.box_setTime.add(combobox_minute);
		this.box_setTime.add(Box.createHorizontalStrut(8));
		this.box_setTime.add(new Label("分"));
		
		this.box_button.add(Okbutton);
		this.box_button.add(Box.createHorizontalStrut(8));
		this.box_button.add(Resetbutton);
		this.box_button.add(Box.createHorizontalStrut(8));
		this.box_button.add(Deletebutton);
		
		this.box_yoursettime.add(Box.createHorizontalStrut(130));
		this.box_yoursettime.add(yoursettime);
		
		this.box_remaintime.add(Box.createHorizontalStrut(130));
		this.box_remaintime.add(remaintime);
		
		this.basebox.add(box_setTime);
		this.basebox.add(Box.createVerticalStrut(18));
		this.basebox.add(box_button);
		this.basebox.add(Box.createVerticalStrut(40));
		this.basebox.add(box_yoursettime);
		this.basebox.add(Box.createVerticalStrut(0));
		this.basebox.add(box_remaintime);
		this.add(basebox);
	}
	
	/*
	 * 为下拉框设置值:0-23小时,0-59分钟
	 */
	public void setcombobox_hourAndminute(JComboBox<String> hour,JComboBox<String> minute)
	{
		String flaghour = "";
		for(int i = 0; i < 24; ++i)
		{
			combobox_hour.addItem(flaghour + i);
		}
		
		String flagminute = "";
		for(int i = 0; i < 60; ++i)
		{
			combobox_minute.addItem(flagminute + i);
		}
	}
	
	/*
	 * 为组建添加监视器
	 */
	public void setListener(MyItemListener myitemlistener,MyActionListener myactionlistener)
	{
		myitemlistener.setHour(combobox_hour);
		myitemlistener.setMinute(combobox_minute);
		myitemlistener.setActionlistener(myactionlistener);
		
		myactionlistener.setYoursettime(yoursettime);
		myactionlistener.setRemaintime(remaintime);
		
		combobox_hour.addItemListener(myitemlistener);
		combobox_minute.addItemListener(myitemlistener);
		Okbutton.addActionListener( myactionlistener);
		Resetbutton.addActionListener(myactionlistener);
		Deletebutton.addActionListener(myactionlistener);
	}
}
Sauron1 2014-04-16
  • 打赏
  • 举报
回复
三.源代码: 1.主函数:

public class MainFunction {
	
	public static void main(String args[])
	{
		CreateWindow window = new CreateWindow("关机软件");	
		setListener(window);
	}
	
	
	public static void setListener(CreateWindow window)
	{
		MyItemListener myitemlistener = new MyItemListener();
		MyActionListener myactionlistener = new MyActionListener();
		
		window.setListener(myitemlistener,myactionlistener);
	}
}
Sauron1 2014-04-16
  • 打赏
  • 举报
回复
二.出现问题及我的解决办法(希望大神们给点其他思路) 问题1:当点击重新设置的时候,如何把之前的设置时间覆盖掉,(覆盖的时候注意如何输出倒计时) 问题2:如何更好的终止前一个线程。   我的解决办法:   对于问题1:当点设置关机的时候,开辟一个线程thread_first,当点击重新设置的时候,再开辟一个线程thread_second,同时把线程thread_first终止掉,如果又一次点击重新设置,就再开辟一个线程thread_first,同时把thread_second关闭掉。依次类推。   对于问题2:我首先.isAlive()) deletethread.interrupt();在此附上run()函数(里面需要不断sleep()来输出倒计时在RunExeThread线程类中设置private boolean stop = false;当需要终止前一个线程的时候,在监视器的actionPerformed方法中deleteTheOtherThread(thread_first); 该方法调用if(deletethread,当stop为true时,就不输出了,stop变量的更改在抛出InterruptedException时)

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧