0

更新:问题是我希望我的上一个按钮只有在我以前去过那个按钮时才能工作。例如,如果我登录并单击下一步,我应该能够后退一步。但不是两个。

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;
        }
    }
}
4

1 回答 1

1

例如,使用布尔方法创建一个接口 Verifiable isDataValid(),:

interface Verifiable {
   boolean isDataValid();
}

并让您的面板类实现接口,以便每个面板都可以验证自己的数据。

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

      JLabel lblPassword = new JLabel("Password:", JLabel.CENTER);

      add(lblUsername);
      add(txtUsername);
      add(lblPassword);
      add(txtPassword);
      String title = "Use \"foo\" and \"bar\"";
      setBorder(BorderFactory.createTitledBorder(title ));
   }

   @Override
   public boolean isDataValid() {
      return txtUsername.getText().equals("foo") && 
            java.util.Arrays.equals(txtPassword.getPassword(), "bar".toCharArray());
   }
}

然后检查当前显示面板的数据在下一个和上一个 JButton 的 ActionListener 中是否有效。

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

     }
  });
于 2012-03-11T19:45:20.167 回答