0

有什麼方法可以將actionListener類移動到獨立類?Java ActionListeners到獨立類

我做了一個使用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());    
    } 
} 
} 

最後,我用主要方法創建類

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

有什麼辦法可以使這項工作?我不希望有一個內部類與事件處理與多個如果blick中。任何建議將非常感激。

+1

請仔細閱讀[如何創建一個最小的,完整的和可驗證的示例](HTTP ://stackoverflow.com/help/mcve)。代碼牆是不好的。沒有人想讀這一切。 –

+0

是的,你可以編寫一個動作控制器類。你的控制器不一定是一個類。它可以是許多課程,每個課程都對某種行爲作出反應。看看我的[2048 Game in Java Swing](http://java-articles.info/articles/?p=516)文章,看看使用MVC創建Java Swing應用程序的一種方法。 –

+0

好吧,我會讀兩個。謝謝您的建議 –

回答

3

這是行不通的。但是,問題不是因爲你有獨立的類(使用獨立的類很好)。相反,問題是:

在視圖類,你必須:

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

這意味着,所有按鈕的點擊次數是按yellowBackgroundHandler處理。但是,yellowBackgroundHandler只接受來自changeToYellowButton的點擊。除此之外,yellowBackgroundHandler沒有配置下一個處理程序。

爲了解決這個問題,考慮到類,改變該:

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

和控制器類,改變該:

this.changeFontColorApplicationViewObject.addButtonActionListener(); 

this.changeFontColorApplicationViewObject.addButtonActionListener(yellowBackgroundHandlerObject); 

我看到的另一個問題是,yellowBackgroundHandler,blackBackgroundHandler和blueBackgroundHandler具有專用變量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); 
} 
+0

恐怕沒有工作,要麼...這將是因爲添加在控制器的ActionListeners並沒有考慮到,添加按鈕buttonPanel過嗎? –

+0

也許你可以詳細說明「也沒有工作」?黃色按鈕是否有效? – fajarkoe

+0

不,它不需要...更具體,我發現該事件stucks成EventDispatchThread.java,pumpEventsForFilter方法while循環。進入循環後,調試只停留在那裏。我不能再繼續下去了。這就像它不能離開while循環並停止調試。那可能是什麼?每個按鈕都是一樣的。 –