2015-08-03 99 views
0

嘿傢伙即時通訊發民的應用程序讀取目錄和子目錄和文件,所以這個工作完全如何顯示不同勢線在同一個JLabel在Java中

/* 
* To change this license header, choose License Headers in Project Properties. 
* To change this template file, choose Tools | Templates 
* and open the template in the editor. 
*/ 

package avd; 

import java.io.File; 



/** 
* 
* @author USER 
*/ 
public class arborescence { 
    private String initialpath = ""; 
    private Boolean recursivePath = false; 
    public int filecount = 0; 
    public int dircount = 0; 

/** 
* Constructeur 
* @param path chemin du répertoire 
* @param subFolder analyse des sous dossiers 
*/ 
    public arborescence(String path, Boolean subFolder) { 
     super(); 
     this.initialpath = path; 
     this.recursivePath = subFolder; 
    } 

    public void list() { 
     this.listDirectory(this.initialpath); 
    } 

    private void listDirectory(String dir) { 
     File file = new File(dir); 
     File[] files = file.listFiles(); 
     if (files != null) { 
      for (int i = 0; i < files.length; i++) { 
       if (files[i].isDirectory() == true) { 
        System.out.println("Dossier: " + files[i].getAbsolutePath()); 
        FirstWindow.lblRepertoire.setText(files[i].getAbsolutePath()); 
        this.dircount++; 
       } else { 
        System.out.println(" Fichier: " + files[i].getName()); 
        FirstWindow.lblFile.setText(files[i].getAbsolutePath()); 
        this.filecount++; 
       } 
       if (files[i].isDirectory() == true && this.recursivePath == true) { 
        this.listDirectory(files[i].getAbsolutePath()); 
       } 
      } 
     } 
    }  
} 

,並在主類我已經這個

public void scanfiles(){ 
String pathToExplore = fileName; 
arborescence arbo = new arborescence(pathToExplore, true); 
    Long start = System.currentTimeMillis(); 
    arbo.list(); 
    System.out.println("----------"); 
    System.out.println("Analyse de " + pathToExplore + " en " + (System.currentTimeMillis() - start) + " mses"); 
    lbltime.setText(Integer.toString((int) (System.currentTimeMillis() - start))); 
    System.out.println(arbo.dircount + " dossiers"); 
    lblCountRep.setText(Integer.toString(arbo.dircount)); 
    System.out.println(arbo.filecount + " fichiers"); 
    lblCountFiles.setText(Integer.toString(arbo.filecount)); 
} 

這是我的框架 enter image description here

,在標籤「劇目」它讓我看到的問題,最後一個我想它SHO當我的每一個人像防病毒軟件掃描你的文件時一樣,你會看到它掃描的是什麼,我們可以做到這一點?

+2

一般情況下,你可以使用HTML格式設置應用到文本,對於[示例](http://stackoverflow.com/questions/29550524/jlabel-with-multiple -lines-and-alignment-the-right/29551195#29551195)和[示例](http://stackoverflow.com/questions/14737810/jlabel-show-longer-text-as-multiple-lines/14738193# 14738193) – MadProgrammer

+0

@MadProgrammer取決於[''Document'' type](http://docs.oracle.com/javase/7/docs/api/javax/swing/text/html/HTMLDocument.html),但是的,基本上你是對的 –

+0

@BinkanSalaryman沒有,簡單得多,那只是' This
is
some
text' - 自從Java 1.3開始工作:) – MadProgrammer

回答

0

This是一個示例如何做到這一點。不知道你是否也在尋找相同的東西。其實html爲你做的東西。

JLabel label = new JLabel("<html>First line and maybe second line</html>"); 

讓您的JLabel伸展通過適當的佈局。可能是網格包佈局。它應該做的伎倆。

+0

除非你用它作爲你自己的答案的基礎,否則鏈接到其他人的答案是不鼓勵的,這更適合於評論 – MadProgrammer

+0

@MadProgrammer很有意義 –

+0

除了從原始答案複製,它不會「真的「回答的問題,打破了多行內容 – MadProgrammer

2

確保listDirectory()方法在單獨的線程中運行。標籤setText調用應該移動到EDT(使用SwingUtilities.invokeLatter)。

問題是EDT正在爲迭代文件工作,因此它沒有機會重新繪製標籤。

// call this method in separate thread , not in EDT 
private void listDirectory(String dir) { 
     File file = new File(dir); 
     File[] files = file.listFiles(); 
     if (files != null) { 
      for (int i = 0; i < files.length; i++) { 
       if (files[i].isDirectory() == true) { 
        System.out.println("Dossier: " + files[i].getAbsolutePath()); 
        FirstWindow.lblRepertoire.setText(files[i].getAbsolutePath()); // call in invoke latter 
        this.dircount++; 
       } else { 
        System.out.println(" Fichier: " + files[i].getName()); 
        FirstWindow.lblFile.setText(files[i].getAbsolutePath()); // call in invoke latter 
        this.filecount++; 
       } 
       if (files[i].isDirectory() == true && this.recursivePath == true) { 
        this.listDirectory(files[i].getAbsolutePath()); 
       } 
      } 
     } 
    }  

編輯:

scanButton.addActionListener(new ActionListener() { 
public void actionPerformed(ActionEvent e) { 
Thread scanThread = new Thread() { 
    public void run() { 
    scanFiles(); 
    } 
    }; 
    scanThread .start(); 
    } 
    }); 
+0

我沒有得到答案我必須改變這種方法,我把它在其他類中移動?這是你的意思? –

+1

不,我添加了一個示例代碼,然後將在新線程中調用scanFiles方法。但是當你再次更新GUI組件時,你必須在EDT中完成。去一些很好的教程,你會理解這個概念 – hunter

相關問題