重做cardlayout-应用到MVC架构中 [英] Redo cardlayout-application into MVC architecture

查看:89
本文介绍了重做cardlayout-应用到MVC架构中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以.我已经申请了.它有一个mainFrame,它创建了两个面板,一个面板包含我的向导式按钮(例如,Next,Previous)和一个cardLayout,使我能够转到在mainFrame中声明的下一个面板或上一个面板.通过具有可验​​证的接口,我可以覆盖每个窗口类的isDataValid()函数,该函数确定next和prev按钮是否应调用cardlayouts翻转函数.

So. I've made an application. It has a mainFrame which creates two panels, one holding my wizard-alike buttons (e.g. Next, Previous) and one cardLayout, enabling me to go to either next or previous panel, which I declare in the mainFrame. By having an interface, Verifiable, I'm able to override every window-class a isDataValid()-function which decides whether next and prev buttons should call for cardlayouts flip functions.

源代码如下:

public class MainFrame {

    private static void createAndShowGUI() {
        JFrame frame = new JFrame("Dimensions helper");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);

        final JPanel contentPane = new JPanel();
        contentPane.setLayout(new CardLayout(5, 5));

        Window1 win1 = new Window1();
        contentPane.add(win1);
        Window2 win2 = new Window2();
        contentPane.add(win2);
        Window3 win3 = new Window3();
        contentPane.add(win3);
        Window4 win4 = new Window4();
        contentPane.add(win4);
        Window5 win5 = new Window5();
        contentPane.add(win5);
        Window6 win6 = new Window6();
        contentPane.add(win6);

        JPanel buttonPanel = new JPanel(); 
        final JButton previousButton = new JButton("< PREVIOUS");
        previousButton.setEnabled(false);
        final JButton nextButton = new JButton("NEXT >");
        final JButton cancelButton = new JButton("CANCEL");
        buttonPanel.add(cancelButton);
        buttonPanel.add(previousButton);
        buttonPanel.add(nextButton);

        previousButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                CardLayout cardLayout = (CardLayout) contentPane.getLayout();
                cardLayout.previous(contentPane);
                Component[] contents = contentPane.getComponents();
                nextButton.setText("NEXT");
                for(Component component : contents) {
                    if(component instanceof Verifiable && component.isVisible()) {
                        Verifiable window = (Verifiable)component;
                        if(window.getIdentifier().equals("FIRST")) {
                            previousButton.setEnabled(false);
                        }
                        break;
                    }
                }
            }

        });

        nextButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                Verifiable verifiable = null;
                Component[] contents = contentPane.getComponents();
                for(Component component : contents) {
                    if(component.isVisible() && component instanceof Verifiable) {
                        verifiable = (Verifiable)component;
                    }
                }
                if(verifiable != null && verifiable.isDataValid()) {
                    CardLayout cardLayout = (CardLayout) contentPane.getLayout();
                    cardLayout.next(contentPane); 
                    previousButton.setEnabled(true);

                }
            }
        });



        cancelButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                System.exit(0);
            }

        });

        frame.add(contentPane);
        frame.add(buttonPanel, BorderLayout.PAGE_END);
        frame.setSize(400, 400);
        frame.setVisible(true);
    }



    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
} 

public class Window1 extends JPanel implements Verifiable {

    public static final String IDENTIFIER = "FIRST";

    JTextField txtUsername = new JTextField();
    JPasswordField txtPassword = new JPasswordField();

    Database db = new Database();

    public Window1() {
        init();
    }

    private void init() {
        JLabel lblUsername = new JLabel("Username:", JLabel.CENTER);
        lblUsername.setBounds(10, 91, 135, 77);
        txtUsername = new JTextField();
        txtUsername.setBounds(155, 116, 188, 27);

        JLabel lblPassword = new JLabel("Password:", JLabel.CENTER);
        lblPassword.setBounds(0, 163, 149, 77);
        txtPassword = new JPasswordField();
        txtPassword.setBounds(155, 188, 188, 27);
        setLayout(null);

        add(lblUsername);
        add(txtUsername);
        add(lblPassword);
        add(txtPassword);
        String title = "Log in";
        setBorder(BorderFactory.createTitledBorder(title));
    }

    @Override
    public boolean isDataValid() {
        String username = new String(txtUsername.getText());
        String password = new String(txtPassword.getPassword());


        try {
            Database conn = di.getDatabaseConnection(username, password);
            di.getTest(conn);
            return true;
            } catch (LoginFailedException e) {
                JOptionPane.showMessageDialog(this, "Something went wrong", 
                        "Error", JOptionPane.ERROR_MESSAGE);
                return false;
        }
    }

    @Override
    public String getIdentifier() {
        return IDENTIFIER;
    }
}

interface Verifiable {
    boolean isDataValid();
    String getIdentifier();
}

但是.我想将该应用程序重做为Model-View-Controller体系结构,这意味着我在Window类(即视图)中不再有任何验证代码.因此,我将旧的Window类划分为WinView类(Win1),分别是接口(Interface1)和用于验证数据的模型.

However. I want to redo this application into a Model-View-Controller architecure, meaning that I no longer have any validation code in the Window-classes (i.e. the views). Therefore I've devided the old Window-classes into a WinView-class (Win1) respectively an interface (Interface1) and a Model to verify the data.

public class Win1 extends JPanel implements Interface1 {

    JTextField txtUsername = new JTextField();
    JPasswordField txtPassword = new JPasswordField();


    public Win1() {
        JLabel lblUsername = new JLabel("Username:", JLabel.CENTER);
        txtUsername = new JTextField();
        JLabel lblPassword = new JLabel("Password:", JLabel.CENTER);
        txtPassword = new JPasswordField();

        setLayout(null);
        add(lblUsername);
        add(txtUsername);
        add(lblPassword);
        add(txtPassword);
        String title = "Log in";
        setBorder(BorderFactory.createTitledBorder(title)); 

    }

    @Override
    public String getUsername() {
        return txtUsername.getText();
    }

    @Override
    public String getPassword() {
        String password = new String(txtPassword.getPassword());
        return password;
    }

}

public interface Interface1 {
    String getUsername();
    String getPassword();
}


public class Model {

    Model() {

    }       
}

到目前为止,我的MainFrame看起来还是一样.当我尝试规划新架构时,我陷入了困境.我应该如何绑定MVC体系结构?我对当前的下一个和上一个按钮的工作方式感到非常满意,是否有可能仍像现在一样使用它们?

My MainFrame looks the same so far. I get stuck when I'm trying to plan my new architecture. How should I bind the MVC architecture? I'm very happy with how my next and previous buttons work at the moment, is it possible to somehow still use them like I currently do?

推荐答案

我要说的第一件事是,据我了解MVC方法论(我可能错了),任何直接影响View自身行为的事物应该保留在视图"中(例如启用/禁用按钮).其他(功能方法)应由控制器处理.

The first thing I want to say is, as far as I understand the MVC methodology (I could be wrong), anything that directly influences how the View itself behaves should remain in the View (like enabling / disabling buttons). Anything else (functional methods) should be handled by a controller.

我会使用单人模式.有一个名为MainController的类,并在该类上使用单例模式.然后在您的MainView中,有一个名为controllerMainController实例的全局变量.然后在控制器中使用方法isDataValid(String username, String password)并调用boolean valid = controller.isDataValid(txtUsername.getText(), new String(txtPassword.getPassword()));.任何功能方法都可以完成同一件事.我还要指出,控制器是唯一应该与数据库建立任何连接才能使MVC方法学起作用的控制器.我希望这会有所帮助.

I would use the Singleton Pattern. Have a class called MainController and use the singleton pattern on that class. Then in your MainView, have a global variable of an instance of the MainController called controller. Then in the controller have a method of isDataValid(String username, String password) and call boolean valid = controller.isDataValid(txtUsername.getText(), new String(txtPassword.getPassword()));. The same thing could be done with any functional methods. I would also point out that controllers are the only ones which should have any connection to the database for the MVC methodology to work. I hope this helps.

这篇关于重做cardlayout-应用到MVC架构中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆