FileDocCategorySizeDatePackage
Calculator.javaAPI DocExample2196Thu Apr 17 10:38:16 BST 1997None

Calculator

public class Calculator extends Applet implements ActionListener, ContainerListener

Fields Summary
GridBagConstraints
gbc
TextField
theDisplay
Constructors Summary
Methods Summary
public voidactionPerformed(java.awt.event.ActionEvent e)

		if ( e.getActionCommand().equals("C") )
			theDisplay.setText( "" );
		else 
			theDisplay.setText( theDisplay.getText() + e.getActionCommand() );
	
private voidaddGB(java.awt.Container cont, java.awt.Component comp, int x, int y)

		if ( ! (cont.getLayout() instanceof GridBagLayout) )
			cont.setLayout( new GridBagLayout() );

        gbc.gridx = x;  gbc.gridy = y;
        cont.add( comp, gbc );
	
public voidcomponentAdded(java.awt.event.ContainerEvent e)

		Component comp = e.getChild();
		if ( comp instanceof Button )
			((Button)comp).addActionListener( this );
	
public voidcomponentRemoved(java.awt.event.ContainerEvent e)

 
public voidinit()


	   
		setFont( new Font("Monospaced", Font.BOLD, 24) );
		addContainerListener( this );

		gbc.gridwidth=4;
		addGB( this, theDisplay, 0, 0 );

		// make the top row
		Panel topRow = new Panel(); 
		topRow.addContainerListener( this );
		gbc.gridwidth = 1;
		gbc.weightx = 1.0;
		addGB( topRow, new Button("C"), 0, 0 );
		gbc.weightx = 0.33;
		addGB( topRow, new Button("%"), 1, 0 );
		gbc.weightx = 1.0;
		addGB( topRow, new Button("+"), 2, 0 );
		gbc.gridwidth = 4;
		addGB( this, topRow, 0, 1 );

		gbc.weightx = 1.0;  gbc.gridwidth = 1;

		// make the digits
		for(int j=0; j<3; j++)
			for(int i=0; i<3; i++)
				addGB( this, new Button( "" + ((2-j)*3+i+1) ), i, j+2 );

		// -, x, and divide
		addGB( this, new Button("-"), 3, 2 );
		addGB( this, new Button("x"), 3, 3 );
		addGB( this, new Button("\u00F7"), 3, 4 );

		// make the bottom row
		Panel bottomRow = new Panel(); 
		bottomRow.addContainerListener( this );
		gbc.weightx = 1.0;
		addGB( bottomRow, new Button("0"), 0, 0 );
		gbc.weightx = 0.33;
		addGB( bottomRow, new Button("."), 1, 0 );
		gbc.weightx = 1.0;
		addGB( bottomRow, new Button("="), 2, 0 );
		gbc.gridwidth = 4;
		addGB( this, bottomRow, 0, 5 );