Vous êtes sur la page 1sur 12

Event and Listener (Java Event Handling)

Changing the state of an object is known as an event. For example, click on


button, dragging mouse etc. The java.awt.event package provides many event
classes and Listener interfaces for event handling.

Java Event classes and Listener interfaces

Event Classes Listener Interfaces

ActionEvent ActionListener

MouseEvent MouseListener and MouseMotionListener

MouseWheelEvent MouseWheelListener

KeyEvent KeyListener

ItemEvent ItemListener

TextEvent TextListener

AdjustmentEvent AdjustmentListener

WindowEvent WindowListener

ComponentEvent ComponentListener

ContainerEvent ContainerListener

FocusEvent FocusListener
Steps to perform Event Handling
Following steps are required to perform event handling:

1. Register the component with the Listener

Registration Methods

For registering the component with the Listener, many classes provide the
registration methods. For example:

o Button
o public void addActionListener(ActionListener a){}
o MenuItem
o public void addActionListener(ActionListener a){}
o TextField
o public void addActionListener(ActionListener a){}
o public void addTextListener(TextListener a){}
o TextArea
o public void addTextListener(TextListener a){}
o Checkbox
o public void addItemListener(ItemListener a){}
o Choice
o public void addItemListener(ItemListener a){}
o List
o public void addActionListener(ActionListener a){}
o public void addItemListener(ItemListener a){}

Java Event Handling Code


We can put the event handling code into one of the following places:

1. Within class
2. Other class
3. Anonymous class
Java event handling by implementing ActionListener
import java.awt.*;
import java.awt.event.*;
class AEvent extends Frame implements ActionListener{
TextField tf;
AEvent(){
//create components
tf=new TextField();
tf.setBounds(60,50,170,20);
Button b=new Button("click me");
b.setBounds(100,120,80,30);
//register listener
b.addActionListener(this);//passing current instance
//add components and set size, layout and visibility
add(b);add(tf);
setSize(300,300);
setLayout(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e){
tf.setText("Welcome");
}
public static void main(String args[]){
new AEvent();
}
}

public void setBounds(int xaxis, int yaxis, int width, int height); have been used in the above
exam ple that sets the position of the component it may be button, textfield etc.
Java event handling by anonymous class
import java.awt.*;
import java.awt.event.*;
class AEvent3 extends Frame{
TextField tf;
AEvent3(){
tf=new TextField();
tf.setBounds(60,50,170,20);
Button b=new Button("click me");
b.setBounds(50,120,80,30);

b.addActionListener(new ActionListener(){
public void actionPerformed(){
tf.setText("hello");
}
});
add(b);add(tf);
setSize(300,300);
setLayout(null);
setVisible(true);
}
public static void main(String args[]){
new AEvent3();
}
}
Java AWT (Abstract Window Toolkit)

Java AWT is an API to develop GUI or window-based applications in java.


Java AWT components are platform-dependent i.e. components are displayed
according to the view of operating system. AWT is heavyweight i.e. its components
are using the resources of OS.
The java.awt package provides classes for AWT api such
as TextField, Label, TextArea,RadioButton, CheckBox, Choice, List etc.

The Hierarchy of AWT is


Container
The Container is a component in AWT that can contain another components
like buttons, text fields, labels etc. The classes that extends Container class are
known as container such as Frame, Dialog and Panel.
Window
The window is the container that have no borders and menu bars. You must use
frame, dialog or another window for creating a window.
Panel
The Panel is the container that doesn't contain title bar and menu bars. It can have
other components like button, text field etc.
Frame
The Frame is the container that contain title bar and can have menu bars. It can
have other components like button, text field etc.
Useful Methods of Component class

Method Description

public void add(Component c) inserts a component on this component.

public void setSize(int width,int height) sets the size (width and height) of the
component.

public void setLayout(LayoutManager defines the layout manager for the


m) component.

public void setVisible(boolean status) changes the visibility of the component,


by default false.
To create simple awt example, you need a frame.
There are two ways to create a frame in AWT.
• By extending Frame class (inheritance)
• By creating the object of Frame class (association)

Example 1

import java.awt.*;
class First extends Frame{
First(){
Button b=new Button("click me");
b.setBounds(230,120,100,70);// setting button position
add(b);//adding button into frame
setSize(400,350);//frame size 300 width and 300 height
setLayout(null);//no layout manager
setVisible(true);//now frame will be visible, by default not visible
}
public static void main(String args[]){
First f=new First();
}}

AWT Window

import java.awt.*;
import java.awt.event.*;

public class AwtContainerDemo {


private Frame mainFrame;
private Label headerLabel;
private Label statusLabel;
private Panel controlPanel;
private Label msglabel;

public AwtContainerDemo(){
prepareGUI();
}

public static void main(String[] args){


AwtContainerDemo awtContainerDemo = new AwtContainerDemo();
awtContainerDemo.showFrameDemo();
}

private void prepareGUI(){


mainFrame = new Frame("Java AWT Examples");
mainFrame.setSize(400,400);
mainFrame.setLayout(new GridLayout(3, 1));
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent){
System.exit(0);
}
});
headerLabel = new Label();
headerLabel.setAlignment(Label.CENTER);
statusLabel = new Label();
statusLabel.setAlignment(Label.CENTER);
statusLabel.setSize(350,100);

msglabel = new Label();


msglabel.setAlignment(Label.CENTER);
msglabel.setText("Welcome to TutorialsPoint AWT Tutorial.");

controlPanel = new Panel();


controlPanel.setLayout(new FlowLayout());

mainFrame.add(headerLabel);
mainFrame.add(controlPanel);
mainFrame.add(statusLabel);
mainFrame.setVisible(true);
}

private void showWindowDemo(){


headerLabel.setText("Container in action: Window");
final MessageWindow window =
new MessageWindow(mainFrame,
"Welcome to TutorialsPoint AWT Tutorial.");

Button okButton = new Button("Open a Window");


okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
window.setVisible(true);
statusLabel.setText("A Window shown to the user.");
}
});
controlPanel.add(okButton);
mainFrame.setVisible(true);
}

class MessageWindow extends Window{


private String message;

public MessageWindow(Frame parent, String message) {


super(parent);
this.message = message;
setSize(300, 300);
setLocationRelativeTo(parent);
setBackground(Color.gray);
}

public void paint(Graphics g) {


super.paint(g);
g.drawRect(0,0,getSize().width - 1,getSize().height - 1);
g.drawString(message,50,150);
}
}
}
Java AWT Canvas

The Canvas control represents a blank rectangular area


where the application can draw or trap input events from
the user.
It inherits the Component class.

AWT Canvas Class Declaration is

public class Canvas extends Component implements Accessible

import java.awt.*;
public class CanvasExample
{
public CanvasExample()
{
Frame f= new Frame("Canvas Example");
f.add(new MyCanvas());
f.setLayout(null);
f.setSize(400, 400);
f.setVisible(true);
}
public static void main(String args[])
{
new CanvasExample();
}
}
class MyCanvas extends Canvas
{
public MyCanvas() {
setBackground (Color.PINK);
setSize(300, 200);
}
public void paint(Graphics g)
{
g.setColor(Color.blue);
g.fillOval(75, 60, 150, 60);
}
}
Awt Menu

import java.awt.*;
class MenuExample
{
MenuExample(){
Frame f= new Frame("Menu and MenuItem Example");
MenuBar mb=new MenuBar();
Menu menu=new Menu("Menu");
Menu submenu=new Menu("Sub Menu");
MenuItem i1=new MenuItem("File");
MenuItem i2=new MenuItem("Edit");
MenuItem i3=new MenuItem("View");
MenuItem i4=new MenuItem("PrintPreview");
MenuItem i5=new MenuItem("Close");
menu.add(i1); menu.add(i2); menu.add(i3);
submenu.add(i4);
submenu.add(i5); menu.add(submenu); mb.add(menu);
f.setMenuBar(mb);
f.setSize(400,400); f.setLayout(null); f.setVisible(true); }
public static void main(String args[])
{ new MenuExample(); } }

Awt Panel
import java.awt.*;
public class PanelExample {
PanelExample()
{
Frame f= new Frame("Panel Example");
Panel panel=new Panel();
panel.setBounds(40,80,200,200);
panel.setBackground(Color.gray);
Button b1=new Button("Button 1");
b1.setBounds(50,100,80,30);
b1.setBackground(Color.yellow);
Button b2=new Button("Button 2");
b2.setBounds(100,100,80,30);
b2.setBackground(Color.green);
panel.add(b1); panel.add(b2);
f.add(panel);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[])
{
new PanelExample();
}
}

Vous aimerez peut-être aussi