Restarter

Ein kleines Programm, dem ich den Namen „Restarter“ gegeben habe. Man kann einen Timer einstellen und zwischen den Optionen „Herunterfahren“, „Neu starten“, „Benutzer abmelden“ wählen.

  Restarter.rar (35,1 KiB, 64 hits)

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SpinnerNumberModel;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;

/**
 * @author Armin
 * @since 16.10.2012
 * info@prog-blog.de
 * www.prog-blog.de
 */
public class Restarter extends JFrame {

	private static final long serialVersionUID = -7459160975308208169L;
	private JButton cancel;
	private JComboBox comboBox;
	private JPanel contentPane;
	private JButton submit;
	private JTextField txtRestartInXxx;
	private JSpinner spinner;
	private Thread workerThread;
	
	private static String commandShutDown = "shutdown -s";
	private static String commandRestart = "shutdown -r";
	private static String commandLogout = "shutdown -l";
	
	private static String shutDown = "Shut down";
	private static String restart = "Restart";
	private static String logout = "Logout";
	
	public Restarter() {
		
		initGUI();
	}
	
	private void initGUI() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 166, 212);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		Integer start = new Integer(1);
		Integer min = new Integer(1);
		Integer max = new Integer(600);
		Integer step = new Integer(1);
		SpinnerNumberModel model = new SpinnerNumberModel(start, min, max, step);

		spinner = new JSpinner(model);
		spinner.setBounds(10, 11, 100, 20);
		contentPane.add(spinner);
		
		JLabel lblMin = new JLabel("min.");
		lblMin.setBounds(120, 14, 20, 14);
		contentPane.add(lblMin);		
		
		String[] valuesComboBox = {shutDown, restart, logout};
		comboBox = new JComboBox(valuesComboBox);		
		comboBox.setBounds(10, 42, 130, 20);
		contentPane.add(comboBox);
		
		submit = new JButton("Submit!");
		submit.addActionListener(new SubmitAction());
		submit.setBounds(10, 73, 130, 23);
		contentPane.add(submit);
		
		txtRestartInXxx = new JTextField();
		txtRestartInXxx.setEditable(false);
		txtRestartInXxx.setBounds(10, 107, 130, 20);
		contentPane.add(txtRestartInXxx);
		txtRestartInXxx.setColumns(10);
		
		cancel = new JButton("Cancel");
		cancel.setBounds(75, 138, 65, 23);
		cancel.setEnabled(false);
		cancel.addActionListener(new CancelAction());
		contentPane.add(cancel);
		
		setResizable(false);
		setVisible(true);
	}
	
	class SubmitAction implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			
			cancel.setEnabled(true);
			submit.setEnabled(false);
			spinner.setEnabled(false);
			comboBox.setEnabled(false);
									
			final int counter = (Integer) spinner.getValue() - 1;
			
			workerThread = new Thread(new Runnable() {
				
				@Override
				public void run() {
					for (int i = counter; i >= 0; i--) {
												
						for (int j = 59; j >= 10; j--) {
							
							txtRestartInXxx.setText(i + ":" + j + " till " + comboBox.getSelectedItem());
														
							try {
								Thread.sleep(1000);
							} catch (InterruptedException e) {
								e.printStackTrace();
							}
						}
						
						for (int j = 9; j >= 0; j--) {
							
							txtRestartInXxx.setText(i + ":0" + j + " till " + comboBox.getSelectedItem());
														
							try {
								Thread.sleep(1000);
							} catch (InterruptedException e) {
								e.printStackTrace();
							}
						}
					}
										
					try {
					
						Runtime rt = Runtime.getRuntime();

						if (comboBox.getSelectedItem().equals(shutDown)) {							
							rt.exec(commandShutDown);
						} else if (comboBox.getSelectedItem().equals(restart)) {
							rt.exec(commandRestart);
						} else if (comboBox.getSelectedItem().equals(logout)) {
							rt.exec(commandLogout);
						}

						// schließt das Programm
						System.exit(0);
						
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			});
			
			workerThread.start();
			
			// now action!
		}
		
	}
	

	class CancelAction implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			
			cancel.setEnabled(false);
			submit.setEnabled(true);
			spinner.setEnabled(true);
			comboBox.setEnabled(true);
			txtRestartInXxx.setText("");
			
			workerThread.stop();
		}
	}
	
	public static void main(String[] args) {
		
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		
		new Restarter();
	}
}

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht.