2016-06-01 7 views
0

私はTextAreaの文字列を上書きしたい、私はsetText(文字列)に精通していますが、動作していないようです。私は、textAreaがユーザが入力したものに従ってtxtファイルを読むことを望みます。次のように私のコードは次のとおりです。上書き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

"...しかし、うまくいかないようです..."どのようにして? setText(String)に精通している場合は、少なくともエラーメッセージを知る必要がありますか? –

答えて

0

あなたはpublic機能でprivate変数を変更しようとしているので、それはおそらくです。あなたは変更する必要があります

プライベートJTextArea textArea;

公共JTextAreaのテキストエリアに

まだテストしていないため、問題があるかどうかはわかりません。