Java 将cardlayout应用程序重做到MVC体系结构中
所以。我已经申请了。它有一个大型机,可以创建两个面板,一个保存着与向导类似的按钮(例如下一个、上一个)和一个cardLayout,使我能够转到我在大型机中声明的下一个或上一个面板。通过有一个可验证的接口,我能够覆盖每个窗口类a isDataValid()-函数,该函数决定下一个和上一个按钮是否应该调用cardlayouts flip函数 源代码如下所示:Java 将cardlayout应用程序重做到MVC体系结构中,java,swing,model-view-controller,user-interface,cardlayout,Java,Swing,Model View Controller,User Interface,Cardlayout,所以。我已经申请了。它有一个大型机,可以创建两个面板,一个保存着与向导类似的按钮(例如下一个、上一个)和一个cardLayout,使我能够转到我在大型机中声明的下一个或上一个面板。通过有一个可验证的接口,我能够覆盖每个窗口类a isDataValid()-函数,该函数决定下一个和上一个按钮是否应该调用cardlayouts flip函数 源代码如下所示: public class MainFrame { private static void createAndShowGUI() {
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();
}
到目前为止,我的主机看起来是一样的。当我试图规划我的新架构时,我陷入了困境。我应该如何绑定MVC架构?我对我的下一个和上一个按钮目前的工作方式非常满意,是否有可能像我现在这样使用它们?我想说的第一件事是,就我所理解的MVC方法论而言(我可能是错的),任何直接影响视图本身行为的东西都应该保留在视图中(如启用/禁用按钮)。任何其他(功能方法)都应由控制器处理
我会使用。有一个名为
MainController
的类,并在该类上使用单例模式。然后在MainView
中,有一个名为controller
的MainController
实例的全局变量。然后在控制器中有一个isDataValid(字符串用户名、字符串密码)的方法
并调用boolean valid=controller.isDataValid(txtsername.getText(),新字符串(txtPassword.getPassword())
。任何功能方法都可以做同样的事情。我还想指出,控制器是唯一应该连接到数据库的MVC方法。我希望这会有所帮助。好吧,这是否也适用于我的向导的想法,即使用相同的外部GUI,只使用我的汽车更改内容dPanel?我喜欢我现在的简单方式,但我知道我最终需要迁移到MVC,所以我最好现在就这样做。我的向导基本上应该是这样工作的:1.单击“下一步”时登录,后面跟着一个检查程序。2.单击“下一步”时检查程序会从用户那里输入一些内容。等等,因为用户应该只能键入一些内容数据,然后单击“下一步”和“上一步”,我想让它尽可能简单。肯特:谢谢你的反馈,我真的很感激。我是GUI编程新手。(上次评论中有字符限制)我明白你的意思了。嗯……你可以试着在JPanel所需的组件上设置getter,并在主视图中处理所有的按钮点击。因此,设置一个变量来跟踪哪个面板启动,当单击下一个按钮时,调用组件的getter,获取处理用户输入所需的信息,然后将该信息发送到要向控制器处理的信息。肯特:嗯,好的。你介意展示一些你的意思是什么源代码吗?在我的大型机中,这是否仍然适用于相同的底层GUI(即contentPane和三个按钮)?由于我一直在努力想出一个好的例子,我不断得出结论,有很多方法可以做到这一点。因此我想我将把它留给你。这里有一些建议:1.有一张卡片列表/地图(JPanels)每次按下“下一步”按钮时,都会在列表中循环查找可见的卡并对其执行所需操作。2.设置一个名为currentCard
的变量,并在每次单击按钮时对其进行设置,以便您知道下次单击按钮时要执行的操作。3.有更多方法。只要做最适合您的事情。如果您需要我更具体一点,让我知道。
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() {
}
}