为什么 jframe 没有被清除?

Why is the jframe not being cleared?

这很奇怪situation.I删除框架的内容并添加新内容。
但是正在添加 旧的 内容twice.Why 发生这种情况吗?
这可能看起来很长的代码,但其中大部分是为框架生成内容。
manager() 函数被 ActionListener 调用时,问题就开始了 addUserFrame()函数。

public class adminManager {
    private static Connection con;
    private static Statement stmt;
    private static JFrame f;
    private static JPanel p = new JPanel(); 
    private static JButton addUser,exitTest,addTest,editTest;   

    public static void manager(JFrame frame)
   {
    f=frame;f.setSize(400,800);
    p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
    Dimension d = new Dimension(100,30);
    addUser = new JButton("ADD USER");
    exitTest = new JButton("EXIT");
    addTest =  new JButton("ADD TEST");
    editTest = new JButton("EDIT TEST");
    p.add(addUser);p.add(addTest);
    p.add(editTest);p.add(exitTest);
    f.setTitle("Select Option");
    f.setLayout(new FlowLayout());
    f.add(p);   
    f.setVisible(true);     
    f.setResizable(false);
    f.setLocationRelativeTo(null);
    f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    bindEvents();
  }

    public static void addUserFrame(JFrame f)
   {        
    JPanel p1 = new JPanel();
    JPanel p2 = new JPanel();
    JPanel p3 = new JPanel();J
    Panel parentPanel = new JPanel();
    JLabel l1 = new JLabel("Enter User-Name");
    JLabel l2 = new JLabel("Enter Password");
    JTextField t1 = new JTextField(10);
    JTextField t2 = new JTextField(10);
    JButton btn = new JButton("Submit");
    JButton back = new JButton("Go Back");
    p1.setLayout(new FlowLayout(FlowLayout.CENTER));
    p2.setLayout(new FlowLayout(FlowLayout.CENTER));
    p3.setLayout(new FlowLayout(FlowLayout.CENTER));
    parentPanel.setLayout(new BoxLayout(parentPanel,BoxLayout.Y_AXIS));
    p1.setSize(200,100);
    p2.setSize(200,100);
    p3.setSize(200,100);
    p1.add(l1);p1.add(t1);
    p2.add(l2);p2.add(t2);
    p3.add(btn);p3.add(back);
    parentPanel.add(p1);parentPanel.add(p2);parentPanel.add(p3);
    f.add(parentPanel);
    back.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){             
            f.getContentPane().removeAll();f.repaint();
            manager(f);             
        }
    });

}

public static void bindEvents(){
    addUser.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            f.getContentPane().removeAll();
            f.repaint();    
            addUserFrame(f);
            f.setSize(400, 150);
            f.setTitle("Add User");
            f.setVisible(true);             
        }
    });
    }

public static void main(String []args){
    manager(new JFrame());
    }
}

更新:

通过将 p=new JPanel() 移动到 manager() 方法解决

f.getContentPane().removeAll();

您从框架中删除了面板 p,但在方法 manager() 中,您在添加新元素的位置添加了相同的 p。您必须在 p 上调用 clear() 或创建一个新面板 p

问题是正在使用同一个 JPanel。
通过将 p=new JPanel() 移动到 manager() 方法

来解决它