الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

خطأ في shut down timer

بدأه zenkology في 5 نوفمبر 2011 · 3 رد · 531 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

أقوم بعمل اول برنامج لي في الجافا وهو shutdown للجهاز ولكن ظهرت اخطاء فجأة غير منطقيه بالنسبه لي ومضطر ان اضيف الكود كامل

package shutdown.timer;

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.BorderLayout;
import java.awt.FlowLayout;

public class model extends JFrame {

    private boolean con = true;
    private JComboBox h;
    private JComboBox m;
    private JComboBox ap;
    private JLabel apaa;
    private JLabel hours;
    private JLabel minutes;
    private JRadioButton shutDownradio;
    private JRadioButton restartradio;
    private JLabel shutDownLabel;
    private JLabel resetLabel;
    private JButton submit;
    private JButton reset;
    private JPanel pan1;
    private JPanel pan2;
    private JPanel pan3;
    private Date d1;
    private Date d;
    private String[] hh = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
        "11", "12"};
    private String[] mm = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
        "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21",
        "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32",
        "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43",
        "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54",
        "55", "56", "57", "58", "59"};
    private String[] apa = {"am", "pm"};
    private ButtonGroup bg;

    public model() {
        setupGUI();
        getTime();
        addListeners();

    }

    private void getTime() {
        try {
            d = new Date();
            DateFormat df = new SimpleDateFormat("h:m a");
            df.format(d);
            String x = (String) h.getSelectedItem() + (String) m.getSelectedItem() + (String) ap.getSelectedItem();
            d1 = df.parse(x);
            System.out.println(d1);
        } catch (ParseException ex) {
ex.getMessage();
        }

    }

    private void addListeners() {

        submit.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                con = true;
                while (con) {
                    if (shutDownradio.isSelected()) {
                        if (d1.equals(d)) {
                            try {
                                Runtime runtime = Runtime.getRuntime();
                                Process proc = runtime.exec("shutdown.exe -s -t 0");
                                System.exit(0);
                            } catch (IOException ex) {
                                ex.getMessage();
                            }
                        }

                    } else if (restartradio.isSelected()) {
                        if (d1.equals(d)) {
                            try {
                                Runtime runtime2 = Runtime.getRuntime();
                                Process proc = runtime2.exec("restart.exe -s -t 0");
                                System.exit(0);
                            } catch (IOException ex) {
                                ex.getMessage();
                            }

                        }
                    }
                }
            }
        });
        reset.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
             Thread t=new Thread(){
             public void run(){
             con=false;

             }


             };
             t.start();
            }
        });




    }

    private void setupGUI() {

        // Panels
        pan1 = new JPanel();
        pan2 = new JPanel();
        pan3 = new JPanel();

        // Labels

        resetLabel = new JLabel("Reset");
        shutDownLabel = new JLabel("ShutDown ");
        hours = new JLabel("Hours");
        minutes = new JLabel("Minutes");
        apaa = new JLabel("am/pm");

        // CheckBoxes
        shutDownradio = new JRadioButton();
        restartradio = new JRadioButton();
        bg = new ButtonGroup();
        bg.add(shutDownradio);
        bg.add(restartradio);
        // Buttons

        submit = new JButton("Submit");
        reset = new JButton("Reset");

        // ComboBoxes
        h = new JComboBox(hh);
        m = new JComboBox(mm);
        ap = new JComboBox(apa);
        ap.setSize(20, 10);
        h.setSize(20, 10);
        m.setSize(20, 10);

        // panels configuration
        pan1.setLayout(new FlowLayout());
        pan1.add(shutDownradio);
        pan1.add(shutDownLabel);
        pan1.add(restartradio);
        pan1.add(resetLabel);

        // ---------------------------------------------------------

        pan2.setLayout(new FlowLayout());
        pan2.add(hours);
        pan2.add(h);
        pan2.add(minutes);
        pan2.add(m);
        pan2.add(apaa);
        pan2.add(ap);
        // ---------------------------------------------------------

        pan3.setLayout(new FlowLayout());
        pan3.add(submit);
        pan3.add(reset);

        // Frame Layout

        setSize(350, 150);
        setVisible(true);
        add(pan2, BorderLayout.BEFORE_FIRST_LINE);
        add(pan1, BorderLayout.CENTER);
        add(pan3, BorderLayout.SOUTH);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

    }

    public static void main(String[] args) {
        new model();
    }
}
#2

الخطأ بسب مقارنة التاريخ

قم بطابعة ناتج هذا السطر و إنظر إلى الناتج

String x = (String) h.getSelectedItem() + (String) m.getSelectedItem() + (String) ap.getSelectedItem();

لاحظ ايضأ ان d1 is null.

أعتقد انك ما محتاج لتاريخين للمقارنة (d and 1) ، يمكنك إستخدام تاريخ واحد فقط.

قمت بتعديل الكود و إستخدام String بدلاً من Date لعمل المقارنة

الكود بعد التعديل

لاحظ الفرق ايضاً فى ال shutdown و restart

Process proc = runtime.exec("shutdown.exe -s -t 0");
Process proc = runtime.exec("shutdown.exe -r -t 0");

الكود كامل :

package shutdown.timer;


import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.swing.*;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.BorderLayout;
import java.awt.FlowLayout;

public class Model extends JFrame {

    private boolean con = true;
    private JComboBox h;
    private JComboBox m;
    private JComboBox ap;
    private JLabel apaa;
    private JLabel hours;
    private JLabel minutes;
    private JRadioButton shutDownradio;
    private JRadioButton restartradio;
    private JLabel shutDownLabel;
    private JLabel resetLabel;
    private JButton submit;
    private JButton reset;
    private JPanel pan1;
    private JPanel pan2;
    private JPanel pan3;;
    private Date date;


//    private String[] hh = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
//        "11", "12"};
//    
//    private String[] mm = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
//        "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21",
//        "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32",
//        "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43",
//        "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54",
//        "55", "56", "57", "58", "59"};

    private String[ ]hh = new String[12];
    private String[] mm = new String [59];

    private String[] apa = {"am", "pm"};
    private ButtonGroup bg;

    public Model() {

    	//Setup hh and mm
    	for(int mmIndex = 1; mmIndex <= 59; mmIndex++){
    		String tempMM = "";
    		if(mmIndex <=9 ){
    			tempMM = "0" + mmIndex;
    		}else{
    			tempMM = Integer.toString(mmIndex);
    		}

    		mm[mmIndex-1] = tempMM;
    	}

    	for(int hhIndex = 1; hhIndex <= 12; hhIndex++){
    		String tempHH = "";
    		if(hhIndex <= 9 ){
    			tempHH = "0" + hhIndex;
    		}else{
    			tempHH = Integer.toString(hhIndex);
    		}
    		hh[hhIndex-1] = tempHH;
    	}

        setupGUI();

        addListeners();

    }

    /**
     * Get current system time
     * @return String, fromat (HH:MM AM or PM)
     */
    private String getCurrentTime() {
        date = new Date();
        DateFormat df = new SimpleDateFormat("hh:mm");
        String now = df.format(date);

        return now + " " + ap.getSelectedItem().toString();
    }

    /**
     * Get selected shutdown time
     * @return String, format (HH:MM AM or PM)
     */
    private String getShutdownSelectedTimer(){
    	String shutdownTime  = "";
    	shutdownTime += h.getSelectedItem().toString() + ":" + m.getSelectedItem().toString() + " " + ap.getSelectedItem().toString();
    	return shutdownTime;
    }

    private void addListeners() {

        submit.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
            		Thread runningThread = new Thread(){
            			public void run(){
            				System.out.println("Shutdown Thread has started ... ");

            				while(true){            					

            					try{

            						if(shutDownradio.isSelected()){

            							if(getShutdownSelectedTimer().equals(getCurrentTime())){
            								Runtime runtime = Runtime.getRuntime();
            								Process proc = runtime.exec("shutdown.exe -s -t 0");
            								System.exit(0);
            							}
            							//Sleep for one second and then try shutdown again
            							Thread.sleep(1000);

            						}else if(restartradio.isSelected()){

            							if(getShutdownSelectedTimer().equals(getCurrentTime())){
            								Runtime runtime = Runtime.getRuntime();
            								Process proc = runtime.exec("shutdown.exe -r -t 0");
            								System.exit(0);
            							}
            							//Sleep for one second and then try shutdown again
            							Thread.sleep(1000);

            						}

            					}catch (Exception e) {
            						e.printStackTrace();
								}

        					}//End While
        			}
        		};
        		runningThread.start();
            }
        });


        reset.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
             Thread t=new Thread(){
             public void run(){
             con=false;

             }


             };
             t.start();
            }
        });




    }

    private void setupGUI() {

        // Panels
        pan1 = new JPanel();
        pan2 = new JPanel();
        pan3 = new JPanel();

        // Labels

        resetLabel = new JLabel("Reset");
        shutDownLabel = new JLabel("ShutDown ");
        hours = new JLabel("Hours");
        minutes = new JLabel("Minutes");
        apaa = new JLabel("am/pm");

        // CheckBoxes
        shutDownradio = new JRadioButton();
        shutDownradio.setSelected(true); //Default option is shutdown

        restartradio = new JRadioButton();
        bg = new ButtonGroup();
        bg.add(shutDownradio);
        bg.add(restartradio);
        // Buttons

        submit = new JButton("Submit");
        reset = new JButton("Reset");

        // ComboBoxes
        h = new JComboBox(hh);
        m = new JComboBox(mm);
        ap = new JComboBox(apa);
        ap.setSize(20, 10);
        h.setSize(20, 10);
        m.setSize(20, 10);

        // panels configuration
        pan1.setLayout(new FlowLayout());
        pan1.add(shutDownradio);
        pan1.add(shutDownLabel);
        pan1.add(restartradio);
        pan1.add(resetLabel);

        // ---------------------------------------------------------

        pan2.setLayout(new FlowLayout());
        pan2.add(hours);
        pan2.add(h);
        pan2.add(minutes);
        pan2.add(m);
        pan2.add(apaa);
        pan2.add(ap);
        // ---------------------------------------------------------

        pan3.setLayout(new FlowLayout());
        pan3.add(submit);
        pan3.add(reset);

        // Frame Layout

        setSize(350, 150);
        setVisible(true);
        add(pan2, BorderLayout.BEFORE_FIRST_LINE);
        add(pan1, BorderLayout.CENTER);
        add(pan3, BorderLayout.SOUTH);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

    }

    public static void main(String[] args) {
        new Model();
    }
}
1
#3

اشكرك جدا :)

#4

العفو :)

مواضيع مشابهة