有没有办法将 actionListener 类移动到独立类?
我使用 Java 和 MVC 设计模式做了一个例子。我有 3 个更改背景颜色的按钮。
这是模型
public class ChangeFontColorApplicationModel {
public ChangeFontColorApplicationModel(){}
}
看法
import java.awt.event.*;
import javax.swing.*;
public class ChangeFontColorApplicationView extends JFrame{
private JButton changeToYellowButton = new JButton("Yellow font");
private JButton changeToBlackButton = new JButton("Black font");
private JButton changeToBlueButton = new JButton("Blue font");
public ChangeFontColorApplicationView(){
super("Change font");
JPanel buttonPanel = new JPanel();
buttonPanel.add(changeToYellowButton);
buttonPanel.add(changeToBlackButton);
buttonPanel.add(changeToBlueButton);
this.add(buttonPanel);
}
public void addButtonActionListener(){
changeToYellowButton.addActionListener(new yellowBackgroundHandler());
changeToBlackButton.addActionListener(new yellowBackgroundHandler());
changeToBlueButton.addActionListener(new yellowBackgroundHandler());
}
}
控制器
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ChangeFontColorApplicationController {
private ChangeFontColorApplicationView changeFontColorApplicationViewObject;
backgroundHandler yellowBackgroundHandlerObject = new yellowBackgroundHandler();
backgroundHandler blackBackgroundHandlerObject = new blackBackgroundHandler();
backgroundHandler blueBackgroundHandlerObject = new blueBackgroundHandler();
public ChangeFontColorApplicationController(ChangeFontColorApplicationView changeFontColorApplicationViewObject){
this.changeFontColorApplicationViewObject = changeFontColorApplicationViewObject;
yellowBackgroundHandlerObject.setNextHandlerInChain(blackBackgroundHandlerObject);
blackBackgroundHandlerObject.setNextHandlerInChain(blueBackgroundHandlerObject);
this.changeFontColorApplicationViewObject.addButtonActionListener();
}
}
现在我创建了一个处理按钮事件的接口
public interface backgroundHandler extends ActionListener{
public void setNextHandlerInChain(backgroundHandler nextInChain);
public void handleCommand(String getActionCommandString);
public void actionPerformed(ActionEvent event);
}
每个实现接口的类都处理 ActionEvent。如果不能,则传递给下一个类(作为责任链)
黄色字体处理程序
import java.awt.Color;
import java.awt.event.ActionEvent;
public class yellowBackgroundHandler implements backgroundHandler{
private backgroundHandler nextInChain;
private ChangeFontColorApplicationView ChangeFontColorApplicationViewObject = new ChangeFontColorApplicationView();
public yellowBackgroundHandler(){}
@Override
public void setNextHandlerInChain(backgroundHandler nextInChain) {
this.nextInChain = nextInChain;
}
@Override
public void handleCommand(String getActionCommandString) {
if(getActionCommandString.equalsIgnoreCase("Yellow font")){
ChangeFontColorApplicationViewObject.setBackground(Color.yellow);
}
else {
nextInChain.handleCommand(getActionCommandString);
}
}
@Override
public void actionPerformed(ActionEvent event) {
try{
handleCommand(event.getActionCommand());
}
catch (Exception exeption){
ChangeFontColorApplicationViewObject.setTitle(exeption.toString());
}
}
}
黑色字体处理程序
import java.awt.Color;
import java.awt.event.ActionEvent;
public class blackBackgroundHandler implements backgroundHandler{
private backgroundHandler nextInChain;
private ChangeFontColorApplicationView ChangeFontColorApplicationViewObject = new ChangeFontColorApplicationView();
public blackBackgroundHandler(){}
@Override
public void setNextHandlerInChain(backgroundHandler nextInChain) {
this.nextInChain = nextInChain;
}
@Override
public void handleCommand(String getActionCommandString) {
if(getActionCommandString.equalsIgnoreCase("Black font")){
ChangeFontColorApplicationViewObject.setBackground(Color.BLACK);
}
else {
nextInChain.handleCommand(getActionCommandString);
}
}
@Override
public void actionPerformed(ActionEvent event) {
try{
handleCommand(event.getActionCommand());
}
catch (Exception exeption){
ChangeFontColorApplicationViewObject.setTitle(exeption.toString());
}
}
}
蓝色字体处理程序
import java.awt.Color;
import java.awt.event.ActionEvent;
public class blueBackgroundHandler implements backgroundHandler{
private backgroundHandler nextInChain;
private ChangeFontColorApplicationView ChangeFontColorApplicationViewObject = new ChangeFontColorApplicationView();
public blueBackgroundHandler(){}
@Override
public void setNextHandlerInChain(backgroundHandler nextInChain) {
this.nextInChain = nextInChain;
}
@Override
public void handleCommand(String getActionCommandString) {
if(getActionCommandString.equalsIgnoreCase("Μπλε φόντο")){
ChangeFontColorApplicationViewObject.setBackground(Color.BLUE);
}
else {
ChangeFontColorApplicationViewObject.setTitle("This is the back end of COR");
}
}
@Override
public void actionPerformed(ActionEvent event) {
try{
handleCommand(event.getActionCommand());
}
catch (Exception exeption){
ChangeFontColorApplicationViewObject.setTitle(exeption.toString());
}
}
}
最后我用 main 方法创建了类
public class ChangeFontColorApp {
public static void main(String[] args){
ChangeFontColorApplicationView changeFontColorApplicationViewObject = new ChangeFontColorApplicationView();
ChangeFontColorApplicationController changeFontColorApplicationControllerObject = new ChangeFontColorApplicationController(changeFontColorApplicationViewObject);
changeFontColorApplicationViewObject.setVisible(true);
changeFontColorApplicationViewObject.setSize(600, 600);
changeFontColorApplicationViewObject.setDefaultCloseOperation(ChangeFontColorApplicationView.EXIT_ON_CLOSE);
}
}
有什么办法可以使这项工作?我不想有一个用于事件处理的内部类,其中包含多个 if blick。任何建议将不胜感激。