replacing one jpanel with another jpanel

萝らか妹 提交于 2020-01-07 03:51:29

问题


I want to replace the current panel and call another panel in JPanel forms. I try to do using setContentPane() and getContentPane() method but it gives error. how can I do that.... I also try this but clear all the componens but do not add anything

private void loginButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            
    try {
        if (new ConnectionFactory().userLoginCheck(usernameText.getText(), new String(passwordText.getPassword()))) {
            removeAll();   
            add(new ChangeUsernamePassword());
            revalidate();
            repaint();
             //new Welcomeboard();
        } else {
            warningLabel.setText("Invalid Username Or Password!!!");
        }
    } catch (ClassNotFoundException ex) {
        Logger.getLogger(DashboardPanel.class.getName()).log(Level.SEVERE, null, ex);
    } catch (SQLException ex) {
        Logger.getLogger(DashboardPanel.class.getName()).log(Level.SEVERE, null, ex);
    }
}                                           

回答1:


"I want to replace the current panel and call another panel in JPanel forms"

Instead of trying to add an remove panels, use a CardLayout. Seeing how you're using Netbeans GUI Builder, see How to Use CardLayout with Netbeans GUI Builder. What the CardLayout does is allow you to change between different views without having to add and drop panels, which can be troublesome.

Also you may want to debug your if statement. Hard to tell with only the little but of code you're showing.



来源:https://stackoverflow.com/questions/22735049/replacing-one-jpanel-with-another-jpanel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!