Sunday, December 9, 2007

Events

Event-Driven Programs

import java.awt.*;

public class Xxxx extends Frame
{
Xxxx()
{
setLayout(new FlowLayout());
Button btn = new Button("Push Me");
add(btn);
setSize(300, 300);
}

public void paint(Graphics g)
{
g.setColor(Color.blue);
g.fillOval(100, 100, 100, 100);
}

public static void main(String[] args)
{
(new Xxxx()).setVisible(true);
}
}

Threads

I was eight years old. I was waiting my turn to get my hair cut, and I was reading a Superman comic book. The Man of Steel was entertaining some kids at an orphanage by playing ping-pong against himself. He would serve the ball, and then run at super-speed to the other side of the table to return the ball. Then he would run back to the original side, and so on. Each time he changed sides, he ran so fast that nobody could see him, and he ended up exactly where he had been before. This created the illusion of two identical Supermen.

Computers do something similar. They create the illusion of doing several things simultaneously by rapidly switching from one task to another, many times each second, like Superman running from one side of the ping-pong table to the other. The individual tasks are called threads.

Action Listeners and Action Events

Java uses many types of events. The simplest is the action event, which is used by buttons and several other components to indicate that simple user input activity has occurred. The other event types are slightly more complicated than action events, but they are used in analogous ways. The nice thing about Java's event mechanism is that once you've learned how to handle one kind of event, it's easy to handle the other kinds.

Action Listeners and Action Events

Java uses many types of events. The simplest is the action event, which is used by buttons and several other components to indicate that simple user input activity has occurred. The other event types are slightly more complicated than action events, but they are used in analogous ways. The nice thing about Java's event mechanism is that once you've learned how to handle one kind of event, it's easy to handle the other kinds.

import java.awt.event.*;

class SimpleActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.out.println("The button was pushed.");
}
}

The following code creates a button that uses an instance of SimpleActionListener as its action listener:

import java.awt.*;

public class UsesListener extends Frame
{
UsesListener ()
{
setLayout(new FlowLayout());
Button btn = new Button("Push Me");
SimpleActionListener sal = new SimpleActionListener();
btn.addActionListener(sal);
add(btn);
setSize(300, 100);
}


public static void main(String[] args)
{
(new UsesListener ()).setVisible(true);
}
}

Events from other Components

  • Buttons

  • Check boxes

  • Choices

  • Labels

  • Menus and menu items

  • Scrollbars

  • Text areas

  • Text fields

Now you will learn how to respond to user input activity on each type of component. You already know how to respond to buttons. Labels do not send events. In the rest of this chapter, you will learn how to detect events from the other component types.




No comments: