Change content pane on button press

一曲冷凌霜 提交于 2019-12-12 03:03:27

问题


I want a button to change the contentPane in a frame. An admin panel and a user panel in two different JPanel classes that gets called in a MainWindow class with a frame.

My code looks as follows:

The method the button calls:

public void ChangeContent() throws Exception{
    MainWindow mw = new MainWindow();
    AdminUI admin = new AdminUI ();
    mw.frame.getContentPane().removeAll();
    mw.frame.setContentPane(admin);
    mw.frame.revalidate();
    mw.frame.repaint();

}

This is the method being called in my "public MainWindow()" method in the MainWindow class (the one with the frame) to show the contentPane in the beginning:

public void Admin() throws Exception {
        frame = new JFrame("Admin Panel");
        frame.setResizable(false);
        frame.setBounds(100, 100, 256, 457);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        AdminUI Admin = new AdminUI();
        frame.setContentPane(Admin);
    }

An equal method exists for the userUI.

I have tried just called the userUI() method in the MainWindow when I press a button, but that doesn't refresh the window, and neither does the ChangeConten() method. I need some help. I used the search feature on Stackoverflow to no success.

来源:https://stackoverflow.com/questions/16079886/change-content-pane-on-button-press

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