package shutDown;import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.Calendar;
import java.util.GregorianCalendar;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;public class Window extends JFrame{
/**
*
*/
private static final long serialVersionUID = -1633125450689037337L;
String h,m,s;
JPanel p1=new JPanel();
JPanel p2=new JPanel();
JPanel p3=new JPanel();
JLabel l1=new JLabel("开发人KGD",0);
JLabel l2=new JLabel("当前时",0);
JLabel l3=new JLabel("当前分",0);
JLabel l4=new JLabel("当前秒",0);
JLabel l5=new JLabel("关机时",0);
JLabel l6=new JLabel("关机分",0);
JLabel l7=new JLabel("关机秒",0);
JLabel l8=new JLabel("");
JButton b1=new JButton("关机");
JButton b2=new JButton("重启");
JButton b3=new JButton("注销");
JButton b4=new JButton("清空");
JTextField tf1=new JTextField(5);
JTextField tf2=new JTextField(5);
JTextField tf3=new JTextField(5);
JTextField tf4=new JTextField(5);
JTextField tf5=new JTextField(5);
JTextField tf6=new JTextField(5); public Window(){
super("定时关机系统");
setLayout(new BorderLayout(5,5));
add(p1,"North");
add(p2,"Center");
p1.add(l1);
p2.setLayout(new GridLayout(3,6,5,5));
tf1.setHorizontalAlignment(JTextField.RIGHT );
tf2.setHorizontalAlignment(JTextField.RIGHT );
tf3.setHorizontalAlignment(JTextField.RIGHT );
tf4.setHorizontalAlignment(JTextField.RIGHT );
tf5.setHorizontalAlignment(JTextField.RIGHT );
tf6.setHorizontalAlignment(JTextField.RIGHT );
p2.add(l2);
p2.add(tf1);
p2.add(l3);
p2.add(tf2);
p2.add(l4);
p2.add(tf3);
p2.add(l5);
p2.add(tf4);
p2.add(l6);
p2.add(tf5);
p2.add(l7);
p2.add(tf6);
p2.add(l8);
p2.add(b1);
p2.add(b2);
p2.add(b3);
p2.add(b4);
b1.addActionListener(new action());
b2.addActionListener(new action());
b3.addActionListener(new action());
b4.addActionListener(new action());
this.setVisible(true);
this.pack();
}
public void time(){
GregorianCalendar gc=new GregorianCalendar();
int hour;
int hou=gc.get(Calendar.HOUR);
int minute=gc.get(Calendar.MINUTE);
int second=gc.get(Calendar.SECOND);
if(hou<24){
hour=hou;
}
else{
hour=hou-24;
}
h=String.valueOf(hour);
m=String.valueOf(minute);
s=String.valueOf(second);
}
public void show(){
tf1.setText(h);
tf2.setText(m);
tf3.setText(s);
}
public void close()//关机
{
try{Runtime.getRuntime().exec("shutdown.exe -s -t 10");}
catch(IOException e){
JOptionPane.showMessageDialog(this,"执行失败!");
}}
public void restart()//重启
{
try{Runtime.getRuntime().exec("shutdown.exe -r -t 10");}
catch(IOException e){
JOptionPane.showMessageDialog(this,"执行失败!");
}
}
public void logout()//注销
{
try{Runtime.getRuntime().exec("shutdown.exe -l");}
catch(IOException e){
JOptionPane.showMessageDialog(this,"执行失败!");
}
}
public void cancel()//取消
{ tf4.setText("");
tf5.setText("");
tf6.setText("");
}
public boolean equal(){
if(h.equals(tf4.getText())&&m.equals(tf5.getText())&&s.equals(tf6.getText())){
return true;
}
else
return false;
}
public class action implements ActionListener{
public void actionPerformed(ActionEvent e){
String command = e.getActionCommand();
Equals eq=new Equals();
if(command.equals("关机")){
eq.start();
eq.stop();
close();
}
else if(command.equals("重启")){
eq.start();
eq.stop();
restart();
}
else if(command.equals("注销")){
eq.start();
eq.stop();
logout();
}
else if(command.equals("清空")){
eq.start();
eq.stop();
cancel();
}
}
}
public static void main(String[] args){
new Window();
}
}package shutDown;public class Equals extends Thread{
public void run(){
Window win=new Window();
while(true){
try {
Thread.sleep(1000);
win.time();
win.show();
if(win.equal())
break;
else
continue;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.Calendar;
import java.util.GregorianCalendar;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Test extends JFrame implements ActionListener { String h, m, s;
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JPanel p3 = new JPanel();
JLabel l1 = new JLabel("开发人KGD", 0);
JLabel l2 = new JLabel("当前时", 0);
JLabel l3 = new JLabel("当前分", 0);
JLabel l4 = new JLabel("当前秒", 0);
JLabel l5 = new JLabel("关机时", 0);
JLabel l6 = new JLabel("关机分", 0);
JLabel l7 = new JLabel("关机秒", 0);
JLabel l8 = new JLabel("");
JButton b1 = new JButton("关机");
JButton b2 = new JButton("重启");
JButton b3 = new JButton("注销");
JButton b4 = new JButton("清空");
JTextField tf1 = new JTextField(5);
JTextField tf2 = new JTextField(5);
JTextField tf3 = new JTextField(5);
JTextField tf4 = new JTextField(5);
JTextField tf5 = new JTextField(5);
JTextField tf6 = new JTextField(5); public Test() {
setLayout(new BorderLayout(5, 5));
add(p1, "North");
add(p2, "Center"); p1.add(l1);
p2.setLayout(new GridLayout(3, 6, 5, 5)); tf1.setHorizontalAlignment(JTextField.RIGHT);
tf2.setHorizontalAlignment(JTextField.RIGHT);
tf3.setHorizontalAlignment(JTextField.RIGHT);
tf4.setHorizontalAlignment(JTextField.RIGHT);
tf5.setHorizontalAlignment(JTextField.RIGHT);
tf6.setHorizontalAlignment(JTextField.RIGHT); p2.add(l2);
p2.add(tf1);
p2.add(l3);
p2.add(tf2);
p2.add(l4);
p2.add(tf3);
p2.add(l5);
p2.add(tf4);
p2.add(l6);
p2.add(tf5);
p2.add(l7);
p2.add(tf6);
p2.add(l8);
p2.add(b1);
p2.add(b2);
p2.add(b3);
p2.add(b4); b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
setLayout(new BorderLayout());
add(p1, BorderLayout.NORTH);
add(p2, BorderLayout.CENTER);
add(p3, BorderLayout.SOUTH);
setTitle("定时关机系统");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
} public static void main(String[] args) {
new Test();
} public void time() {
GregorianCalendar gc = new GregorianCalendar();
int hour;
int hou = gc.get(Calendar.HOUR);
int minute = gc.get(Calendar.MINUTE);
int second = gc.get(Calendar.SECOND);
if (hou < 24) {
hour = hou;
} else {
hour = hou - 24;
}
h = String.valueOf(hour);
m = String.valueOf(minute);
s = String.valueOf(second);
}// public void show() {
// tf1.setText(h);
// tf2.setText(m);
// tf3.setText(s);
// } public void close()// 关机
{
try {
Runtime.getRuntime().exec("shutdown.exe -s -t 10");
} catch (IOException e) {
JOptionPane.showMessageDialog(this, "执行失败!");
}
} public void restart()// 重启
{
try {
Runtime.getRuntime().exec("shutdown.exe -r -t 10");
} catch (IOException e) {
JOptionPane.showMessageDialog(this, "执行失败!");
}
} public void logout()// 注销
{
try {
Runtime.getRuntime().exec("shutdown.exe -l");
} catch (IOException e) {
JOptionPane.showMessageDialog(this, "执行失败!");
}
} public void cancel()// 取消
{ tf4.setText("");
tf5.setText("");
tf6.setText("");
} public boolean equal() {
if (h.equals(tf4.getText()) && m.equals(tf5.getText())
&& s.equals(tf6.getText())) {
return true;
} else
return false;
}
@Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
Equals eq = new Equals();
if (command.equals("关机")) {
eq.start();
eq.stop();
close();
} else if (command.equals("重启")) {
eq.start();
eq.stop();
restart();
} else if (command.equals("注销")) {
eq.start();
eq.stop();
logout();
} else if (command.equals("清空")) {
eq.start();
eq.stop();
cancel();
}
}
}
class Equals extends Thread {
public void run() {
Test win = new Test();
while (true) {
try {
Thread.sleep(1000);
win.time();
win.show();
if (win.equal())
break;
else
continue;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}