I have some problems with my code and i would need some help if you can please(and explain it a bit so i can understand in the future:)), so this is my code and what i need is that my JButton to action a shutdown command and the shutdown command to be delayed from the seconds i input in my JTextfield. So my code so far is :
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);
}
}
Thank you or the help in advanced !!! :D
Lots of things jump out at me here, but...
Redeclare text1
as JTextField
instead of an InputStream
...
//InputStream text1;
private JTextField text1;
This will allow you to access the field and it's value from anywhere in the class.
Make sure you aren't shadowing the variables when you create the text field...
//JTextField text1 = new JTextField();
text1 = new JTextField(10);
Make use of ProcessBuilder
instead of Runtime.getRuntime()
. It will make your life easier to deals with parameters much better
ProcessBuilder pb = new ProcessBuilder("shutdown", "-s", "-t", text1.getText());
pb.redirectError();
Process p = pb.start();
The action command will always be null
as you never set, so the following will cause you a NullPointerException
String actionstart = arg0.getActionCommand();
if(actionstart.equals("Start Shudown")){
When you create your button, you need to set the action command...
JButton start = new JButton("Start Shudown");
start.setActionCommand("Start Shudown");
Additional suggestions...
JFrame
. Instead, base you application on something like JPanel
. It makes your application for flexible and re-usable. All you need to do is make the JTextField a field:
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{
JTextField 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);
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();
long a=Long.parseLong(text1.getText());
Process proc = runtime.exec("shutdown -s -t "+a);
System.exit(0);
}
}
If it's global, you can use it in any of this object's functions, so you can get the Text from the textField from anywhere you want inside your JFrame Object.
I hope this is what you want to do. If its not explained well enough, please tell me. ;)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.