更新:问题是我希望我的上一个按钮只有在我以前去过那个按钮时才能工作。例如,如果我登录并单击下一步,我应该能够后退一步。但不是两个。
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(200, 200));
Window1 win1 = new Window1();
contentPane.add(win1);
Window2 win2 = new Window2();
contentPane.add(win2);
Window3 win3 = new Window3();
contentPane.add(win3);
JPanel buttonPanel = new JPanel();
final JButton previousButton = new JButton("< PREVIOUS");
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) {
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.previous(contentPane);
}
}
});
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);
}
}
});
cancelButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
System.exit(0);
}
});
frame.add(contentPane);
frame.add(buttonPanel, BorderLayout.PAGE_END);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
} }
Window1.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JPanel;
public class Window1 extends JPanel implements Verifiable {
JTextField txtUsername = new JTextField();
JPasswordField txtPassword = new JPasswordField();
public Window1() {
init();
}
private void init() {
setLayout(new GridLayout(2, 2));
JLabel lblUsername = new JLabel("Username:", JLabel.CENTER);
txtUsername = new JTextField();
JLabel lblPassword = new JLabel("Password:", JLabel.CENTER);
txtPassword = new JPasswordField();
add(lblUsername);
add(txtUsername);
add(lblPassword);
add(txtPassword);
String title = "Log in";
setBorder(BorderFactory.createTitledBorder(title));
}
@Override
public boolean isDataValid() {
if(txtUsername.getText().equals("foo") &&
java.util.Arrays.equals(txtPassword.getPassword(), "bar".toCharArray())) {
return true;
} else {
JOptionPane.showMessageDialog(this, "Fel användarnamn och/eller lösenord",
"Error", JOptionPane.ERROR_MESSAGE);
return false;
}
}
}