Discord Server Twitter
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Tutorial Java Shut down the PC after specified amount of time
#1
In The Name OF Allah
Al-Salam Alekum

Hi guys, today we will make a simple app for shutting down the PC after specified amount of time in Java Wink
Add two Text Field and a button to your JFrame, then Double Click on the Button, our work Starts here :Tongue
The First textField is for the hours, the second is for minutes. Okay we will get the text and convert it to int:

    
Code:
try{
            int hours = Integer.parseInt(jTextField1.getText()); //get the text and convert it to integer
            int minutes = Integer.parseInt(jTextField2.getText()); //get the text and convert it to integer
            Thread.sleep((hours * 60 * 60 * 1000) + (minutes * 60 * 1000)); //Here the thread will sleep until the time to shutdown the PC.
            Runtime.getRuntime().exec("shutdown -s -t 30"); //shutting down the PC after 30 sec of executing the code.
         }
    catch(NumberFormatException | InterruptedException | IOException e){
            JOptionPane.showMessageDialog(this, "Pls fill the TextFields", "Erro", JOptionPane.ERROR_MESSAGE);
         }

Big Grin
Wa Salam Alekum

[Image: 0yaXvcy.gif]
We Are Anonymous, We Are Legion, We Do not Forgive, We do not Forget
Expect Us
Reply




Users browsing this thread: 1 Guest(s)