
I would like to start an application, stop it and restart it again and stop etc. I tried the following code. It does start and stop, but I get an error when I try to restart.
Any suggestions?
/* * To change this template, choose
Tools | Templates * and open the
template in the editor. */
package testactionevent;
import javax.swing.*; import
java.awt.<em>; import java.awt.event.</em>;
/** * * @author */ public
class TestActionEvent extends JFrame
implements ActionListener{
private JButton jbtStart = new JButton("Start");
private JButton jbtStop = new JButton("Stop");
Thread print100 = new Thread(new SiThread());
public TestActionEvent(){
setTitle("TestActionEvent");
getContentPane().setLayout(new FlowLayout());
getContentPane().add(jbtStart);
getContentPane().add(jbtStop);
jbtStart.addActionListener(this);
jbtStop.addActionListener(this); }
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
TestActionEvent frame = new TestActionEvent();
frame.setTitle("Two buttons");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(100, 100);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e){
if (e.getSource() == jbtStart){
System.out.println("Start ");
String a = new Boolean(Thread.currentThread().isInterrupted()).toString();
System.out.println("Start " + a);
if (!print100.isInterrupted()){
System.out.println("Startbbbbbbb ");
print100.start();
}
}
else if (e.getSource() == jbtStop){
print100.interrupt();
}
} }
class SiThread implements Runnable {
public void run() {
int num = 1;
System.out.println(" - " + num);
while (0 == 0){
num = num +1;
System.out.println(" " + num);
if (Thread.interrupted()) {
System.out.println("Thread is
interrupted ");
return;
}
} } }