2016-06-01 12 views
0

Ich möchte die Zeichenfolge in der TextArea überschreiben, ich bin vertraut mit dem SetText (String), aber es scheint nicht zu funktionieren. Ich möchte, dass die textArea die txt-Dateien nach den Angaben des Benutzers liest. Mein Code ist wie folgt:Overwrite TextArea

first class: 

    public String getUsuario(){ 

     return txtUsuario.getText(); 
    } 
} 

second class: 

public class PanelResultado extends JPanel 
{ 

    private JLabel lblMostrar; 
    private JScrollPane scrollPane; 
    private JTextArea textArea; 
    private PanelUsuario panelUsuario; 
    private InterfazMilkyWay principal; 
    private PanelResultado resultado; 

    /** 
    * Constructor for objects of class PanelInfo 
    */ 
    public PanelResultado() 
    { 
     this.setLayout(null); 

     textArea = new JTextArea(""); 
     textArea.setEditable(false); 
     textArea.setBorder(BorderFactory.createLineBorder(Color.gray)); 

     panelUsuario = new PanelUsuario();  

     //JScrollPane scrollPane = new JScrollPane(textArea); // le pone un scrollPane al txtArea 

     lblMostrar = new JLabel("Resultado;"); 
     lblMostrar.setBounds(0,0,385,30); 
     textArea.setBounds(0,30,440,110); 
     this.setBackground(Color.WHITE); 

     add(lblMostrar); 
     add(textArea); 
     //add(scrollPane, BorderLayout.CENTER); 




    } 
    public void mostrar(){ 
     String tema = panelUsuario.getUsuario(); 
     String texto = ""; 
      switch (PlanetaActual.planetaActual){ 


      case 0: 
      textArea.setText("holaaaa"); 
       if (tema.equals("temperatura") || tema.equals("Temperatura")){ 

        System.out.println("temperatura"); 

        texto = ""; 
        try { 
         Scanner scanner = new Scanner(new File("temperatura mercurio.txt")); 

         while (scanner.hasNext()) { 
          // mientras el scanner tenga otra linea 
          texto += scanner.hasNext(); 


         } 
         textArea.setText(texto); 
        } catch (FileNotFoundException e) { 

         texto = "El archivo no se encuentra"; 
        } 
        textArea.setText(texto); 
       } 
       else if (tema.equals("posicion") || tema.equals("Posicion")|| tema.equals("Posición")){ 
          texto = ""; 
        try { 
         Scanner scanner = new Scanner(new File("posicion mercurio.txt")); 

         while (scanner.hasNext()) { 
          // mientras el scanner tenga otra linea 
          texto += scanner.hasNext(); 

         } 
        } catch (FileNotFoundException e) { 
         texto = "El archivo no se encuentra"; 
        } 
        textArea.setText(texto); 

       }else if (tema.equals("gravedad") || tema.equals("Gravedad")){ 
          texto = ""; 
        try { 
         Scanner scanner = new Scanner(new File("gravedad mercurio.txt")); 

         while (scanner.hasNext()) { 
          // mientras el scanner tenga otra linea 
          texto += scanner.hasNext(); 

         } 
        } catch (FileNotFoundException e) { 
         texto ="El archivo no se encuentra"; 
        } 
        textArea.setText(texto); 

      }  else { 
       texto ="no hay información de este tema en este planeta"; 
       textArea.setText(texto); 
      } 
      break; 
+0

"... aber es scheint nicht zu funktionieren ..." In welcher Weise? Wenn Sie mit setText (String) vertraut sind, müssen Sie die Fehlermeldung zumindest kennen? –

Antwort

0

Es ist wahrscheinlich, weil Sie versuchen, eine private Variable in einer public Funktion zu ändern. Sie müssen ändern

private JTextArea textArea;

zu

öffentlichen JTextArea textarea;

Ich habe es noch nicht getestet, also bin ich mir nicht sicher, ob das das Problem ist.