I have written a
Java applet to demonstrate starting 3 threads, but when clicking on the "Start Threads Button" nothing happens.
The program compiles correctly.
My code is listed below. Any help would be appreciated.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;
public class Lab2_2Applet extends Applet implements Runnable
{
static JFrame aWindow = new JFrame("Thread Applet");
// Define threads.
Thread Child1Thread;
Thread Child2Thread;
Thread ParentThread;
//Will be T as long as threads run.
boolean Trunning = true;
//Label to display Child1 Message
JLabel Child1Label = new JLabel(" ", JLabel.RIGHT);
//Label to display Child2 Message
JLabel Child2Label = new JLabel(" ", JLabel.RIGHT);
//Label to display Parent Message
JLabel ParentLabel = new JLabel(" ", JLabel.RIGHT);
//Button to start Thread Program
JButton StartThreadsButton = new JButton("Start Threads");
public void init()
{
Toolkit theKit = aWindow.getToolkit();
Dimension wndSize = theKit.getScreenSize();
aWindow.setBounds(wndSize.width/4, wndSize.height/4,
wndSize.width/2, wndSize.height/2);
FlowLayout flow = new FlowLayout(FlowLayout.LEFT, 40, 10);
Container content = aWindow.getContentPane();
content.setLayout(flow);
content.add(Child1Label);
content.add(Child2Label);
content.add(ParentLabel);
content.add(StartThreadsButton);
// Create Threads.
Child1Thread = new Thread();
Child2Thread = new Thread();
ParentThread = new Thread();
aWindow.setVisible(true);
ActionListener al = new BListener();
StartThreadsButton.addActionListener(al);
}//INIT()
public void destroy()
{
//Thread will stop running
Trunning = false;
// Destroy threads
Child2Thread = null;
Child2Thread = null;
ParentThread = null;
}//END PUBLIC VOID DESTROY
public void run()
{
while (Trunning)
{
// Put text textfield
Child1Label.setText("I am child #1");
Child2Label.setText("I am child #2");
ParentLabel.setText("I am parent");
}//WHILE
}//END PUBLIC VOID RUN
class BListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
//Start Threads
Child1Thread.start();
Child2Thread.start();
ParentThread.start();
}//ACTIONPERFORMED
}//BLISTENER
}//CLASS Lab2_2Applet