0

所以。我已经申请了。它有一个mainFrame,它创建了两个面板,一个带有我的类似向导的按钮(例如Next、Previous)和一个cardLayout,使我能够转到下一个或上一个面板,我在mainFrame 中声明了它们。通过拥有一个可验证的接口,我能够覆盖每个窗口类的 isDataValid() 函数,该函数决定下一个和上一个按钮是否应该调用卡片布局翻转函数。

源代码如下:

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();
}

然而。我想将此应用程序重做为模型-视图-控制器架构,这意味着我不再在窗口类(即视图)中有任何验证代码。因此,我将旧的 Window 类分为 WinView 类 (Win1) 和接口 (Interface1) 和模型来验证数据。

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 架构?我对我的下一个和上一个按钮目前的工作方式感到非常满意,是否有可能像现在一样仍然使用它们?

4

1 回答 1

0

我想说的第一件事是,据我了解 MVC 方法(我可能是错的),任何直接影响 View 本身行为方式的东西都应该保留在 View 中(比如启用/禁用按钮)。其他任何东西(功能方法)都应该由控制器处理。

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

于 2012-03-22T15:28:31.650 回答