用另一个jpanel替换一个jpanel [英] replacing one jpanel with another jpanel

查看:193
本文介绍了用另一个jpanel替换一个jpanel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想替换当前面板并以JPanel形式调用另一个面板. 我尝试使用setContentPane()getContentPane()方法执行操作,但是它给出了错误. 我怎样才能做到这一点.... 我也尝试这样做,但清除所有组件,但不添加任何内容

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

推荐答案

我想替换当前面板并以JPanel形式调用另一个面板"

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

使用CardLayout而不是尝试添加移除面板.要查看您如何使用Netbeans GUI Builder,请参阅如何在Netbeans GUI Builder中使用CardLayout . CardLayout的作用是允许您在不同的视图之间进行更改,而无需添加和放置面板,这可能很麻烦.

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.

此外,您可能想调试if语句.很难仅显示少量代码.

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

这篇关于用另一个jpanel替换一个jpanel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆