如何从不同的类中观察JButton?

时间:2013-03-19 20:37:28

标签: java swing model-view-controller jbutton observer-pattern

所以我有一个扩展JPanel的类,在构造函数中我添加了JButtons以及我需要添加的任何其他内容。我还有一个MainFrame类,它是容器(JFrame),这个类将从一个名为FrameSwitcher(Controller)的类中获取一个参数,该类将评估点击的按钮,并通过信息到MainFrame

我遇到麻烦,我找不到合适的方法来做到这一点。我也希望将JButtons保持为私有和非静态。

JPanel示例:

public class MainMenu() {

    private JButton btnSinglePlayer, btnMultiPlayer;

    public MainMenu() {
        setLayout(null);

        btnSinglePlayer = new JButton("singlePlayer");
        btnSinglePlayer.setBounds(320, 25, 275, 130);
        add(btnSinglePlayer);

        btnMultiPlayer = new JButton("MultiPlayer");
        btnMultiPlayer.setBounds(320, 170 , 275, 130);
        add(btnMultiPlayer);

   }
}

FrameSwitcher:

public class FrameSwitcher implements panelListener { // panelListener is an interface defined else where.

    public FrameSwitcher(MainFrame frame) {
        // This is irrelevant to the question. 
    } 

    @Override
    public void gamePanel() {
        System.out.println("GamePanel Event: Recieved");
    }
    @Override
    public void mainMenu() {
        System.out.println("mainMenu Event: Recieved");
    }
    @Override
    public void scoreBoardPanel() {
        System.out.println("scoreBoardPanel Event: Recieved");
    }
}

然后我的MainFrame:

public class MainFrame extends JFrame implements ActionListener {

    private PanelListener panelListener;
    private JFrame mainContainer = new JFrame("Game");
    private JPanel mainMenu = new MainMenu();

    public void start() {
         mainContainer(mainMenu);
    }

    public MainFrame(JPanel frame) {
        mainContainer.getContentPane().add(frame);
        mainContainer.pack();
        // Other methods to initialize the frame
        return mainContainer;
     }

    public void switchFrames(PanelListener panelListener) {
        this.panelListener = panelListener; // PanelListener is an interface.
    }

    public void actionPerformed(ActionEvent e) {

        JButton source = (JButton)e.getsource();

        if(source == MainMenu.btnSinglePlayer) {
            if(panelListener != null) {
                System.out.println("Recieved the event approriately.");
            }
        }
    }
}

在这个例子中,它确实编译,但没有做它应该做的事情。另一件事是我目前将JButton视为公共和静态,我不希望这样。

2 个答案:

答案 0 :(得分:2)

MainMenu课程中,您需要添加某些感兴趣的人可以注册的听众,因此当某些事件发生时,可以通知他们。

最简单的解决方案是提供委派给每个按钮的addActionListener方法。但是,这可能会暴露您未暴露的部分应用程序(监听器现在可以直接访问JButton并且可以对其执行各种令人讨厌的事情。)

更好的解决方案是创建类似MainMenuListener的内容,其中包含startSinglePlayerstartMultiPlayer等方法

然后,您将在add/removeMainMenuListener课程中提供MainMenu方法。

然后,每个按钮都会在那里注册自己的actionListener并触发相应的菜单监听器事件

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.EventListener;
import javax.swing.JButton;
import javax.swing.JPanel;

public class MainMenu extends JPanel {

    private JButton btnSinglePlayer, btnMultiPlayer;

    public MainMenu() {
        setLayout(null);

        btnSinglePlayer = new JButton("singlePlayer");
        btnSinglePlayer.setBounds(320, 25, 275, 130);
        add(btnSinglePlayer);
        btnSinglePlayer.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                fireStartSinglePlayer();
            }
        });

        btnMultiPlayer = new JButton("MultiPlayer");
        btnMultiPlayer.setBounds(320, 170, 275, 130);
        add(btnMultiPlayer);
        btnMultiPlayer.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                fireStartMultiPlayer();
            }
        });

    }

    public void addMainMenuListener(MainMenuListener listener) {
        listenerList.add(MainMenuListener.class, listener);
    }

    public void removeMainMenuListener(MainMenuListener listener) {
        listenerList.remove(MainMenuListener.class, listener);
    }

    public void fireStartSinglePlayer() {
        MainMenuListener[] listeners = listenerList.getListeners(MainMenuListener.class);
        if (listeners != null && listeners.length > 0) {
            for (MainMenuListener listener : listeners) {
                listener.startSinglePlayer();
            }
        }
    }

    public void fireStartMultiPlayer() {
        MainMenuListener[] listeners = listenerList.getListeners(MainMenuListener.class);
        if (listeners != null && listeners.length > 0) {
            for (MainMenuListener listener : listeners) {
                listener.startMultiPlayer();
            }
        }
    }

    public interface MainMenuListener extends EventListener {

        public void startSinglePlayer();
        public void startMultiPlayer();

    }

}

答案 1 :(得分:0)

首先,你必须像这样创建一个ActionListener

class MyActionListener implements ActionListener{
    public void actionPerformed(ActionEvent ae){
        //do your stuff
    }
}

然后打电话,

yourButton.addActionListener(new MyActionListener());