CardLayout не будет работать в проекте MVC

Я создал класс с несколькими JPanels и хочу переключаться между этими JPanels с помощью cardLayout. Я создал свой код, следуя шаблону MVC, и я хотел бы сохранить его таким. По некоторым причинам я думаю, что метод actionPerfomed из addCostumersListerner никогда не выполняется. Любая идея почему?

package project3;

import java.awt.*;
import java.awt.event.ActionListener;

import javax.swing.*;

@SuppressWarnings("serial")
public class MainView extends JFrame{

    private JButton costumersButton = new JButton("COSTUMERS");
    private JButton productsButton = new JButton("PRODUCTS");
    private JButton ordersButton = new JButton("ORDERS");
    private JButton createBillButton = new JButton("CREATE BILL");

    private JButton addCostumerButton = new JButton("ADD COSTUMER");
    private JButton updateCostumerButton = new JButton("UPDATE COSTUMER");
    private JButton findCostumerButton = new JButton("FIND COSTUMER");
    private JButton deleteCostumerButton = new JButton("DELETE COSTUMER");
    private JButton filterCostumersButton = new JButton("FILTER COSTUMERS");
    private JButton viewCostumersButton = new JButton("VIEW COSTUMERS");

    //private CardLayout cardLayout;

    JPanel backgroundPanel = new JPanel();
    JPanel menuPanel = new JPanel(new CardLayout());
    JPanel costumersMenuPanel = new JPanel();
    JPanel generalAccesMenuPanel = new JPanel();
    JPanel displayPanel = new JPanel();

    public MainView() {
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(900, 600);
        this.setName("Login Form");
        this.setResizable(false);
        this.setLocationRelativeTo(null);

        //un panou mare care il vom aseza pe frame
        backgroundPanel.setLayout(new BorderLayout());

        //panou cu cardLayout ca sa putem realiza interschimbarea dintre meniuri
        menuPanel.setPreferredSize(new Dimension(200, 600));
        menuPanel.setBackground(new Color(45, 32, 100));


        //panoul de acces general
        generalAccesMenuPanel.setPreferredSize(new Dimension(200, 600));
        generalAccesMenuPanel.setLayout(new BoxLayout(generalAccesMenuPanel, BoxLayout.PAGE_AXIS));
        generalAccesMenuPanel.setBackground(new Color(45, 32, 100));

        costumersButton.setMaximumSize(new Dimension(200, 40));
        productsButton.setMaximumSize(new Dimension(200, 40));
        ordersButton.setMaximumSize(new Dimension(200, 40));
        createBillButton.setMaximumSize(new Dimension(200, 40));

        generalAccesMenuPanel.add(costumersButton);
        generalAccesMenuPanel.add(productsButton);
        generalAccesMenuPanel.add(ordersButton);
        generalAccesMenuPanel.add(createBillButton);

        //panou pentru lucrul cu costumerii

        costumersMenuPanel.setPreferredSize(new Dimension(200, 600));
        costumersMenuPanel.setLayout(new BoxLayout(costumersMenuPanel, BoxLayout.PAGE_AXIS));
        costumersMenuPanel.setBackground(new Color(45, 32, 100));

        addCostumerButton.setMaximumSize(new Dimension(200, 40));
        updateCostumerButton.setMaximumSize(new Dimension(200, 40));
        findCostumerButton.setMaximumSize(new Dimension(200, 40));
        deleteCostumerButton.setMaximumSize(new Dimension(200, 40));
        filterCostumersButton.setMaximumSize(new Dimension(200, 40));
        viewCostumersButton.setMaximumSize(new Dimension(200, 40));

        costumersMenuPanel.add(addCostumerButton);
        costumersMenuPanel.add(updateCostumerButton);
        costumersMenuPanel.add(findCostumerButton);
        costumersMenuPanel.add(deleteCostumerButton);
        costumersMenuPanel.add(filterCostumersButton);
        costumersMenuPanel.add(viewCostumersButton);

        //punem meniurile in panoul cu cardLayout
        menuPanel.add(generalAccesMenuPanel, "Card 1");
        menuPanel.add(costumersMenuPanel, "Card 2");


        //panoul unde vom afisa tabelele
        displayPanel.setBackground(new Color(107, 3, 135));

        //punem panel-uri pe panoul principal
        backgroundPanel.add(displayPanel, BorderLayout.CENTER);
        backgroundPanel.add(menuPanel, BorderLayout.LINE_START);

        this.add(backgroundPanel);

    }

    public void showCostumersMenu(ActionListener listenForCostumersMenu) {
        costumersButton.addActionListener(listenForCostumersMenu);
    }

    public void setCostumersMenu() {
        CardLayout cardLayout = (CardLayout) menuPanel.getLayout();
        cardLayout.show(menuPanel, "Card 2");
    }
}

Второй интерфейс (который не очень важен для предмета, но если кто-то хочет запустить код, я подумал, что должен поставить его здесь)

 package project3;

    import javax.swing.JFrame;

    import java.awt.Container;
    import java.awt.Font;
    import java.awt.event.ActionListener;

    import javax.swing.*;

    @SuppressWarnings("serial")
    public class LogInWindow extends JFrame {
        private Container container = getContentPane();
        private JLabel titleLabel = new JLabel("WarehouseApp");
        private JLabel userLabel = new JLabel("USERNAME");
        private JLabel passwordLabel = new JLabel("PASSWORD");
        private JTextField userTextField = new JTextField();
        private JPasswordField passwordField = new JPasswordField();
        private JButton loginButton = new JButton("LOGIN");
        private JCheckBox showPassword = new JCheckBox("Show Password");
        private JLabel logInAsLabel = new JLabel("LOGIN AS");
        private JComboBox<String> logInAsComboBox = new JComboBox<String>();

        public LogInWindow() {
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setBounds(10, 10, 370, 370);
            this.setName("Login Form");
            this.setResizable(false);
            this.setLocationRelativeTo(null);

            container.setLayout(null);

            titleLabel.setBounds(80, -10, 200, 100);
            userLabel.setBounds(50, 80, 100, 30);
            userTextField.setBounds(150, 80, 150, 30);
            passwordLabel.setBounds(50, 130, 100, 30);
            passwordField.setBounds(150, 130, 150, 30);
            logInAsLabel.setBounds(50, 180, 100, 30);
            logInAsComboBox.setBounds(150, 180, 150, 30);
            showPassword.setBounds(150, 220, 150, 30);
            loginButton.setBounds(150, 260, 100, 30);

            Font font = new Font("Times New Roman", Font.BOLD, 30);
            titleLabel.setFont(font);
            logInAsComboBox.addItem("ADMIN");
            logInAsComboBox.addItem("CLIENT");
            logInAsComboBox.setSelectedIndex(-1);

            container.add(titleLabel);
            container.add(userLabel);
            container.add(passwordLabel);
            container.add(userTextField);
            container.add(passwordField);
            container.add(logInAsLabel);
            container.add(logInAsComboBox);
            container.add(showPassword);
            container.add(loginButton);

            this.setVisible(true);
        }

        public void showPasswordWhenClicked(ActionListener listenForPassword) {
            showPassword.addActionListener(listenForPassword);
        }

        public void connectWhenClicked(ActionListener listenForLoginButton) {
            loginButton.addActionListener(listenForLoginButton);
        }

        public boolean getPasswordStatus() {
            if (showPassword.isSelected()==true) 
                return true;
            return false;
        }

        public void setPasswordVisible() {
            passwordField.setEchoChar((char) 0);
        }

        public void setPasswordInvisible() {
            passwordField.setEchoChar('*');
        }   
    }

And finally, the controller: 

package project3;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class Controller {

    private LogInWindow theView;
    private MainView mainView;

    public Controller(LogInWindow theView, MainView mainView) {
        this.theView = theView;
        this.mainView = mainView;

        this.theView.showPasswordWhenClicked(new showPasswordListener());
        this.theView.connectWhenClicked(new loginListener());
        this.mainView.showCostumersMenu(new addCostumersListener());
    }

    public class loginListener implements ActionListener {
        public void actionPerformed(ActionEvent arg0) {
            MainView mainView = new MainView();
            mainView.setVisible(true);
            theView.setVisible(false);
        }
    }

    public class showPasswordListener implements ActionListener {
        public void actionPerformed(ActionEvent arg0) {
            if (theView.getPasswordStatus()==true) {
                theView.setPasswordVisible();
            } else {
                theView.setPasswordInvisible();
            }
        }
    }

    public class addCostumersListener implements ActionListener {
        public void actionPerformed(ActionEvent arg0) {
            mainView.setCostumersMenu();
        }
    }

    public static void main(String[] args) {
        LogInWindow logIn = new LogInWindow();
        MainView mainView = new MainView();
        new Controller(logIn, mainView);
    }
}

0 ответов

Другие вопросы по тегам