JLabel

2016-04-01 24 views
0
için 0txt satırları nasıl kullanılır

address için düzeltilen bağlantıdan ilk 10 satırı almam gerekir, böylece bunları bir GUI için JLabel'e yerleştirebilirim. line.split("\\s+") ile ayrıldım, ancak bir süre döngü içinde kullanıyorum çünkü ilk 10 satırını Etiket'e nasıl taşıyacağımı anlayamıyorum.JLabel

try{ 
       String address = "https://www.cia.gov/library/publications/the-world-factbook/rankorder/rawdata_2151.txt"; 
       URL pageLocation = new URL(address); 
       Scanner in = new Scanner (pageLocation.openStream()); 
       String line = ""; //intialize 
       int i = 0; 

       while(i < 10){ //add limit for 10 
        line = in.nextLine(); //take line 
        String[] content = line.split("\\s+"); //seperate by multiple spaces 
        i++; //count another country 
       } 

       } 
       catch (IOException exception){ 
        System.out.println("File not Found"); 
       } 
+0

Metni html olarak sarmayı denediniz mi? [Örnek] için (http://stackoverflow.com/questions/29550524/jlabel-with-multiple-lines-and-alignment-to-the-right/29551195#29551195) – MadProgrammer

+0

Sorunu anladığımdan emin değilim. Her satırı yeni bir dizi konumuna atamak yerine dizeyi birleştirmeyi denediniz mi? Ayrıca bir “JTextPane” veya “JTextArea” kullanmayı deneyin. –

cevap

0

Çözümüm, döngü dışındaki diziyi oluşturup bölme satırlarını ona ekler.

try { 
     String address = "https://www.cia.gov/library/publications/the-world-factbook/rankorder/rawdata_2151.txt"; 
     URL pageLocation = new URL(address); 
     Scanner in = new Scanner(pageLocation.openStream()); 
     String line = ""; // intialize 
     int i = 0; 
     String[] panelContent = new String[10]; 
     while (i < 10) { // add limit for 10 
      line = in.nextLine(); // take line 
      // seperate by multiple spaces, but must be more than one 
      // because some countrys have composed names like United States 
      String lineContent[] = line.split("\\s\\s+"); 
      panelContent[i] = String.format("%s %s %s", lineContent[0], lineContent[1], lineContent[2]); 
      i++; // count another country 
     } 
     // Here you have de content to populate de JLabel 
     System.out.println(Arrays.toString(panelContent)); 

    } catch (IOException exception) { 
     System.out.println("File not Found"); 
    } 
İlgili konular