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

解决方案 »

  1.   

    估计是你的环境变量改了 然后启动不了shutdown 我的也是 在配置java环境的时候不小心把path的变量改了 现在还不知怎么弄回来
      

  2.   

    你试试看在命令符输入shutdown 有没有显示 没有就说明和我说的一样 有些程序不用到path的变量所以其他可以啊
      

  3.   

    我上面已经说了 我的变量也改了 所以我shutdown也运行不了 郁闷死我
      

  4.   

    shutdown  能运行的  刚注销了 
      

  5.   

    Because some native platforms only provide limited buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block, and even deadlock.if the program you launch produces output or expects input, ensure that you process the input and output streams. 
      

  6.   

    http://www.blogjava.net/westwin/archive/2005/07/29/8698.html?opt=admin
      

  7.   

    不是要这么写么?cmd.exe /c shutdown -s -t 10
      

  8.   

    你的Button上面都没加监听器 怎么会有反应呢 
      

  9.   

    你说的是没有界面出来吗,我开始运行的时候是没有界面出来,不过把show()方法注释掉之后就ok了,我只测试了注销方法,是可以注销的.import java.awt.BorderLayout;
    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();
    }
    }
    }
    }
      

  10.   

    都是调用dos 命令,没有什么好新鲜事的
      

  11.   

    何必这么麻烦,  直接shutdown -s -f -t 0   就可以睡觉去了。
      

  12.   

    我也发现show()方法注释后可以执行    但不是定时的  也就是说判断时间是否相等的线程没用上就直接注销了