62,620
社区成员
发帖
与我相关
我的任务
分享




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