我的代码有一些问题,如果您可以的话,我需要一些帮助(并解释一下,以便我将来能理解:)),所以这是我的代码,我需要的是我的 JButton 来执行关闭命令和关闭命令从我在 JTextfield 中输入的秒数开始延迟。所以到目前为止我的代码是:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class Shutdown extends JFrame{
InputStream text1;
JButton start;
String shutdownCmd;
public Shutdown() {
this.setTitle("Shutdown When you want");
setSize(300, 150);
setResizable(false);
setLocation(370, 150);
setLayout(null);
JLabel desc1 = new JLabel("Time until shutdown : ");
desc1.setBounds(95, 25, 125, 25);
add(desc1);
JTextField text1 = new JTextField();
text1.setBounds(95, 45, 120, 25);
text1.setForeground(Color.BLACK);
text1.setToolTipText("Introdu textu aici");
add(text1);
JButton start = new JButton("Start Shudown");
start.setBounds(95, 75, 120, 25);
add(start);
ActionListener eventstart = new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO auto- generated method
String actionstart = arg0.getActionCommand();
if(actionstart.equals("Start Shudown")){
try {
ShutdownCmd();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};
start.addActionListener(eventstart);
}
public void ShutdownCmd() throws IOException{
Runtime runtime = Runtime.getRuntime();
BufferedReader br=new BufferedReader(new InputStreamReader(text1));
long a=Long.parseLong(br.readLine());
Process proc = runtime.exec("shutdown -s -t "+a);
System.exit(0);
}
}
谢谢您或高级的帮助!:D