we were asked to create a calculator in java applet......My codes are working except for one thing....whenever I click the clear button, it only erases the first,second and the total sum.....it does not clear the total difference, total product and total quotient....please help me what to do?......

here is the code that I made.....

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Button extends JFrame implements ActionListener
{

	private static int width=600;
	private static int height=300;
	private JLabel[] labelJL = new JLabel[6];
	private JTextField[] textJT = new JTextField[6];
	private JButton[] AddClearExit = new JButton[6];
	public Button()
	{
		Container pane = getContentPane();
		setBackground(Color.blue);
		setTitle("USING BUTTON EVENT");
		setSize(width,height);
		setLayout(null);


		labelJL[0] = new JLabel("FIRST NUMBER",0);
		labelJL[0].setLocation(220,220);
		labelJL[0].setSize(100,20);
		pane.add(labelJL[0]);

		textJT[0] = new JTextField();
		textJT[0].setLocation(330,220);
		textJT[0].setSize(200,20);
		pane.add(textJT[0]);

		labelJL[1] = new JLabel("SECOND NUMBER",0);
		labelJL[1].setLocation(550,220);
		labelJL[1].setSize(100,20);
		pane.add(labelJL[1]);

		textJT[1] = new JTextField();
		textJT[1].setLocation(660,220);
		textJT[1].setSize(200,20);
		pane.add(textJT[1]);

		labelJL[2] = new JLabel("Total Sum",0);
		labelJL[2].setLocation(550,280);
		labelJL[2].setSize(100,20);
		pane.add(labelJL[2]);

		textJT[2] = new JTextField();
		textJT[2].setLocation(660,280);
		textJT[2].setSize(200,20);
		textJT[2].setEditable(false);
		pane.add(textJT[2]);

		labelJL[3] = new JLabel("Total Difference",0);
		labelJL[3].setLocation(550,320);
		labelJL[3].setSize(100,20);
		pane.add(labelJL[3]);

		textJT[3] = new JTextField();
		textJT[3].setLocation(660,320);
		textJT[3].setSize(200,20);
		textJT[3].setEditable(false);
		pane.add(textJT[3]);

		labelJL[4] = new JLabel("Total Product",0);
		labelJL[4].setLocation(550,360);
		labelJL[4].setSize(100,20);
		pane.add(labelJL[4]);

		textJT[4] = new JTextField();
		textJT[4].setLocation(660,360);
		textJT[4].setSize(200,20);
		textJT[4].setEditable(false);
		pane.add(textJT[4]);

		labelJL[5] = new JLabel("Total Quotient",0);
		labelJL[5].setLocation(550,399);
		labelJL[5].setSize(100,20);
		pane.add(labelJL[5]);

		textJT[5] = new JTextField();
		textJT[5].setLocation(660,399);
		textJT[5].setSize(200,20);
		textJT[5].setEditable(false);
		pane.add(textJT[5]);

		AddClearExit[0] = new JButton("Sum");
		AddClearExit[0].setLocation(370,280);
		AddClearExit[0].setSize(150,20);
		AddClearExit[0].addActionListener(this);
		pane.add(AddClearExit[0]);

		AddClearExit[1] = new JButton("Difference");
		AddClearExit[1].setLocation(370,320);
		AddClearExit[1].setSize(150,20);
		AddClearExit[1].addActionListener(this);
		pane.add(AddClearExit[1]);

		AddClearExit[2] = new JButton("Product");
		AddClearExit[2].setLocation(370,360);
		AddClearExit[2].setSize(150,20);
		AddClearExit[2].addActionListener(this);
		pane.add(AddClearExit[2]);

		AddClearExit[3] = new JButton("Quotient");
		AddClearExit[3].setLocation(370,399);
		AddClearExit[3].setSize(150,20);
		AddClearExit[3].addActionListener(this);
		pane.add(AddClearExit[3]);

		AddClearExit[4] = new JButton("Clear");
		AddClearExit[4].setLocation(450,470);
		AddClearExit[4].setSize(150,20);
		AddClearExit[4].addActionListener(this);
		pane.add(AddClearExit[4]);

		AddClearExit[5] = new JButton("Exit");
		AddClearExit[5].setLocation(629,470);
		AddClearExit[5].setSize(150,20);
		AddClearExit[5].addActionListener(this);
		pane.add(AddClearExit[5]);
		setVisible(true);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
	}
		public void actionPerformed(ActionEvent ae)
		{
			String temp = "";
			long fn=0, sn=0;



	try
	{
		fn=Long.parseLong(textJT[0].getText());
		sn=Long.parseLong(textJT[1].getText());
	}
		catch (Exception ne)
		{
		for(int x=0;x<3;textJT[x].setText(temp),x++);

		}

				
		if(ae.getActionCommand().equals("Sum"))
			{temp+=sum(fn,sn);
					textJT[2].setText(temp);}
				else if(ae.getActionCommand().equals("Difference"))
				{temp+=difference(fn,sn);
						textJT[3].setText(temp);}
					else if(ae.getActionCommand().equals("Product"))
					{temp+=product(fn,sn);
							textJT[4].setText(temp);}
						else if(ae.getActionCommand().equals("Quotient"))
						{temp+=quotient(fn,sn);
							textJT[5].setText(temp);}
							
						 if(ae.getActionCommand().equals("Clear"))

				for(int x=0;x<3;textJT[x].setText(temp),x++);
				if(ae.getActionCommand().equals("Exit"))
					dispose();
				


		}
	public long sum(long x,long y)
	{
		return x+y;

	}
	public long difference(long x,long y)
	{
		return x-y;
	}
	public long product(long x,long y)
	{
		return x*y;
	}
	public long quotient(long x,long y)
	{
		return x/y;
	}
	public static void main (String args[])
	{
		new Button();

		}
}

textJT array contains 6 text fields, but the clear code (line 157) only clears the first 3

textJT array contains 6 text fields, but the clear code (line 157) only clears the first 3

thanks a lot.....how can I set a color background and a text that tells "My Calculator" i am trying to declare them with the set title but is not working.......

No time to study all your code now, but since you extend JFrame you should be able to use setTitle and setBackground. Because the JFrame is filled by a ContentPane, you need to set the background color of the content pane, not the JFrame.

No time to study all your code now, but since you extend JFrame you should be able to use setTitle and setBackground. Because the JFrame is filled by a ContentPane, you need to set the background color of the content pane, not the JFrame.

I tried to put it under the container pane but it is still not working................

here is the code........

Container pane = getContentPane();
		setBackground(Color.blue);
		setTitle("USING BUTTON EVENT");
		setSize(width,height);
		setLayout(null);

is this correct????

No, your indentation means nothing to the Java compiler, so lines 2-5 apply to "this" (the current Button, which extends JFrame) not to pane.
You need pane.setBackground(Color.blue);

No, your indentation means nothing to the Java compiler, so lines 2-5 apply to "this" (the current Button, which extends JFrame) not to pane.
You need pane.setBackground(Color.blue);

thanks a lot............you are really a great help to me!.........really......hontouni arigatou!!!!!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.