Java事件初步

本文介绍了Java中事件处理的基本原理及实现方式,通过两个实例详细展示了如何使用内部类和匿名类作为事件监听器来响应按钮点击事件,并进一步扩展了示例以处理颜色选择事件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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]

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");
}

}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值