import java.applet.Applet; import java.awt.*; import java.awt.event.*; /** * Tests the operation of the Fraction Class * * @author Mr J * @version 20040212 */ public class TestFraction extends Applet implements ActionListener { // input boxes Label topLabel = new Label("Numerators:"); Label botLabel = new Label("Demoninators:"); TextField n1 = new TextField("n1"); TextField n2 = new TextField("n2"); TextField d1 = new TextField("d1"); TextField d2 = new TextField("d2"); private Button addButton = new Button("Add"); private Label greeting = new Label("Applet that adds two fractions"); /** * Add objects to the Applet */ public void init() { Panel inputPanel = new Panel(); inputPanel.setLayout(new GridLayout(2, 3)); inputPanel.add(topLabel); inputPanel.add(n1); inputPanel.add(n2); inputPanel.add(botLabel); inputPanel.add(d1); inputPanel.add(d2); add(inputPanel); add(addButton); add(greeting); // Causes button presses to be detected addButton.addActionListener(this); } /** * When an event occurs on an object with an ActionListener attached, this * method is carried out. * * @param e carries details about the event that occurred */ public void actionPerformed(ActionEvent e) { // Get the data from the boxes and make the fraction objects Fraction a = new Fraction( Integer.parseInt(n1.getText()), Integer.parseInt(d1.getText()) ); Fraction b = new Fraction( Integer.parseInt(n2.getText()), Integer.parseInt(d2.getText()) ); // add them together Fraction c = Fraction.add( a, b ); greeting.setText("" + c.toString()); } }