Така. Направих заявка. Той има mainFrame, който създава два панела, единият съдържа моите подобни на съветника бутони (напр. Next, Previous) и един cardLayout, който ми позволява да отида до следващия или предишния панел, който декларирам в mainFrame. Като имам интерфейс, Verifiable, мога да отменя всеки клас прозорец с функция isDataValid(), която решава дали бутоните next и prev трябва да извикват функции за обръщане на cardlayouts.
Изходният код може да се види по-долу:
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) и модел за проверка на данните.
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 архитектурата? Много съм доволен от това как работят моите следващ и предишен бутон в момента, възможно ли е по някакъв начин да ги използвам, както правя в момента?