在Java中处理事件的办法最原始的方法如下
一.使用内部类
一个个设置Button然后创建一个内部类, 用ActionPerformed来实现按钮事件内容;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
public class Test {
public static void main(String[] args) {
EventQueue.invokeLater(()->{
JFrame myFrame = new ButtonFrame();
myFrame.setTitle("Recluse");
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setVisible(true);
});
}
}
class ButtonFrame extends JFrame{
private JPanel myWindow;
private static final int DEFAULT_WIDTH = 500;
private static final int DEFAULT_HEIGHT = 500;
public ButtonFrame(){
this.setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
JButton greenButton = new JButton("Green");
JButton yellowButton = new JButton("Yellow");
JButton redButton = new JButton("Red");
myWindow = new JPanel();
myWindow.add(greenButton);
myWindow.add(yellowButton);
myWindow.add(redButton);
this.add(myWindow);
Colorbutton greenbut = new Colorbutton(Color.green);
Colorbutton yellowbut = new Colorbutton(Color.yellow);
Colorbutton redbut = new Colorbutton(Color.red);
greenButton.addActionListener(greenbut);
yellowButton.addActionListener(yellowbut);
redButton.addActionListener(redbut);
}
private class Colorbutton implements ActionListener{
private Color backgroundColor;
public Colorbutton(Color c){
backgroundColor = c;
}
@Override
public void actionPerformed(ActionEvent e) {
myWindow.setBackground(backgroundColor);
}
}
}
二. 使用lambda表达式
但是这个方法比较繁琐,需要创建一个内部类, 其实有更简单的方法, 比如使用lambda表达式, 而不需要创建内部类, 我们可以把创建按钮和响应的部分放到一个方法里面, 如下:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
public class Test {
public static void main(String[] args) {
EventQueue.invokeLater(()->{
JFrame myFrame = new ButtonFrame();
myFrame.setTitle("Recluse");
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setVisible(true);
});
}
}
class ButtonFrame extends JFrame{
private JPanel myWindow;
private static final int DEFAULT_WIDTH = 500;
private static final int DEFAULT_HEIGHT = 500;
public ButtonFrame(){
myWindow = new JPanel();
this.setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
myWindow.setPreferredSize(new Dimension(500,100));
this.add(myWindow,BorderLayout.SOUTH);
makeButton("Green", Color.GREEN);
makeButton("Yellow",Color.YELLOW);
makeButton("Blue",Color.BLUE);
}
public void makeButton(String name, Color backgroundcolor){
JButton aButton = new JButton(name);
myWindow.add(aButton);
aButton.addActionListener(event->{myWindow.setBackground(backgroundcolor);});
}
}
三. 使用匿名对象
当然使用lambda表达式肯定不是非用不可的, 以前的方法是使用匿名对象, 也是可以的:
aButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
myWindow.setBackground(backgroundcolor);
}
});
四. 自身框架做事件源容器
其实可以直接用JFrame本身做事件源容器, 使ButtonFrame支持ActionListener接口, 需要把按钮定义在属性中, 然后在构造函数中添加的监听器中直接使用this 像这样:greenButton.addActionListener(this);
然后在自己类中实现ActionPerformed方法, 使用Object source = e.getSource();
获取到事件的更新源, 然后用判断语句来判断是哪个按钮的事件依次执行事件操作;
class ButtonFrame extends JFrame implements ActionListener{
private JPanel myWindow;
private static final int DEFAULT_WIDTH = 500;
private static final int DEFAULT_HEIGHT = 500;
JButton greenButton = new JButton("Green");
JButton redButton = new JButton("Red");
JButton blueButton = new JButton("Blue");
public ButtonFrame(){
myWindow = new JPanel();
this.setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
myWindow.setPreferredSize(new Dimension(500,100));
greenButton.addActionListener(this);
redButton.addActionListener(this);
blueButton.addActionListener(this);
myWindow.add(greenButton);
myWindow.add(redButton);
myWindow.add(blueButton);
this.add(myWindow,BorderLayout.SOUTH);
}
@Override
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if(source == greenButton) myWindow.setBackground(Color.GREEN);
else if(source == redButton) myWindow.setBackground(Color.RED);
else if(source == blueButton) myWindow.setBackground(Color.BLUE);
}
}
五.使用EventHandler指定事件监听器
使用这样的方法EventHandler.create(ActionListener.class, frame, "loadData")
创造一个ActionListener, 这种方法用lambda表达式就等同于event->frame.localData();
但是这个方法我还不会用呜呜呜