简体   繁体   English

Java ActionListeners到独立类

[英]Java ActionListeners to stand-alone classes

Is there any way to move actionListener classes to stand-alone classes? 有什么方法可以将actionListener类移动到独立类?

I made an exaple using Java and MVC design pattern. 我以Java和MVC设计模式为例。 I have 3 buttons that change background color. 我有3个按钮可以更改背景颜色。

Here's Model 这是模特

public class ChangeFontColorApplicationModel {
    public ChangeFontColorApplicationModel(){}
}

View 视图

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

Controller 控制者

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

Now I created an interface that handles the button events 现在,我创建了一个处理按钮事件的界面

public interface backgroundHandler extends ActionListener{
public void setNextHandlerInChain(backgroundHandler nextInChain);
public void handleCommand(String getActionCommandString);
public void actionPerformed(ActionEvent event);
}

Every class that implements the interfaces, handles the ActionEvent. 每个实现接口的类都处理ActionEvent。 If it cannot, it passes to the next class (as Chain of responsibility) 如果不能,则转到下一类(作为责任链)

yellow font handler 黄色字体处理程序

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

black font handler 黑色字体处理程序

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

blue font handler 蓝色字体处理程序

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

Finally I create the class with the main method 最后,我使用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);
}
}

Is there any way to make this work? 有什么办法可以使这项工作吗? I don't want to have an inner class for event handling with a multiple if blick in it. 我不想有一个内部类用于事件处理,如果其中包含blick,则使用多个。 Any suggestions would be really appreciated. 任何建议将不胜感激。

That won't work. 那行不通。 But, the problem is not because you have standalone classes (using standalone classes is fine). 但是,问题不在于您有独立类(使用独立类就可以)。 Rather, the problem is: 相反,问题是:

In view class, you have: 在视图类中,您具有:

public void addButtonActionListener(){
    changeToYellowButton.addActionListener(new yellowBackgroundHandler());
    changeToBlackButton.addActionListener(new yellowBackgroundHandler());
    changeToBlueButton.addActionListener(new yellowBackgroundHandler());
}

this means that clicks of all buttons are handled by yellowBackgroundHandler. 这意味着所有按钮的单击都由yellowBackgroundHandler处理。 But, yellowBackgroundHandler only accepts clicks from changeToYellowButton. 但是,yellowBackgroundHandler仅接受来自changeToYellowButton的点击。 In addition to that, yellowBackgroundHandler is not configured with next handler. 除此之外,还没有为YellowBackgroundHandler配置下一个处理程序。

To fix this, in view class, change this: 要解决此问题,请在视图类中更改此内容:

public void addButtonActionListener(){
    changeToYellowButton.addActionListener(new yellowBackgroundHandler());
    changeToBlackButton.addActionListener(new yellowBackgroundHandler());
    changeToBlueButton.addActionListener(new yellowBackgroundHandler());
}

to

public void addButtonActionListener(backgroundHandler handler){
    changeToYellowButton.addActionListener(handler);
    changeToBlackButton.addActionListener(handler);
    changeToBlueButton.addActionListener(handler);
}

and in controller class, change this: 并在控制器类中更改此:

this.changeFontColorApplicationViewObject.addButtonActionListener();

to

this.changeFontColorApplicationViewObject.addButtonActionListener(yellowBackgroundHandlerObject);

Another problem that I see is that yellowBackgroundHandler, blackBackgroundHandler, and blueBackgroundHandler have private variable ChangeFontColorApplicationViewObject, which points to its own instance of ChangeFontColorApplicationView. 我看到的另一个问题是yellowBackgroundHandler,blackBackgroundHandler和blueBackgroundHandler具有私有变量ChangeFontColorApplicationViewObject,该变量指向其自己的ChangeFontColorApplicationView实例。 We need this variable to point to the instance created in the main method. 我们需要此变量指向在main方法中创建的实例。 To do this, change this: 为此,请更改此:

private ChangeFontColorApplicationView ChangeFontColorApplicationViewObject = new ChangeFontColorApplicationView();
public blueBackgroundHandler(){}

to: 至:

private ChangeFontColorApplicationView ChangeFontColorApplicationViewObject;
public blueBackgroundHandler(ChangeFontColorApplicationView ChangeFontColorApplicationViewObject){
  this.ChangeFontColorApplicationViewObject = ChangeFontColorApplicationViewObject;
}

and change this: 并更改此:

private ChangeFontColorApplicationView ChangeFontColorApplicationViewObject = new ChangeFontColorApplicationView();
public blackBackgroundHandler(){}

to: 至:

private ChangeFontColorApplicationView ChangeFontColorApplicationViewObject;
public blackBackgroundHandler(ChangeFontColorApplicationView ChangeFontColorApplicationViewObject){
  this.ChangeFontColorApplicationViewObject = ChangeFontColorApplicationViewObject;
}

and change this: 并更改此:

private ChangeFontColorApplicationView ChangeFontColorApplicationViewObject = new ChangeFontColorApplicationView();
public blueBackgroundHandler(){}

to: 至:

private ChangeFontColorApplicationView ChangeFontColorApplicationViewObject;
public blueBackgroundHandler(ChangeFontColorApplicationView ChangeFontColorApplicationViewObject){
  this.ChangeFontColorApplicationViewObject = ChangeFontColorApplicationViewObject;
}

finally, change this: 最后,更改此:

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

to

backgroundHandler yellowBackgroundHandlerObject = new yellowBackgroundHandler();
backgroundHandler blackBackgroundHandlerObject = new blackBackgroundHandler();
backgroundHandler blueBackgroundHandlerObject = new blueBackgroundHandler();

public ChangeFontColorApplicationController(ChangeFontColorApplicationView changeFontColorApplicationViewObject){
    this.changeFontColorApplicationViewObject = changeFontColorApplicationViewObject;
    this.yellowBackgroundHandlerObject = new yellowBackgroundHandler(this.changeFontColorApplicationViewObject);
    this.blackBackgroundHandlerObject = new blackBackgroundHandler(this.changeFontColorApplicationViewObject);
    this.blueBackgroundHandlerObject = new blueBackgroundHandler(this.changeFontColorApplicationViewObject);

    yellowBackgroundHandlerObject.setNextHandlerInChain(blackBackgroundHandlerObject);
    blackBackgroundHandlerObject.setNextHandlerInChain(blueBackgroundHandlerObject);

    this.changeFontColorApplicationViewObject.addButtonActionListener(this.changeFontColorApplicationViewObject);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM