Applet programming

Addition of two numbers using Applet

Page 1 of 1

4 Replies - 2781 Views - Last Post: 22 August 2008 - 09:15 PM Rate Topic: -----

#1 mother_success  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 06-March 08

Applet programming

Posted 06 March 2008 - 09:23 AM

Hi,
Plz guide me in getting output of the addition pgm using applet wen clicked on "Add" button ,the output should be displayed in the textbox.I have attached the code to the extent that i tried but dint get the output.

Regards,
Sudha

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/*<applet code="addition" width=400 height=400>
</applet>
*/
public class addition extends Applet implements ActionListener
{
TextField t1,t2,t3;
Button b1;
String ans=" ";
public void init()
{
setBackground(Color.black);
setForeground(Color.red);
Label l1=new Label("Enter The First Number ");
Label l2=new Label("Enter The Second Number ");
t1=new TextField(6);
t2=new TextField(6);
t3=new TextField(6);
b1=new Button("Add");
add(l1);
add(t1);
add(l2);
add(t2);
add(b1);
add(t3);
t1.addActionListener(this);
t2.addActionListener(this);
t3.addActionListener(this);
b1.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
String s=ae.getActionCommand();
if(s.equals("Add"))
{
ans=t3.setText(s3);
}
repaint();
}
public void paint(Graphics g)
{
int a=0,b=0,c=0;
String s1,s2,s3;
try
{
s1=t1.getText();
a=Integer.parseInt(s1);
s2=t2.getText();
b=Integer.parseInt(s2);
}
catch(Exception e)
{
c=a+b;
s3=String.valueof(c);
}
}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Applet programming

#2 letthecolorsrumble  Icon User is offline

  • Student of The Sun
  • member icon

Reputation: 27
  • View blog
  • Posts: 555
  • Joined: 07-November 07

Re: Applet programming

Posted 06 March 2008 - 09:42 AM

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/*<applet code="addition" width=400 height=400>
</applet>
*/
public class Addition extends Applet implements ActionListener {
	TextField t1,t2,t3;
	Button b1;
	String ans="";
	
	public void init() {
		
		setBackground(Color.black);
		setForeground(Color.red);
		Label l1=new Label("Enter The First Number ");
		Label l2=new Label("Enter The Second Number ");
		t1=new TextField(6);
		t2=new TextField(6);
		t3=new TextField(6);
		b1=new Button("Add");
		add(l1);
		add(t1);
		add(l2);
		add(t2);
		add(b1);
		add(t3);
		t1.addActionListener(this);
		t2.addActionListener(this);
		t3.addActionListener(this);
		b1.addActionListener(this);
		
	}
	public void actionPerformed(ActionEvent ae) {
		String s=ae.getActionCommand();
		int a=0,b=0,c=0;
		String s1,s2,s3;					
		
		if(s.equals("Add")) {
			s1=t1.getText();
			a=Integer.parseInt(s1);
			s2=t2.getText();
			b=Integer.parseInt(s2);
			ans = String.valueOf( a+b );	
			t3.setText(ans);  //changed
		}
		repaint();
	}
	
	public void paint(Graphics g) {		
		
	}
}



This should make it work...

Welcome to </dream.in.code> Have a great time here. :)

This post has been edited by letthecolorsrumble: 06 March 2008 - 09:42 AM

Was This Post Helpful? 2
  • +
  • -

#3 mother_success  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 06-March 08

Re: Applet programming

Posted 07 March 2008 - 03:34 AM

Hi,
Thank you very much..The Applet programme worked out well.got output.
Was This Post Helpful? 0
  • +
  • -

#4 kkelageri  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 21-August 08

Re: Applet programming

Posted 22 August 2008 - 12:01 AM

hi sudha,
Try this code it will work fine,
package simplecalc;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/*<applet code="addition" width=400 height=400>
</applet>
*/
public class addition extends Applet implements ActionListener
{
TextField t1,t2,t3;
Button b1;
String ans=" ";
public void init()
{
setBackground(Color.black);
setForeground(Color.red);
Label l1=new Label("Enter The First Number ");
Label l2=new Label("Enter The Second Number ");
t1=new TextField(6);
t2=new TextField(6);
t3=new TextField(6);
b1=new Button("Add");
add(l1);
add(t1);
add(l2);
add(t2);
add(b1);
add(t3);
t1.addActionListener(this);
t2.addActionListener(this);
t3.addActionListener(this);
b1.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
int a=0,b=0,c=0;
String s1,s2,s3;
s1=t1.getText();
a=Integer.parseInt(s1);
s2=t2.getText();
b=Integer.parseInt(s2);
c=a+b;
s3=String.valueOf©;
String s=ae.getActionCommand();
if(s.equals("Add"))
{
t3.setText(s3);
}
}
}
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Applet programming

Posted 22 August 2008 - 09:15 PM

You don't have to call repaint() if you change the content of a JComponent like a JTextfield, it will do it for you so no need to repaint() twice
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1