有什麼方法可以將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中。任何建議將非常感激。
請仔細閱讀[如何創建一個最小的,完整的和可驗證的示例](HTTP ://stackoverflow.com/help/mcve)。代碼牆是不好的。沒有人想讀這一切。 –
是的,你可以編寫一個動作控制器類。你的控制器不一定是一個類。它可以是許多課程,每個課程都對某種行爲作出反應。看看我的[2048 Game in Java Swing](http://java-articles.info/articles/?p=516)文章,看看使用MVC創建Java Swing應用程序的一種方法。 –
好吧,我會讀兩個。謝謝您的建議 –