简体   繁体   中英

How to replace current JPanel with another JPanel?

I am trying to transit from a UserAdminPanel to AdminLogin within the same JPanel when I press the Admin button.

UserAdmin Panel

在此处输入图片说明

transit to AdminLogin Panel

在此处输入图片说明

The problem I have now is that I am opening up a new panel instead of changing the current panel to the new panel.

This is my code for the UserAdminPanel

public class SelectAdminUserPanel extends JPanel 
{
    public SelectAdminUserPanel()
    {
        setLayout(new GridLayout(3,1));
        JButton b1 = new JButton("User Login");
        JButton b2 = new JButton("Admin Login");
        JButton b3 = new JButton("Exit");

        b1.addActionListener(new  SelectUserButtonListener() );
        b2.addActionListener(new SelectAdminButtonListener());
        b3.addActionListener(new SelectExitButtonListener() );

        add(b1);
        add(b2);
        add(b3);
    }

    private class SelectAdminButtonListener implements ActionListener
    {

        public void actionPerformed(ActionEvent event)
        {
            AdminModule am = new AdminModule();

              am.run();   
        }
    }

    private class SelectUserButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent event)
        {
              GameModule gm = new GameModule();
              gm.run(); 
        }
    }

    private class SelectExitButtonListener implements ActionListener
    {
        public void actionPerformed (ActionEvent event)
        {

        }
    }
}

This is the code for the AdminLogin Panel

public class AdminLoginPanel extends JPanel
{
    AdminLoginPanel()
    {         
        JLabel pwlabel = new JLabel("Password");
        JPasswordField pwfield = new JPasswordField(20);
        JButton loginbutton = new JButton("Login");

        add(pwlabel);
        add(pwfield);
        add(loginbutton);
     }
}

I have looked at the following example and this example but it's not very applicable because it talks about CardLayout instead of like rewriting the current JPanel .

I think that you should have a reference to your main frame and just remove the components from it based on the button pressed and add only the required components. From what you say, UserAdminPanel is your main panel. I think it's added to a frame for which you can obtain a reference. When you click a button, you want to remove all the content shown on it and display only what the button clicked should show. I think it should look something like this:

private class SelectAdminButtonListener implements ActionListener {

        public void actionPerformed(ActionEvent event) {
             frame.getContentPane().removeAll();
             AdminModule am = new AdminModule();
             frame.add(am.getNewPanel());
             frame.pack();                    
             // am.run();   //it's not clear what does for you
        }

}

Where the method getNewPanel() would return the underlying JPanel . I'm assuming that AdminModule has a reference to the AdminLoginPanel .

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