I have created a GUI for starting a Thread which does something very simple. However, the child thread never starts.
The child thread, if started, will give some output; I don't get any output though. What am I missing?
Here's the Code:
The GUI class:
import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; public class gui { public static void main(String [] args) { //final standalone s = new standalone(); final try2 t= new try2(); JFrame win = new JFrame(); win.setLayout(new FlowLayout()); JButton start = new JButton("Start"); JButton stop = new JButton("Stop"); start.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub t.t.start(); System.out.println("M on!"); try{ Thread.currentThread().sleep(10000); }catch(Exception e1) { e1.printStackTrace(); } System.out.println("M off!"); if(t.t.isInterrupted()) System.out.println("Stopped"); } }); win.add(start); win.add(stop); win.setVisible(true); } }
And here is the Child Thread
public class try2 implements Runnable { public Thread t; int i; try2() { t=new Thread(); } public void run() { System.out.println(++i); } }