2011-11-23 50 views
5

我現在有一個SwingWorker發送HTTP請求,我重寫了SwingWorker的done()方法來更改JFrame中的內容。我想基本上刪除所有內容,並根據從服務器返回的值在JFrame上添加新成員面板。JFrame刪除JPanels並添加一個新的JPanel

現在,我面臨的問題是,當我在JFrame上調用以下方法時,它不會從JFrame中刪除任何內容,也不會更改它在Frame中包含的內容。

//TODO: Investigate why JFrame content pane won't repaint. 
f.removeAll(); 
//Pass the frame f reference only into MainDisplay, it doesn't actually do anything apart from allowing a class to add a JMenuBar on the JFrame. 
f.add(new MainDisplay(f)); 
f.getContentPane().invalidate(); 
f.getContentPane().validate(); 
f.getContentPane().repaint(); 

目前修復我已經是這個之下,但我寧願改變JFrame的內容而不是加載了新一輪上漲。

f.dispose(); 
f=new ApplicationFrame(); 

我已經通過,而調用重繪()重新繪製的JFrame在這裏和谷歌以前的答案和一些國家使用的validate()或無效()看着。

任何建議/幫助將不勝感激。

編輯:我想我會進行更多的調試,因爲肯定還有其他問題。

+0

有誰如果使用刪除或知道的removeAll刪除優越,面板/面板上一幀中的實際內存?或者它只是從框架中刪除它? – unleashed

+0

它只是從框架中刪除組件。這取決於JVM垃圾回收器「去除」內存......或者取消分配內存。 GC不保證在您的程序的整個生命週期中都會發生這種情況。另外,如果刪除的組件在程序中的其他地方被引用,那麼它們將不會被GC'd(除了在使用弱引用的特殊情況下)。 – Jason

回答

8

例如

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class MyFrame extends JFrame { 

    private static final long serialVersionUID = 1L; 

    public MyFrame() { 
     final JPanel parentPanel = new JPanel(); 
     parentPanel.setLayout(new BorderLayout(10, 10)); 

     final JPanel childPanel1 = new JPanel(); 
     childPanel1.setBackground(Color.red); 
     childPanel1.setPreferredSize(new Dimension(300, 40)); 

     final JPanel childPanel2 = new JPanel(); 
     childPanel2.setBackground(Color.blue); 
     childPanel2.setPreferredSize(new Dimension(800, 600)); 

     JButton myButton = new JButton("Add Component "); 
     myButton.addActionListener(new ActionListener() { 

      public void actionPerformed(ActionEvent e) { 
       parentPanel.remove(childPanel1); 
       parentPanel.add(childPanel2, BorderLayout.CENTER); 
       parentPanel.revalidate(); 
       parentPanel.repaint(); 
       pack(); 
      } 
     }); 
     setTitle("My Empty Frame"); 
     setLocation(10, 200); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     parentPanel.add(childPanel1, BorderLayout.CENTER); 
     parentPanel.add(myButton, BorderLayout.SOUTH); 
     add(parentPanel); 
     pack(); 
     setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       MyFrame myFrame = new MyFrame(); 
      } 
     }); 
    } 
} 
1

您試圖repaint()/validate()的contentPane。你有沒有試過JFrame? 您也可以嘗試JFrame#pack()

1

修改代碼

f.setContentPane(new MainDisplay(f)); 
f.getContentPane().invalidate(); 
f.getContentPane().validate(); 
f.getContentPane().repaint(); 
+2

嗨isograph 你在這裏發佈的代碼和你修改的代碼之間的區別只是'f.removeAll()'這一行。如果您認爲這會對代碼產生重大影響,請考慮擴展您的答案以解釋原因。如果您只是刪除了該行,因爲這是不必要的,也許請留下問題的評論。答案應該提出問題的解決方案。評論更適合於提出對問題或其他答案的增強。 – Crippledsmurf

0

的您可以嘗試使用Frame.pack()再次它爲我工作。或者嘗試一個OD那些下面的方法:

Frame.setOpaque(false); 
Frame.setEnabled(false); 
Frame.setVisible(false); 
Frame.removeAll(); 
+0

希望能幫助別人,那段時間對我來說非常有用 – AXL