2016-12-21 27 views
-4

這是一個奇怪的情況。我刪除一個框架的內容並添加新的內容。
內容正在被添加兩次。爲什麼會發生這種情況?
這可能看起來很長代碼,但大部分代碼都是爲框架生成內容。
的問題,因爲我看到啓動時經理()功能正在從addUserFrame()功能的ActionListener調用。爲什麼jframe不被清除?

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

enter image description here

enter image description here

enter image description here

UPDATE:

通過移動p=new JPanel()解決成manager()方法

+1

(1)解決了這個問題,我相信你不會對每一行多條語句使代碼無法閱讀和維護編寫代碼。所以不要指望我們在論壇上閱讀這樣的代碼。發佈格式正確的代碼。 – camickr

+0

@camickr done.now讓我們看看你是否可以回答它 – Pradeep

+0

另外,遵守Java語言約定:類名應以大寫字母開頭。 – RealSkeptic

回答

4
f.getContentPane().removeAll(); 

你從幀但在manager()添加相同p您添加新元素的方法除去面板p。您必須撥打電話clear()p或創建一個新面板p

+0

舊內容正在顯示兩次 – Pradeep

+0

@Pradeep請將您的問題中的代碼增強到[MCVE](http://stackoverflow.com/help/mcve) –

0

問題是正在使用相同的JPanel。
通過移動p=new JPanel()manager()方法