//  Scott DeRuiter         11/16/2000
//  Multiply.java
//  This program will use two text fields to multiply two numbers.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Multiply extends JApplet   
{

	private Canvas canvas;

	public void init()
	{		
		canvas = new Canvas();
		getContentPane().add ( canvas, BorderLayout.CENTER );
	}
	
	class Canvas extends JPanel implements ActionListener  
	{
		
		private JTextField xvalue, yvalue;
		private double x, y, product;
	
		public Canvas ()   
		{
			JLabel enterx = new JLabel ( "Enter the x value: " );
			add ( enterx );
			xvalue = new JTextField ( 16 );
			add ( xvalue );
			xvalue.addActionListener(this);
	
			JLabel entery = new JLabel ( "Enter the y value: " );
			add ( entery );
			yvalue = new JTextField(16);
			add ( yvalue );
			yvalue.addActionListener(this);
		}
	
		public void paintComponent ( Graphics g )    
		{
			super.paintComponent ( g );
			product = x * y;
			g.drawString ( "" + x + " * " + y + " = " + product, 30, 150 );
		}
	
		public void actionPerformed (ActionEvent event)   
		{
			if ( event.getSource() == xvalue )   
			{
				Double tempx = Double.valueOf ( xvalue.getText() );
				x = tempx.doubleValue();
			}
			if ( event.getSource() == yvalue )    
			{
				Double tempy = Double.valueOf ( yvalue.getText() );
				y = tempy.doubleValue();
			}
			repaint();
		}
	}
}

Back to Lesson 30 Examples

Back to Java Main Page