繁体   English   中英

如何更改子卡中的cardLayout卡?

[英]How can I change cardLayout card within child card?

我有一个父类是CardLayout。 我也有两个具有文本字段和按钮的子类(JPanel)。 我想要的是,当用户单击第一张卡片上的按钮时,屏幕将切换到第二张屏幕。 这是代码

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

}

一般来说,您不应该这样做。 当前的“卡片”应该不知道导航的顺序,相反,它应该能够提供某种事件通知(例如通过ActionListener ),或者告诉其他管理者下一次需要切换它/上一个/目标“卡片” ...

话虽如此,您“可以”从“卡的”父级中提取布局管理器

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

但是,当导航顺序更改时,这将不会给您带来任何麻烦,最好使用某种“导航”管理器,该管理器可以从中央控制器为您解决。

正如@nachokk指出的那样,此(控制器)将代表中介者模式

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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