事件处理
一、事件处理基础
Java中,所有事件对象都最终派生于java.util.EventObject类。
不同事件源可以产生不同类别的事件对象,如按钮可以产生ActionEvent对象,而窗口可以产生WindowEvent对象。
监听器对象是一个实现了特定监听器接口的类的实例。
事件源是一个能够注册监听器对象并发送事件对象的对象。
当事件发生时,事件源将事件对象传递给所有的注册的事件监听器。
监听器对象利用事件对象中的信息决定对事件做如何处理。
例如一个事件源JButton按钮,上面添加了事件监听器ActionListener,当按钮按下,事件源JButton产生事件对象ActionEvent,并将该事件对象传给事件监听器ActionListener,事件监听器ActionListener收到该事件对象,调用其actionPerformed方法处理该事件。
import javax.swing.*;
import java.awt.*;
import java.io.*;
import javax.imageio.*;
import java.awt.event.*;
public class EventFrameTest01
{
public static void main(String[] args)
{
EventFrame sp = new EventFrame();
sp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //关闭窗口是操作
sp.setVisible(true); //显示组件
sp.setTitle("SimpleWindow"); //标题栏
}
}
class EventFrame extends JFrame
{
public EventFrame()
{
EventPanel mp = new EventPanel();
setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
//setResizable(false);
add(mp);
}
public static final int DEFAULT_WIDTH = 400;
public static final int DEFAULT_HEIGHT = 300;
}
class EventPanel extends JPanel
{
public EventPanel()
{
JButton yelloB = new JButton("黄色"); //事件源
JButton blueB = new JButton("蓝色");
JButton redB = new JButton("红色");
add(yelloB);
add(blueB);
add(redB);
//事件监听器
ActionListener yellowColorAL = new ColorActionListener(Color.YELLOW);
ActionListener blueColorAL = new ColorActionListener(Color.BLUE);
ActionListener redColorAL = new ColorActionListener(Color.RED);
yelloB.addActionListener(yellowColorAL);
blueB.addActionListener(blueColorAL);
redB.addActionListener(redColorAL);
}
class ColorActionListener implements ActionListener
{
public ColorActionListener(Color c)
{
backgroundColor = c;
}
//此处需要调用EventPanel的方法,所以ColorActionListener作为EventPanel的内部类
public void actionPerformed(ActionEvent event)
{
setBackground(backgroundColor);
//setForeground(backgroundColor);
}
private Color backgroundColor;
}
}
二、将组件变成事件监听器
上面对于监听器使用了内部类,如果不使用内部类,可以让组件实现监听器接口,并增加actionPerformed方法
class EventPanel extends JPanel implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
//.................
}
}
然后再将监听器注册到相应的事件源即可。
yelloB.addActionListener(this);
blueB.addActionListener(this);
redB.addActionListener(this);
此时三个按钮共用一个监听器,所以需要对接收到的事件对象在actionPerformed方法中区别处理。
import javax.swing.*;
import java.awt.*;
import java.io.*;
import javax.imageio.*;
import java.awt.event.*;
public class EventFrameTest02
{
public static void main(String[] args)
{
EventFrame sp = new EventFrame();
sp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //关闭窗口是操作
sp.setVisible(true); //显示组件
sp.setTitle("SimpleWindow"); //标题栏
}
}
class EventFrame extends JFrame
{
public EventFrame()
{
EventPanel mp = new EventPanel();
setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
//setResizable(false);
add(mp);
}
public static final int DEFAULT_WIDTH = 400;
public static final int DEFAULT_HEIGHT = 300;
}
class EventPanel extends JPanel implements ActionListener
{
public EventPanel()
{
Toolkit kit = Toolkit.getDefaultToolkit();
Image img = kit.getImage("2b.jpg");
Image imgc = img.getScaledInstance(15,15,5);
ImageIcon imagI = new ImageIcon(imgc);
yelloB = new JButton("黄色",imagI); //事件源
blueB = new JButton("蓝色");
redB = new JButton("红色");
add(yelloB);
add(blueB);
add(redB);
//组件事件监听器
yelloB.addActionListener(this);
blueB.addActionListener(this);
redB.addActionListener(this);
}
//此处需要调用EventPanel的方法,所以ColorActionListener作为EventPanel的内部类
public void actionPerformed(ActionEvent event)
{
Object source = event.getSource();
if(source == yelloB)
setBackground(Color.YELLOW);
if(source == blueB)
setBackground(Color.BLUE);
if(source == redB)
setBackground(Color.RED);
}
private JButton yelloB ; //事件源
private JButton blueB ;
private JButton redB ;
}
三、改变观感
有两种改变观感的方法:
1、到JDK的安装目录下的jre/lib/swing.properties文件中,有swing.defaultlaf配置项,修改成想要的观感即可,有些jdk中不存在swing.properties文件,直接新建一个即可。这种修改方法需要重启程序。
2、动态改变观感,调用静态的UIManager.setLookAndFeel方法设置观感,再调用SwingUtilities.updateComponentTreeUI来刷新全部组件观感。
对于系统中已经安装的观感,可以通过UIManager.LookAndFeelInfo[] infos = UIManager.getInstalledLookAndFeels()获得观感的类名。
import javax.swing.*;
import java.awt.*;
import java.io.*;
import javax.imageio.*;
import java.awt.event.*;
public class EventFrameTest01
{
public static void main(String[] args)
{
EventFrame sp = new EventFrame();
sp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //关闭窗口是操作
sp.setVisible(true); //显示组件
sp.setTitle("SimpleWindow"); //标题栏
//所有已安装的观感
UIManager.LookAndFeelInfo[] infos = UIManager.getInstalledLookAndFeels();
for(int i=0;i<infos.length;i++)
{
System.out.println(infos[i].getClassName());
}
/*System.out.println(UIManager.getSystemLookAndFeelClassName());*/
}
}
class EventFrame extends JFrame
{
public EventFrame()
{
EventPanel mp = new EventPanel();
setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
//setResizable(false);
add(mp);
}
public static final int DEFAULT_WIDTH = 400;
public static final int DEFAULT_HEIGHT = 300;
}
class EventPanel extends JPanel
{
public EventPanel()
{
Toolkit kit = Toolkit.getDefaultToolkit();
Image img = kit.getImage("2b.jpg");
Image imgc = img.getScaledInstance(15,15,5);
ImageIcon imagI = new ImageIcon(imgc);
JButton yelloB = new JButton("黄色",imagI); //事件源
JButton blueB = new JButton("蓝色");
JButton redB = new JButton("红色");
JButton ui1 = new JButton("样式一");
JButton ui2 = new JButton("样式二");
add(yelloB);
add(blueB);
add(redB);
add(ui1);
add(ui2);
//事件监听器
ActionListener yellowColorAL = new ColorActionListener(Color.YELLOW);
ActionListener blueColorAL = new ColorActionListener(Color.BLUE);
ActionListener redColorAL = new ColorActionListener(Color.RED);
yelloB.addActionListener(yellowColorAL);
blueB.addActionListener(blueColorAL);
redB.addActionListener(redColorAL);
ActionListener uiActL1 = new UIActionListener("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
ActionListener uiActL2 = new UIActionListener("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
ui1.addActionListener(uiActL1);
ui2.addActionListener(uiActL2);
}
class ColorActionListener implements ActionListener
{
public ColorActionListener(Color c)
{
backgroundColor = c;
}
//此处需要调用EventPanel的方法,所以ColorActionListener作为EventPanel的内部类
public void actionPerformed(ActionEvent event)
{
setBackground(backgroundColor);
}
private Color backgroundColor;
}
class UIActionListener implements ActionListener
{
public UIActionListener(String uiStyle)
{
this.uiStyle=uiStyle;
}
public void actionPerformed(ActionEvent event)
{
try
{
UIManager.setLookAndFeel(uiStyle);
SwingUtilities.updateComponentTreeUI(EventPanel.this);
}
catch(Exception e)
{
e.printStackTrace();
}
}
private String uiStyle;
}
}
运行结果: