GUI程序发展到现在,事件一直是其核心。
从以前的Win32程序,处理事件需要编写代码来检查事件队列,代码由switch来控制,这种方式编写的程序可读性很差。
后来的Visual Basic,.NET,将事件对程序员隐藏起来。
那么,我们Java中是如何处理事件的呢?
[b]Java对事件控制作了综合,实现了从事件源到事件监听器的传递。[/b]
事件源注册事件监听器对象,如下:
[i]eventSourceObject.addEventListener(eventListenerObject);[/i]
[b]
eg:
ActionListener listener = ...;
JButton button = new JButton("Ok");
button.addActionListener(listener);
[/b]
下面我们来举个例子,我们做个Frame,在上面放个面板(Panel),在Panel上再放个
button,点击button,弹出对话框。
这是一个非常简单的例子。
[b]方法一: 我们可以把事件监听器做成是Panel的私有内部类[/b]
[b]方法二:我们也可以把事件监听器做成是匿名类[/b]
我们再把这个案例扩充一下,我们可以给事件监听器传入参数,案例如下:
从以前的Win32程序,处理事件需要编写代码来检查事件队列,代码由switch来控制,这种方式编写的程序可读性很差。
后来的Visual Basic,.NET,将事件对程序员隐藏起来。
那么,我们Java中是如何处理事件的呢?
[b]Java对事件控制作了综合,实现了从事件源到事件监听器的传递。[/b]
事件源注册事件监听器对象,如下:
[i]eventSourceObject.addEventListener(eventListenerObject);[/i]
[b]
eg:
ActionListener listener = ...;
JButton button = new JButton("Ok");
button.addActionListener(listener);
[/b]
下面我们来举个例子,我们做个Frame,在上面放个面板(Panel),在Panel上再放个
button,点击button,弹出对话框。
这是一个非常简单的例子。
[b]方法一: 我们可以把事件监听器做成是Panel的私有内部类[/b]
package corejava2.event;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SimpleButton1 {
public static void main(String[] args) {
SimpleFrame1 frame = new SimpleFrame1();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
/**
* A frame with a button panel
*/
class SimpleFrame1 extends JFrame {
public static final int DEFAULT_WIDTH = 300;
public static final int DEFAULT_HEIGHT = 200;
public SimpleFrame1() {
setTitle("Simple Button Test");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
//add panel to frame
SimplePanel1 panel = new SimplePanel1();
add(panel);
}
}
/**
* A panel with one simple
*/
class SimplePanel1 extends JPanel {
public SimplePanel1() {
JButton simpleButton = new JButton("Simple Button");
add(simpleButton);
ButtonAction buttonAction = new ButtonAction();
simpleButton.addActionListener(buttonAction);
}
/**
* An action listener which is an inner class
*/
private class ButtonAction implements ActionListener {
public void actionPerformed(ActionEvent event) {
JOptionPane.showMessageDialog(null, "You clicked the button!");
}
}
}
[b]方法二:我们也可以把事件监听器做成是匿名类[/b]
package corejava2.event;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SimpleButton2 {
public static void main(String[] args) {
SimpleFrame2 frame = new SimpleFrame2();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
/**
* A frame with a button panel
*/
class SimpleFrame2 extends JFrame {
public static final int DEFAULT_WIDTH = 300;
public static final int DEFAULT_HEIGHT = 200;
public SimpleFrame2() {
setTitle("Simple Button Test");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
//add panel to frame
SimplePanel2 panel = new SimplePanel2();
add(panel);
}
}
/**
* A panel with one simple
*/
class SimplePanel2 extends JPanel {
public SimplePanel2() {
JButton simpleButton = new JButton("Simple Button");
add(simpleButton);
simpleButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
JOptionPane.showMessageDialog(null, "You clicked the button!");
}
});
}
}
我们再把这个案例扩充一下,我们可以给事件监听器传入参数,案例如下:
package corejava2.event;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ButtonTest {
public static void main(String[] args) {
ButtonFrame frame = new ButtonFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
/**
* A frame with a button panel
*/
class ButtonFrame extends JFrame {
public static final int DEFAULT_WIDTH = 300;
public static final int DEFAULT_HEIGHT = 200;
public ButtonFrame() {
setTitle("ButtonTest");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
// add panel to frame
ButtonPanel panel = new ButtonPanel();
add(panel);
}
}
/**
* A panel with three buttons.
*/
class ButtonPanel extends JPanel {
public ButtonPanel() {
// create buttons
JButton yellowButton = new JButton("Yellow");
JButton blueButton = new JButton("Blue");
JButton redButton = new JButton("Red");
// add buttons to panel
add(yellowButton);
add(blueButton);
add(redButton);
// create button actions
ColorAction yellowAction = new ColorAction(Color.YELLOW);
ColorAction blueAction = new ColorAction(Color.BLUE);
ColorAction redAction = new ColorAction(Color.RED);
// associate actions with buttons
yellowButton.addActionListener(yellowAction);
blueButton.addActionListener(blueAction);
redButton.addActionListener(redAction);
}
/**
* An action listener that sets the panel's background color.
*/
private class ColorAction implements ActionListener {
private Color backgroundColor;
public ColorAction(Color c) {
backgroundColor = c;
}
public void actionPerformed(ActionEvent event) {
String mColor = "";
setBackground(backgroundColor);
if (backgroundColor == Color.YELLOW)
mColor = "Yellow";
else if (backgroundColor == Color.BLUE)
mColor = "Blue";
else if (backgroundColor == Color.RED)
mColor = "Red";
JOptionPane.showMessageDialog(null, "You choosed " + mColor + " button");
}
}
}