The java.awt.event.ActionListener interface is responsible for processing events java.awt.event.ActionEvent action generated by the drive buttons (Button), selection items (MenuItem) in suspended (drop-down) or independent menus (popup) at Pressing the “Enter” in inboxes (TextField) and double-click boxes list (List). Its implementation requires the inclusion of the actionPerformed method in these classes.
As a complement, the ActionEvent event has the following specific methods associated with:
Method | Description |
getActionCommand() | Returns the string associated with this component |
getModifiers() | Obtain the modifiers used in the instant the generation of this event. |
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class JavaExampleHello2JApplet extends JApplet implements ActionListener
{
JLabel LblGrtng = new JLabel("Hi Welcome...!Who are you?");
Font FntOne = new Font("Times New Roman", Font.BOLD, 36);
Font FntTwo = new Font("Times New Roman", Font.ITALIC, 48);
JTextField TxtAns = new JTextField(10);
JButton BtnClck = new JButton("Click Here");
JLabel LblPrsnlGrtng = new JLabel(" ");
Container Cntnr = getContentPane();
public void init()
{
LblGrtng.setFont(FntOne);
LblPrsnlGrtng.setFont(FntTwo);
Cntnr.add(LblGrtng);
Cntnr.add(TxtAns);
Cntnr.add(BtnClck);
Cntnr.setLayout(new FlowLayout());
Cntnr.setBackground(Color.CYAN);
BtnClck.addActionListener(this);
TxtAns.addActionListener(this);
}
public void actionPerformed(ActionEvent ee)
{
String Nme = TxtAns.getText();
Cntnr.remove(LblGrtng);
Cntnr.remove(BtnClck);
Cntnr.remove(TxtAns);
LblPrsnlGrtng.setText("Hello, " + Nme + "! ");
Cntnr.add(LblPrsnlGrtng);
Cntnr.setBackground(Color.WHITE);
validate();
}
}
/*<applet code= JavaExampleHello2JApplet.class Height=300 width=240></applet>*/