package colaBot.testers; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; public class threadtest implements Runnable, ActionListener { Thread t; static JButton Start = new JButton("Start"); static JButton Stop = new JButton("Stop"); static JFrame ff = new JFrame("Test"); public void run() { try { Thread.sleep(30000); } catch(InterruptedException ie) { JOptionPane.showMessageDialog(null, "I was sleeping there!"); return; } catch(Exception e) { System.out.println(e.toString()); } JOptionPane.showMessageDialog(null, "Eggs are not supposed to be green."); } public threadtest() { ff.add("North",Start); ff.add("South",Stop); Start.addActionListener(this); Stop.addActionListener(this); Stop.setEnabled(false); ff.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent evt) { ff.setVisible(false); System.exit(0); } }); ff.setVisible(true); ff.pack(); } public void actionPerformed(ActionEvent e) { if(e.getSource() == Start) { Start.setEnabled(false); Stop.setEnabled(true); t = new Thread(this); t.start(); } if(e.getSource() == Stop) { Start.setEnabled(true); Stop.setEnabled(false); t.interrupt(); } } /** * @param args */ public static void main(String[] args) { new threadtest(); } }