I have two classes that I want to run in separate threads in the same applet:
ScrollingBanner.java
public class ScrollingBanner implements Runnable { private String msg = "This is scrolling text!"; private BannerCalcApplet bca; ScrollingBanner(BannerCalcApplet bca) { this.bca = bca; } public void run() { char ch; for( ; ; ) { try { Thread.sleep(250); ch = msg.charAt(0); msg = msg.substring(1, msg.length()); msg += ch; } catch (InterruptedException ie) { } } } }
CalculatorApplet.java
import java.applet.*; import java.awt.*; import java.awt.event.*; /* <applet code = "CalculatorApplet" width = 225 height = 300> </applet> */ public class CalculatorApplet extends Applet { private TextField fArgTxt; private TextField sArgTxt; private TextField ansTxt; private BannerCalcApplet bca; CalculatorApplet(BannerCalcApplet bca) { this.bca = bca; } public void init () { // Construct the TextFields this.fArgTxt = new TextField(25); this.sArgTxt = new TextField(25); this.ansTxt = new TextField(25); this.ansTxt.setEditable(false); Button adds = new Button("+"); Button subs = new Button("-"); Button mults = new Button("*"); Button divs = new Button("/"); // add the button to the layout this.add(fArgTxt); this.add(sArgTxt); this.add(ansTxt); this.add(adds); this.add(subs); this.add(mults); this.add(divs); AdditionAction aa = new AdditionAction(fArgTxt, sArgTxt, ansTxt); adds.addActionListener(aa); this.fArgTxt.addActionListener(aa); this.sArgTxt.addActionListener(aa); SubtractionAction sa = new SubtractionAction(fArgTxt, sArgTxt, ansTxt); subs.addActionListener(sa); this.fArgTxt.addActionListener(sa); this.sArgTxt.addActionListener(sa); MultiplicationAction ma = new MultiplicationAction(fArgTxt, sArgTxt, ansTxt); mults.addActionListener(ma); this.fArgTxt.addActionListener(ma); this.sArgTxt.addActionListener(ma); DivisionAction da = new DivisionAction(fArgTxt, sArgTxt, ansTxt); divs.addActionListener(da); this.fArgTxt.addActionListener(da); this.sArgTxt.addActionListener(da); // notice that ActionEvents produced by ansTxt are ignored. } } class AdditionAction implements ActionListener { private TextField arg1Txt; private TextField arg2Txt; private TextField answTxt; public AdditionAction(TextField arg1Txt, TextField arg2Txt, TextField answTxt) { this.arg1Txt = arg1Txt; this.arg2Txt = arg2Txt; this.answTxt = answTxt; } public void actionPerformed(ActionEvent ae) { double arg1Val = Double.parseDouble(arg1Txt.getText()); double arg2Val = Double.parseDouble(arg2Txt.getText()); double sumVal = arg1Val + arg2Val; answTxt.setText("" + sumVal); } } class SubtractionAction implements ActionListener { private TextField arg1Txt; private TextField arg2Txt; private TextField answTxt; public SubtractionAction(TextField arg1Txt, TextField arg2Txt, TextField answTxt) { this.arg1Txt = arg1Txt; this.arg2Txt = arg2Txt; this.answTxt = answTxt; } public void actionPerformed(ActionEvent ae) { double arg1Val = Double.parseDouble(arg1Txt.getText()); double arg2Val = Double.parseDouble(arg2Txt.getText()); double difVal = arg1Val - arg2Val; answTxt.setText("" + difVal); } } class MultiplicationAction implements ActionListener { private TextField arg1Txt; private TextField arg2Txt; private TextField answTxt; public MultiplicationAction(TextField arg1Txt, TextField arg2Txt, TextField answTxt) { this.arg1Txt = arg1Txt; this.arg2Txt = arg2Txt; this.answTxt = answTxt; } public void actionPerformed(ActionEvent ae) { double arg1Val = Double.parseDouble(arg1Txt.getText()); double arg2Val = Double.parseDouble(arg2Txt.getText()); double prodVal = arg1Val * arg2Val; answTxt.setText("" + prodVal); } } class DivisionAction implements ActionListener { private TextField arg1Txt; private TextField arg2Txt; private TextField answTxt; public DivisionAction(TextField arg1Txt, TextField arg2Txt, TextField answTxt) { this.arg1Txt = arg1Txt; this.arg2Txt = arg2Txt; this.answTxt = answTxt; } public void actionPerformed(ActionEvent ae) { double arg1Val = Double.parseDouble(arg1Txt.getText()); double arg2Val = Double.parseDouble(arg2Txt.getText()); double quotVal = arg1Val / arg2Val; answTxt.setText("" + quotVal); } }
BannerCalcApplet.java
I tried this:
import java.applet.*; import java.awt.*; /* <applet code = "BannerCalcApplet" width = 225 height = 300 </applet> */ class BannerCalcApplet extends Applet { ScrollingBanner sb; CalculatorApplet ca; Thread sbt; Thread cat; BannerCalcApplet bca = this; public void init() { sb = new ScrollingBanner(this); sbt = new Thread(sb); ca = new CalculatorApplet(this); cat = new Thread (ca); sbt.start(); cat.start(); } }
But I am unsure how to actually create a window into which I can layout the banner and calculator. Also, the code doesn't compile, because the compiler doesn't recognize the form of the Thread constructor that takes the CalculatorApplet object as an argument but there is no compiler error for the corresponding Thread constructor that takes the ScrollingBanner object as an argument.
I'm probably missing something very basic, but I would appreciate it if someone pointed it out to me.