gpt4 book ai didi

独立类的 Java ActionListener

转载 作者:行者123 更新时间:2023-12-01 22:54:16 24 4
gpt4 key购买 nike

有没有办法将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());
}
}

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

现在我创建了一个处理按钮事件的界面

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

每个实现接口(interface)的类都会处理 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。任何建议将不胜感激。

最佳答案

那是行不通的。但是,问题并不是因为您有独立的类(使用独立的类就可以了)。相反,问题是:

在 View 类中,您有:

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

这意味着所有按钮的点击都由 YellowBackgroundHandler 处理。但是,yellowBackgroundHandler 只接受来自changeToYellowButton 的点击。除此之外,yellowBackgroundHandler 未配置下一个处理程序。

要解决此问题,请在 View 类中更改此内容:

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

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

并在 Controller 类中,更改此:

this.changeFontColorApplicationViewObject.addButtonActionListener();

this.changeFontColorApplicationViewObject.addButtonActionListener(yellowBackgroundHandlerObject);

我看到的另一个问题是 YellowBackgroundHandler、blackBackgroundHandler 和 blueBackgroundHandler 有私有(private)变量 ChangeFontColorApplicationViewObject,它指向它自己的 ChangeFontColorApplicationView 实例。我们需要这个变量指向在 main 方法中创建的实例。为此,请更改以下内容:

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

至:

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

并更改此:

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

至:

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

并更改此:

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

至:

private ChangeFontColorApplicationView ChangeFontColorApplicationViewObject;
public blueBackgroundHandler(ChangeFontColorApplicationView ChangeFontColorApplicationViewObject){
this.ChangeFontColorApplicationViewObject = 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();
}

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

关于独立类的 Java ActionListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24326910/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com