
use of this keyword in addActionListener(this) in java applets program or in java Frames program.

The given code allowed the user to enter any number in the first textbox. Every time, when the user enters the number and click the button, its sum will get displayed in another textbox.
import javax.swing.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class SApplet extends Applet implements ActionListener {
TextField input,output;
Label label1,label2;
Button b1;
JLabel lbl;
int num, sum = 0;
public void init(){
label1 = new Label("please enter number : ");
add(label1);
label1.setBackground(Color.yellow);
label1.setForeground(Color.magenta);
input = new TextField(5);
add(input);
label2 = new Label("Sum : ");
add(label2);
label2.setBackground(Color.yellow);
label2.setForeground(Color.magenta);
output = new TextField(20);
add(output);
input.addActionListener( this );
b1 = new Button("Add");
add(b1);
b1.addActionListener(this);
lbl = new JLabel("This is the Swing Applet Example. ");
add(lbl);
setBackground(Color.yellow);
output.addActionListener(this );
}
public void actionPerformed(ActionEvent ae){
try{
num = Integer.parseInt(ae.getActionCommand());
num = Integer.parseInt(input.getText());
sum = sum+num;
input.setText("");
output.setText(Integer.toString(sum));
lbl.setForeground(Color.blue);
lbl.setText("Output of the second Text Box : " + output.getText());
}
catch(NumberFormatException e){
lbl.setForeground(Color.red);
lbl.setText("Invalid Entry!");
}
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.