Hey having fun with the shutdown? Well I have lots of other
stuffs like how to extend the shutdown button so as you could hibernate sleep
and even restart your system. You can even add delay time before the shutdown. If
you haven’t seen our tutorial on shutdown, please click this link.
Requirements: your JAVA IDE
and a little pinch of logic.
1.
Open your
Netbean. And start a new Project.
If you don’t know how to start a new project
in NETBEAN check out this tutorial
2.
Add a new JFrame. [Right click on the project
name on the projects panel like in my case I right clicked on ShutDownExtended=> select New=>JFrame
Form…]. Rename the class to any name you like but note: it must follow
these naming conventions.
a.
The Class name must start with an Upper letter
e.g Gandhi.
b.
Every word must be joint together with an uppercase
letter starting each word e.g Gandhi Berry should be written as GandhiBerry.
c.
And No a class name cannot start with numbers
3.
On the Form drag in 6 buttons and one text field.
Don’t ask me why just drag.
4.
Now rename each button to make your interface to
like mine.
5.
Now we begin the coding. Double click on the
shutdown Button. It takes you to the code window. Normally we are supposed to
type our codes in the area we find our cursor blinking but we aren’t gonna do
that. Instead click outside of the ending curly brace “}” and then type this
command like so.
Runtime r= Runtime.getRuntime();
6.
Now return your cursor back to where it was
blinking at first before you changed its location. In between the curly braces “{“
and “}” type this code.
Try{
Process
Proc=r.exec(“Shutdown –s –t 0”);
}catch(Exception Ex){
}
You can perform an emergency
shutdown by changing the –t to –p and see how that goes.
7.
Nice we just added our shutdown code again but
this time the runtime object is outside of the class. Reason is because we want
this object to be accessible by other methods so we had to do that.
8.
To create the restart button, click on design then double click on the restart button and add these codes.
Try{
Process Proc=r exec(“Shutdown –r
–t 0”);
}catch(Exception
Ex){
}
9.
Repeat step 8 for logoff, hibernate and abort
but change –r to –I for logoff, change –r to –h for hibernate and change –r to –a
for abort.
LOGGOFF
HIBERNATE
ABORT:
You can prevent a shutdown by using this button immediately
after clicking shutdown.
10.
To make the timed shutdown we will let the user
to enter how many seconds he/ she wants the system to delay before shutting
down. Note the highest number of time is 10 years so if you want more than that,
ask Microsoft. Double click on the Timed
shutdown button. And type these codes
int x;
if(jTextField1.getText().isEmpty()){
x=0;
}else{
x=Integer.parseInt(jTextField1.getText());
}
try{
Process Proc =r.exec("Shutdown -s
-t" + x);
}catch(Exception Ex){
}