JAVA ActionListener
Action Listener
Action Listener :- Listener is an interface in java that listens some event generated by component ActionEvent fires on
---------------------------------------------------------------------------------------------------------------------
when user click on a button
- press enter in a TextField
- choose a mentItem
*************** ActionListener ***********************/
- ActionListener
- void actioonPerformance
- addActionListener()
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
How to Write an ActionListener
Syntax:...................
class Example implements ActionListener
{
public example() //constructor
{
component.addActionListener(this);
}
public void actionPerformed(ActuionEvent e)
{
// write your code
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
copy code perform event on laptop...
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class MyFrame extends JFrame implements ActionListener
{
Container c;
JButton button;
public MyFrame()
{
c=this.getContentPane();
c.setLayout(null);
button=new JButton("Click Me");
button.setBounds(100,100,100,30);
c.add(button);
button.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
String str=button.getText();
button.setText(str.toUpperCase());
}
}
public class ActionEvent1
{
public static void main(String[] args)
{
MyFrame f=new MyFrame();
f.setTitle("Action Event ");
f.setBounds(100,100,400,400);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
Comments
Post a Comment