Extinction automatique

Description

Ce programme vous permet d'éteindre, de redémarrer,de mettre en veille ou de déconnecter automatiquement un PC a une date préalablement choisie. et cela fonctionne sur tous les systemes d'exploitations. Ce serait cool si vous l'essayer et relever les insuffisances et les bugs merci.

Source / Exemple :


package eteindrepcenyo;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.sql.Time;
import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public final class decompteur extends JFrame{
     Timer tt;
      Font police = new Font("Arial", Font.BOLD, 14);
    int heure,minute,seconde;
    String Action;
    JPanel pan=new JPanel();
    JLabel affn=new JLabel();
    JLabel aff=new JLabel(" secondes Avant");
    JLabel affe=new JLabel("l'extension");
    JLabel affr=new JLabel("le redémarrage");
    JButton Anul=new JButton("Cancel");
    decompteur(int heur,int min,int sec,String Actio)
    {
        heure=heur;
        minute=min;
        seconde=sec;
        Action=Actio;
        
         

     //Définit un titre pour votre fenêtre
         this.setTitle("EteinddrePCenyo");
         this.setResizable(false);
         //Définit une taille pour celle-ci ; ici, 400 px de large et 500 px de haut
         this.setSize(300, 100);
         //Nous allons maintenant dire à notre objet de se positionner au centre
         this.setLocationRelativeTo(null);
         //Ferme-toi lorsqu'on clique sur "Fermer" !
         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         pan.setLayout(new GridLayout(2,1));
         if(Action.equals("Eteindre"))
         {
             affe.setText("l'exteinsion");
             
         }
         else
         {
             if(Action.equals("Mettre en veille prolongée"))
                 affe.setText("veille Pro.");
             else
             {
                 if(Action.equals("Fermer session"))
                   affe.setText("Fermeture de session");
                 else
             affe.setText("le redémarrage");
             }
         }
         
         JPanel PanAffich=new JPanel();
         PanAffich.setLayout(new FlowLayout());
         PanAffich.add(affn);
         PanAffich.add(aff);
         PanAffich.add(affe);
         pan.add(PanAffich);
         pan.add(Anul);
         
         Anul.addActionListener(new ActionListener()
         {

            @Override
            public void actionPerformed(ActionEvent e) {
                tt.cancel();
                setVisible(false);
                fenetre f=new fenetre();
            }
             
         });
          start1();
           this.setContentPane(pan);
         this.setVisible(true);
    }
    void start1()
    {
        Time t2=new Time(System.currentTimeMillis());
        
         Time t=new Time(heure,minute,seconde);
         int secondeTotale=(heure*3600)+(minute*60)+seconde;
         int secondeTotale2=(t2.getHours()*3600)+(t2.getMinutes()*60)+t2.getSeconds();
          int duree=(secondeTotale-secondeTotale2);
         if(duree<0)
         {
             duree=((secondeTotale+86400)-(secondeTotale2-secondeTotale));
         }
         
         
          System.out.println(t2.toString());
         System.out.println(t.toString());
         System.out.println(secondeTotale);
         System.out.println(secondeTotale2);
        
        
           System.out.println(duree);
         RepetAction r=new RepetAction(duree);
    }
    class RepetAction {
  Time t3;
int nbseconde;
  public RepetAction(int second) {
      
      nbseconde=second;
    tt = new Timer();
    tt.schedule(new MonAction(), 0, 1*1000);
    
    }

  class MonAction extends TimerTask {
    

            @Override
    public void run() {
      if (nbseconde >= 0) {
          
          int seconde=nbseconde%60;
          int minute=(nbseconde/60)%60;
          int heure=(nbseconde/60)/60;
          
        //  t3=new Time((long)(nbseconde*1000));
          if(nbseconde%2==0)
          {
              affn.setForeground(Color.BLACK);
          }
          else
              affn.setForeground(Color.BLUE);
              
          affn.setText(heure+":"+minute+":"+seconde);
         System.out.println(heure+":"+minute+":"+seconde);
          String j="";
       //   j=String.valueOf(nbseconde);
       // affn.setText(j);
        affn.setFont(police);
affe.setFont(police);
aff.setFont(police);

        nbseconde--;
        
        
      } else {
        
        if(Action.equals("Eteindre"))
         {
                        try {
                            affn.setText("");
                            affe.setText("");
                            aff.setText("Arrêt");
                            exteinction();
                        } catch (IOException ex) {
                            Logger.getLogger(decompteur.class.getName()).log(Level.SEVERE, null, ex);
                        }
             
         }
         else
        {
            if(Action.equals("Mettre en veille prolongée"))
            {
                try {
                            affn.setText("");
                             affe.setText("");
                             aff.setText("la mise en veille");
                             exteinction();
                        } catch (IOException ex) {
                            Logger.getLogger(decompteur.class.getName()).log(Level.SEVERE, null, ex);
                        } 
            }
            else
            {
                if(Action.equals("Fermer session"))
                {
                     try {
                            affn.setText("");
                             affe.setText("");
                             aff.setText("Fermeture de session");
                             exteinction();
                        } catch (IOException ex) {
                            Logger.getLogger(decompteur.class.getName()).log(Level.SEVERE, null, ex);
                        }
                }
                else
                {
                        try {
                            affn.setText("");
                             affe.setText("");
                             aff.setText("Redemarrage");
                             exteinction();
                        } catch (IOException ex) {
                            Logger.getLogger(decompteur.class.getName()).log(Level.SEVERE, null, ex);
                        }
                }
            }
        }
        tt.cancel();
        }
      }
    }
  }
    void exteinction() throws IOException
    {
        String commande="";
                String operatingSystem = System.getProperty("os.name");
           System.out.println(operatingSystem);
        // Sous Windows
        if (operatingSystem.startsWith("Windows"))
              {
                  if(Action.equals("Eteindre"))          
        commande = "shutdown.exe -s -t 0";
                  else
                  {
                      if(Action.equals("Mettre en veille prolongée"))
                          commande="shutdown -h";
                      else
                      {
                          if(Action.equals("Fermer session"))
                              commande="shutdown -l";
                          else
                      commande = "shutdown -r";
                      }
                  }
              }
 
        // Sous Linux ou Mac
        else if (operatingSystem.startsWith("Linux") || operatingSystem.startsWith("Mac OS X"))
             {
                 if(Action.equals("Eteindre")) 
              commande = "shutdown -h now";
                 else
                 {
                     if(operatingSystem.startsWith("Linux"))
                     commande = "reboot";
                     else
                         commande = "shutdown -r now";
                 }
              }
 
            else
             {
        affn.setText("Impossible d'éteindre ce PC");
             }
 
          Runtime.getRuntime().exec(commande);
          System.exit(0);
    }
}

public class fenetre extends JFrame{
    JPanel pan=new JPanel();
    JButton Ok=new JButton("OK");
    JButton Anul=new JButton("Cancel");
    String []StringHeure=new String[24];
    String []StringMinute=new String[60];
    String []StringSeconde=new String[60];
    JComboBox AC=new JComboBox();
     
    JLabel Titre=new JLabel("Choisissez l'heure a laquelle vous voulez Eteindre");
    JLabel heu=new JLabel("heure(s)");
    JLabel min=new JLabel("minute(s)");
    JLabel sec=new JLabel("seconde(s)");
    JLabel choi=new JLabel("Que voulez vous faire? ");
   JComboBox heure;
    JComboBox minute;
     JComboBox seconde;
     public fenetre(){
        
        Font police = new Font("Arial", Font.BOLD, 14);
         Font police1 = new Font("Bauhaus 93", Font.ITALIC, 22);
Titre.setFont(police1);
Titre.setForeground(Color.DARK_GRAY);

heu.setFont(police);
heu.setForeground(Color.cyan);

min.setFont(police);
min.setForeground(Color.MAGENTA);

sec.setFont(police);
sec.setForeground(Color.GREEN);

choi.setFont(police);
choi.setForeground(Color.PINK);

         //Définit un titre pour votre fenêtre
         this.setTitle("EteinddrePCenyo");
         //Définit une taille pour celle-ci ; ici, 400 px de large et 500 px de haut
         this.setSize(500, 200);
        this.setResizable(false);
         //Nous allons maintenant dire à notre objet de se positionner au centre
         this.setLocationRelativeTo(null);
         //Ferme-toi lorsqu'on clique sur "Fermer" !
         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
             pan.setLayout(new GridLayout(4,1));
            
             for(int i=1;i<=24;i++)
             {                           
                 StringHeure[i-1]=String.valueOf(i);
             }
             for(int i=0;i<60;i++)
             {                                
                 StringMinute[i]=String.valueOf(i);
             }
             for(int i=0;i<60;i++)
             {                                   
                 StringSeconde[i]=String.valueOf(i);
             }             
               heure=new JComboBox(StringHeure);
               minute=new JComboBox(StringMinute);
               seconde=new JComboBox(StringSeconde);
             heure.setFont(police);
             heure.setForeground(Color.BLUE);
 
minute.setFont(police);
minute.setForeground(Color.orange);

seconde.setFont(police);
seconde.setForeground(Color.lightGray);

AC.setFont(police);
AC.setForeground(Color.red);

              AC.addItem("Eteindre");
              AC.addItem("Redemarrer");
              AC.addItem("Mettre en veille prolongée");
              AC.addItem("Fermer session");
              JPanel temps=new JPanel();
              temps.setLayout(new GridLayout(1,6));
              heure.setPreferredSize(new Dimension(400,10));
              temps.add(heure);
              temps.add(heu);
              temps.add(minute);
              temps.add(min);
              temps.add(seconde);
              temps.add(sec);
              
              JPanel ACttion=new JPanel();
              ACttion.setLayout(new GridLayout(1,2));
              ACttion.add(choi);
              ACttion.add(AC);
              
              
              JPanel valide=new JPanel();
              valide.add(Ok);
              valide.add(Anul);
              
              pan.add(Titre);
              pan.add(temps);
              pan.add(ACttion); 
              pan.add(valide);
                 
              pan.setBackground(Color.white);
  valide.setBackground(Color.white);
   ACttion.setBackground(Color.white);
      temps.setBackground(Color.white);
                 Ok.addActionListener(new ActionListener()
                 {

            @Override
            public void actionPerformed(ActionEvent e) {
               decompteur d=new decompteur(Integer.valueOf((String)heure.getSelectedItem()).intValue(),Integer.valueOf((String)minute.getSelectedItem()).intValue(),Integer.valueOf((String)seconde.getSelectedItem()).intValue(),(String)AC.getSelectedItem());
           setVisible(false);
            }
                     
                 });
                  Anul.addActionListener(new ActionListener()
                 {

            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
                     
                 });
                 
             this.setContentPane(pan);
         this.setVisible(true);
     }

    
}

public class EteindrePCenyo {

    /**

  • @param args the command line arguments
  • /
public static void main(String[] args) { fenetre f=new fenetre(); } }

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.