为什么ActionListener不在控制器中工作?

时间:2015-04-13 04:42:38

标签: java swing model-view-controller actionlistener cardlayout

此帖子继续this post.我的软件基本上基于CardLayout小组。现在我想从cardLayout之外更改面板。在提供的链接中,我得到了帮助,并且它有效。现在我在我的软件中添加了一个控制器,它位于内部面板(CardLayout)和外部面板之间。在外部面板(leftBar)中,我有一个按钮,如果其actionListener位于该类内部,则它可以正常工作并打开所选择的CardLayout内部面板。但是,如果我将ActionListener带入控制器,它就不起作用了。没有错误出现。

这是我的示例代码: BASE:

public class Base {
        JFrame frame = new JFrame("Panel");
        BorderLayout bl = new BorderLayout();

    public Base(){
        MainPanel mainPanel = new MainPanel();
        LeftBar leftBar = new LeftBar(mainPanel);
        frame.setLayout(bl);
        frame.setSize(800, 600);
        frame.add(leftBar, BorderLayout.WEST);
        frame.add(mainPanel, BorderLayout.CENTER);

        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
    public static void main(String[] args) throws IOException {
        new Base();
    }
}

MainPanel(CardLAyout base)

public class MainPanel extends JPanel {
    private CardLayout cl = new CardLayout();
    private JPanel panelHolder = new JPanel(cl);

    public MainPanel(){
        FirstPage firstPage = new FirstPage(this);
        SecondPage secondPage = new SecondPage(this);
        LeftBar leftBar = new LeftBar(this);

        Controller controller = new Controller(secondPage, leftBar, this);
        setLayout(new GridLayout(0,1));

        panelHolder.add(firstPage, "firstPage");
        panelHolder.add(secondPage, "secondPage");

        cl.show(panelHolder, "firstPage");
        add(panelHolder);

    }
    public void showPanel(String panelIdentifier){
        cl.show(panelHolder, panelIdentifier);
    }
}

LeftBar

public class LeftBar extends JPanel{
    private JButton button;
    private MainPanel mainPanel;

    public LeftBar(MainPanel mainPanel){
        this.mainPanel = mainPanel;

        setPreferredSize(new Dimension(200, 40));
        setLayout(new BorderLayout());
        setBackground(Color.black);

        button = new JButton("Show Second Page");


       add(button, BorderLayout.NORTH);
    }
    public void addPageListener(ActionListener listenForButton){
        button.addActionListener(listenForButton);
    }
}

第二页:

public class SecondPage extends JPanel{
    MainPanel mainPanel;
    JButton button;
    public SecondPage(MainPanel mainPanel){
       this.mainPanel = mainPanel;
        setBackground(Color.white);
       add(new JLabel("This is second page"));
    }
}

第一页:

public class FirstPage extends JPanel {
    MainPanel mainPanel;
    JButton button;

    public FirstPage(MainPanel mainPanel) {
       this.mainPanel = mainPanel;
       setBackground(Color.GRAY);

       button = new JButton("Show page");
       button.addActionListener(new ActionListener(){
           @Override
           public void actionPerformed(ActionEvent ae) {
               mainPanel.showPanel("secondPage");
           }

       });

       add(button);
    }
}

和控制器:

public class Controller {
    private SecondPage secondPage;
    private LeftBar leftBar;
    private MainPanel mainPanel;
    public Controller(SecondPage secondPage, LeftBar leftBar, MainPanel mainPanel){
        this.secondPage=secondPage;
        this.leftBar=leftBar;
        this.mainPanel=mainPanel;

        this.leftBar.addPageListener(new ButtonListener());
    }

    class ButtonListener implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent ae) {
            System.out.println("Works");
            mainPanel.showPanel("secondPage");
        }
    }
}

正如您在LeftBar中看到的那样,我有一个按钮的方法,可用作actionListener。我在控制器中调用该方法并给它一个类。但它不起作用。但是ACtionListener如果位于LeftBar类(内联)中则有效。

知道怎么解决吗?

1 个答案:

答案 0 :(得分:2)

您正在创建LeftBar的两个实例,其中一个在Base构造函数中,并添加到屏幕中...

public Base() {
    MainPanel mainPanel = new MainPanel();
    LeftBar leftBar = new LeftBar(mainPanel);
    frame.setLayout(bl);
    frame.setSize(800, 600);
    frame.add(leftBar, BorderLayout.WEST);
    frame.add(mainPanel, BorderLayout.CENTER);

MainPanel构造函数中的一个传递给控制器​​......

public class MainPanel extends JPanel {

    private CardLayout cl = new CardLayout();
    private JPanel panelHolder = new JPanel(cl);

    public MainPanel() {
        FirstPage firstPage = new FirstPage(this);
        SecondPage secondPage = new SecondPage(this);
        LeftBar leftBar = new LeftBar(this);

这意味着控制器将ActionListener附加到屏幕上永远不可见的按钮

我要做的第一件事就是开始解耦你的代码,而不是传递你的类的实例,你需要建立一系列控制器和视图可以同意的合同,例如你的MainPanel应该提供一种切换视图的方法,LeftBar应该在用户想要切换视图时提供通知

例如......

public interface Pageable {

    public void showView(String name);

}

public interface Navigatable {

    public void addActionListener(ActionListener listener);

}

然后您的主视图将实现Pageable接口

public class MainPanel extends JPanel implements Pageable {
    //...
    @Override
    public void showView(String name) {
        cl.show(panelHolder, name);
    }

您的LeftBar将实施Navigatable界面

public class LeftBar extends JPanel implements Navigatable {
    //...
    @Override
    public void addActionListener(ActionListener listener) {
        button.addActionListener(listener);
    }

这只是定义了任何实现的契约能力,你的控制器不应该关心任何其他事情(也不应该允许它做它不是意味着的事情,比如从视图中删除所有组件,顽皮的控制器)

然后您的控制器负责管理接口之间的合同......

public class Controller {

    private final Navigatable navigatable;
    private final Pageable pageable;

    public Controller(Navigatable navigatable, Pageable pageable) {
        this.navigatable = navigatable;
        this.pageable = pageable;

        navigatable.addActionListener(new ButtonListener());
    }

    class ButtonListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent ae) {
            pageable.showView("secondPage");
        }
    }
}

例如......

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Base {

    JFrame frame = new JFrame("Panel");
    BorderLayout bl = new BorderLayout();

    public Base() {
        MainPanel mainPanel = new MainPanel();
        LeftBar leftBar = new LeftBar(mainPanel);
        frame.setLayout(bl);
        frame.setSize(800, 600);
        frame.add(leftBar, BorderLayout.WEST);
        frame.add(mainPanel, BorderLayout.CENTER);

        Controller controller = new Controller(leftBar, mainPanel);

        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    public static void main(String[] args) throws IOException {
        new Base();
    }

    public interface Pageable {

        public void showView(String name);

    }

    public interface Navigatable {

        public void addActionListener(ActionListener listener);

    }

    public class MainPanel extends JPanel implements Pageable {

        private CardLayout cl = new CardLayout();
        private JPanel panelHolder = new JPanel(cl);

        public MainPanel() {
            FirstPage firstPage = new FirstPage(this);
            SecondPage secondPage = new SecondPage(this);
            setLayout(new GridLayout(0, 1));

            panelHolder.add(firstPage, "firstPage");
            panelHolder.add(secondPage, "secondPage");

            cl.show(panelHolder, "firstPage");
            add(panelHolder);

        }

        public void showPanel(String panelIdentifier) {
            cl.show(panelHolder, panelIdentifier);
        }

        @Override
        public void showView(String name) {
            cl.show(panelHolder, name);
        }
    }

    public class LeftBar extends JPanel implements Navigatable {

        private JButton button;
        private MainPanel mainPanel;

        public LeftBar(MainPanel mainPanel) {
            this.mainPanel = mainPanel;

            setPreferredSize(new Dimension(200, 40));
            setLayout(new BorderLayout());
            setBackground(Color.black);

            button = new JButton("Show Second Page");

            add(button, BorderLayout.NORTH);
        }

        @Override
        public void addActionListener(ActionListener listener) {
            button.addActionListener(listener);
        }
    }

    public class SecondPage extends JPanel {

        MainPanel mainPanel;
        JButton button;

        public SecondPage(MainPanel mainPanel) {
            this.mainPanel = mainPanel;
            setBackground(Color.white);
            add(new JLabel("This is second page"));
        }
    }

    public class FirstPage extends JPanel {

        MainPanel mainPanel;
        JButton button;

        public FirstPage(MainPanel mainPanel) {
            this.mainPanel = mainPanel;
            setBackground(Color.GRAY);

            button = new JButton("Show page");
            button.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent ae) {
                    mainPanel.showPanel("secondPage");
                }

            });

            add(button);
        }
    }

    public class Controller {

        private final Navigatable navigatable;
        private final Pageable pageable;

        public Controller(Navigatable navigatable, Pageable pageable) {
            this.navigatable = navigatable;
            this.pageable = pageable;

            navigatable.addActionListener(new ButtonListener());
        }

        class ButtonListener implements ActionListener {

            @Override
            public void actionPerformed(ActionEvent ae) {
                pageable.showView("secondPage");
            }
        }
    }
}

就个人而言,这只是对你的代码的一些攻击,个人而言,我宁愿让一个控制器与一个视图/合同进行交互,这使得它变得更加简单......