2012-05-21 23 views
1

可能重複:
When does one need to call revalidate() on a swing component to make it refresh, and when not?刷新幀的內容實時

我想提出一個資源管理器程序在Java中它可以作爲從用戶如下
我輸入的路徑,在用戶按下輸入路徑之後的文本框被設置並且計算該文件夾的列表並且創建相應的標籤,該標籤應該被顯示在窗口
上,但是該應用程序不顯示新的內容的文件夾,如果我改變文本,那麼它會直接進入一個新的目錄,所以當我按下回車鍵時,它必須顯示新目錄的內容,但只有在調整窗口大小後才顯示框架的新內容

代碼如下

package explorer; 
import java.io.*; 
import java.util.*; 
import javax.swing.*; 
import java.awt.event.*; 
import java.awt.*; 

public class Explorer extends JFrame{ 
    File file; 
    Scanner scan; 
    String path; 
    String [] listOfFiles; 
    JTextField txtPath; 
    JLabel lblLocation; 
    JLabel child[]; 
    JPanel childPanel; 
    JPanel masterPanel; 

     public Explorer(){ 
     lblLocation = new JLabel("Location: "); 
     /* 
     * the declaration of panels 
     */ 
      masterPanel = new JPanel(); 
      childPanel = new JPanel(); 
     JPanel panel = new JPanel(); 

     /*declaration of other components*/ 

     txtPath = new JTextField("",20); 
     /*addition of components to panel for layout*/ 
     panel.add(lblLocation); 
     panel.add(txtPath); 
     /*adding to master panel, for sophisticated layout*/ 
     masterPanel.add(panel, BorderLayout.NORTH); 
     masterPanel.add(childPanel, BorderLayout.SOUTH); 

     getContentPane().add(masterPanel); 
     /*this place from where address is fetched like /home/revolution/Desktop etc on ubuntu*/ 

     txtPath.addActionListener(new ActionListener(){ 
     public void actionPerformed(ActionEvent ev){ 
      childPanel.removeAll(); 
     path = new String(txtPath.getText());//the absolute path 
     file = new File(path); 
     File childFiles[]; 
     String name = path.substring(path.lastIndexOf('/')+1, path.length());//the name of the directory being displayed 
     setTitle(name); 

      if(!file.isDirectory()){ 
      JOptionPane.showMessageDialog(null, "Error file is not a directory"); 
      } else { 
      listOfFiles = file.list(); 
      child = new JLabel[listOfFiles.length];// labels equal to the number fo files and with name of the coresponding file/folder 

      childFiles = new File[listOfFiles.length];//references to files 
      childPanel.setLayout(new GridLayout(listOfFiles.length/2,listOfFiles.length/2));//setting grid layout 

      for(int i=0; i<listOfFiles.length;i++){ 
       childFiles[i] = new File(listOfFiles[i]); 
       child[i] = new JLabel(listOfFiles[i]); 
       child[i].setToolTipText(childFiles[i].isFile()?"File":"Folder"); 
       childPanel.add(child[i]); 
      } 
      childPanel.setVisible(true); 
      } 

     } 
     }); 
} 
} 

有什麼不對?我怎樣才能'刷新'窗口的內容?

+0

使用重新驗證()方法重置基於新組件列表上。 – Sajmon

+0

revalidate()方法的作用是什麼?我沒有在任何書中遇到過它..... – lucifer

+0

@lucifer看看[this](http://stackoverflow.com/questions/5769813/when-does-one-need-to-call-重新驗證它的組件 - 使它刷新) –

回答

1

我想你需要revalidate()面板。

更精確地在你的動作監聽結束時,您可以添加childPanel.revalidate();

 childPanel.setVisible(true); 
     } 
    } 
    childPanel.revalidate(); 
}); 
+1

由於他正在刪除組件,他還必須在包含的JPanel上調用'repaint()'。 –

+0

但是由於他只是在更換子面板,因此子面板上的「重新驗證」應該處理所有事情,因爲整個子面板都會重新繪製* –

+0

不,如果他也在移除組件 - 他就是這樣。注意第一個調用'actionPerformed(...)'方法 - 'childPanel.removeAll();' –