简体   繁体   中英

How can I change cardLayout card within child card?

I have a parent class which is CardLayout. I also have two child classes(JPanel) that has textfields and buttons. What I want is when the user click a button on the first card the screen will change to the second screen. Here is the code

public class GUI extends JFrame {
/**
 * 
 */
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private static LoginScreen login;
private static DatabaseSelection selector;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                GUI frame = new GUI();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }); 
}

/**
 * Create the frame.
 */
public GUI() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    setLocationRelativeTo(null);
    contentPane = new JPanel();
    CardLayout card = new CardLayout();
    contentPane.setLayout(card);
    login = new LoginScreen();
    selector = new DatabaseSelection();
    contentPane.add(login, "1");
    contentPane.add(selector, "2");
    setContentPane(contentPane);
    card.show(contentPane, "1");
}

}

public class LoginScreen extends JPanel implements ActionListener {
/**
 * 
 */
private static final long serialVersionUID = 1L;
private JTextField userName;
private JPasswordField passWord;
private JTextField port;
private JTextField host;
private JLabel hostLabel;
private JLabel portLabel;
private Connection con;

/**
 * Create the panel.
 */
public LoginScreen() {
    GridBagLayout gridBagLayout = new GridBagLayout();
    setLayout(gridBagLayout);

    JLabel userNameLabel = new JLabel("Username: ");
    GridBagConstraints gbc_userNameLabel = new GridBagConstraints();
    gbc_userNameLabel.anchor = GridBagConstraints.EAST;
    gbc_userNameLabel.insets = new Insets(0, 0, 5, 5);
    gbc_userNameLabel.gridx = 1;
    gbc_userNameLabel.gridy = 1;
    add(userNameLabel, gbc_userNameLabel);

    userName = new JTextField();
    GridBagConstraints gbc_userName = new GridBagConstraints();
    gbc_userName.anchor = GridBagConstraints.EAST;
    gbc_userName.insets = new Insets(0, 0, 5, 5);
    gbc_userName.gridx = 2;
    gbc_userName.gridy = 1;
    add(userName, gbc_userName);
    userName.setColumns(10);

    JLabel passWordLabel = new JLabel("Password: ");
    GridBagConstraints gbc_passWordLabel = new GridBagConstraints();
    gbc_passWordLabel.anchor = GridBagConstraints.EAST;
    gbc_passWordLabel.insets = new Insets(0, 0, 5, 5);
    gbc_passWordLabel.gridx = 1;
    gbc_passWordLabel.gridy = 2;
    add(passWordLabel, gbc_passWordLabel);

    passWord = new JPasswordField();
    GridBagConstraints gbc_passWord = new GridBagConstraints();
    gbc_passWord.fill = GridBagConstraints.HORIZONTAL;
    gbc_passWord.insets = new Insets(0, 0, 5, 5);
    gbc_passWord.gridx = 2;
    gbc_passWord.gridy = 2;
    add(passWord, gbc_passWord);

    JButton loginButon = new JButton("Login");
    loginButon.setFont(new Font("Tahoma", Font.BOLD, 16));
    GridBagConstraints gbc_loginButon = new GridBagConstraints();
    gbc_loginButon.anchor = GridBagConstraints.CENTER;
    gbc_loginButon.fill = GridBagConstraints.VERTICAL;
    gbc_loginButon.gridheight = 4;
    gbc_loginButon.gridx = 3;
    gbc_loginButon.gridy = 1;
    add(loginButon, gbc_loginButon);
    loginButon.addActionListener(this);

    hostLabel = new JLabel("Host: ");
    GridBagConstraints gbc_hostLabel = new GridBagConstraints();
    gbc_hostLabel.anchor = GridBagConstraints.EAST;
    gbc_hostLabel.insets = new Insets(0, 0, 5, 5);
    gbc_hostLabel.gridx = 1;
    gbc_hostLabel.gridy = 3;
    add(hostLabel, gbc_hostLabel);

    host = new JTextField();
    host.setText("localhost");
    GridBagConstraints gbc_host = new GridBagConstraints();
    gbc_host.insets = new Insets(0, 0, 5, 5);
    gbc_host.fill = GridBagConstraints.HORIZONTAL;
    gbc_host.gridx = 2;
    gbc_host.gridy = 3;
    add(host, gbc_host);
    host.setColumns(10);

    portLabel = new JLabel("Port:");
    GridBagConstraints gbc_portLabel = new GridBagConstraints();
    gbc_portLabel.anchor = GridBagConstraints.EAST;
    gbc_portLabel.insets = new Insets(0, 0, 0, 5);
    gbc_portLabel.gridx = 1;
    gbc_portLabel.gridy = 4;
    add(portLabel, gbc_portLabel);

    port = new JTextField();
    port.setText("3306");
    GridBagConstraints gbc_port = new GridBagConstraints();
    gbc_port.insets = new Insets(0, 0, 0, 5);
    gbc_port.fill = GridBagConstraints.HORIZONTAL;
    gbc_port.gridx = 2;
    gbc_port.gridy = 4;
    add(port, gbc_port);
    port.setColumns(10);
    setFocusTraversalPolicy(new FocusTraversalOnArray(new Component[]{userName, passWord, host, port, loginButon}));
}

public void actionPerformed(ActionEvent e) {
    try {
        ConnectionManager conMgr = new ConnectionManager(host.getText(), port.getText());
        char[] pass = passWord.getPassword();
        con = conMgr.getConnection(userName.getText(), new String(pass));
    } catch (ClassNotFoundException e1) {
        System.out.println("Driver Error ...");
        //e1.printStackTrace();
    } catch (SQLException e1){
        System.out.println(e1.getMessage());
    }
}

}

Generally speaking, your shouldn't. The current "card" should have no idea about the order of navigation, instead, it should be capable of either providing some kind of event notification (such as through an ActionListener ) or tell some other manager that it needs to be switched for the next/previous/target "card"...

Having said that, you "could" extract the layout manager from the "card's" parent

LayoutManager layout = getParent().getLayout();
if (layout instanceof CardLayout) {
    CardLayout cardLayout = (CardLayout)layout;
    // switch panels...
}

But this will cause you no end of problems when the order of navigation changes, better to use some kind of "navigation" manager which can take care of it for your from a central controller.

As @nachokk has pointed out, this (controller) would represent a Mediator Pattern

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM