Click here to Skip to main content
15,846,144 members
Please Sign up or sign in to vote.
2.63/5 (5 votes)
See more:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Awt implements ActionListener
{
	JFrame f;
	JButton b1,b2,b3,b4,b5;
	JTextField t1,t2,t3;
	JLabel l,l1;
	Awt()
	{
		f=new JFrame("Listener");
		t1=new JTextField("             ");
		t2=new JTextField("             ");
		t3=new JTextField("             ");
		b1=new JButton("Add");
		b2=new JButton("Sub");
		b3=new JButton("Mul");
		b4=new JButton("Div");
		l=new JLabel();
		l1=new JLabel();
	}
	public void awt1()
	{
		f.setLayout(new GridLayout(3,2));
		f.setVisible(true);
		f.add(t1);
		f.add(t2);
		f.add(t3);
		f.add(b1);
 		f.add(b2);
		f.add(b3);
		f.add(b4);
		f.add(l);
		f.add(l1);
		b1.addActionListener(this);
		b2.addActionListener(this);
		b3.addActionListener(this);
		b4.addActionListener(this);
		f.pack();
	}
	public void actionPerformed(ActionEvent e)
	{
		String s=new String(e.getActionCommand());
		l.setText(s);
		if((s).equals("Add"))
		{
			int  a=Integer.parseInt(t1.getText());
			int b=Integer.parseInt(t2.getText());
			Integer c=a+b;
                        t3.setText(c.toString());

		}
		else if((s).equals("Sub"))
		{
			int a=Integer.parseInt(t1.getText());
			int b=Integer.parseInt(t2.getText());
			Integer c=a-b;
			t3.setText(c.toString());
	
		}
		else if((s).equals("Mul"))
		{
			int a=Integer.parseInt(t1.getText());
			int b=Integer.parseInt(t2.getText());
			Integer c=a*b;
			t3.setText(c.toString());
		}
	}
	
	public static void main(String args[])
	{
		Awt a=new Awt();
		a.awt1();
	}
}


Hi..
This code runs successfully but when I entered input and press a button a number format exception occurred. I don't know where I made a mistake, please spot that logical error in this code.
Please help me.
Posted
Updated 22-Nov-10 1:25am
v4
Comments
Abhinav S 20-Nov-10 1:45am    
Corrected pre tags.
Richard MacCutchan 20-Nov-10 12:28pm    
I have no idea why this question was 1-voted, but I have given it a 5 to compensate. There really are some silly people about.
Nagy Vilmos 22-Nov-10 6:25am    
Fixed typing - all caps is considered rude.
Member 9355851 13-Mar-13 13:24pm    
Bro,you havent called the actionPerformed() function!!!how will it perform those operations without the logic!!#noob
Member 9124911 14-Dec-17 4:53am    
Blows up because there are spaces in the string returned by getText() ... simply adding .trim() to each getText() fixes that. Plus there's no handing for division. But it's a nice, simple swing app.

The strings returned by getText() contain trailing spaces which parseInt() objects to. Change your code to something like:
String stemp = t1.getText().trim();
int  a=Integer.parseInt(stemp);
 
Share this answer
 
As a minor advice. Why have you initialized your text fields with so many space characters? If you want the text fields to be empty, the just initialize them like this:

Java
JTextField myTextField=new JTextField("");


Thus the compiler knows that text fields must be empty. And by doing so, you won't be needing trim() method when parsing the value from the text fields.

And also in the actionPerformed() method with getting the name of the event, rather i would just use:
Java
   if(e.getSource()==bt1){

     //do something -> add, mul etc.
}


Other than that, the code seems fine.

Best regards.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900