简体   繁体   English

如何用另一个JPanel替换当前的JPanel?

[英]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. 当我按Admin按钮时,我试图从同一JPanelUserAdminPanel过渡到AdminLogin

UserAdmin Panel UserAdmin面板

在此处输入图片说明

transit to AdminLogin Panel 过渡到AdminLogin面板

在此处输入图片说明

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 这是我的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 这是AdminLogin面板的代码

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 . 我看了以下示例和该示例,但是它不是很适用,因为它谈论CardLayout而不是像重写当前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. 用您所说的话, UserAdminPanel是您的主面板。 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 . 方法getNewPanel()将返回基础JPanel I'm assuming that AdminModule has a reference to the AdminLoginPanel . 我假设AdminModule具有对参考AdminLoginPanel

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

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