2016-09-17 74 views
-1

我想創建一個連接到Linux虛擬機的應用程序(使用JSch)並向Linux詢問關於它自己的一些問題,如操作系統名稱和內核版本。我已經成功了,應用程序工作..但只在Eclipse控制檯。JavaFX標籤沒有正確更新文本

如果我嘗試打印出標籤或TextArea ......奇怪的事情正在發生。例如,如果我嘗試在標籤上打印出來,則只打印出最後一個命令。如果我和文本區域,然後嘗試它,它打印出的一切,但在一個行,我不知道如何剎車線......

下面是代碼:

import javafx.fxml.FXML; 
import javafx.scene.canvas.Canvas; 
import javafx.scene.control.Label; 
import javafx.scene.control.TextArea; 
import javafx.scene.control.TextField; 
import javafx.scene.layout.Pane; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.util.Properties; 

import com.jcraft.jsch.Channel; 
import com.jcraft.jsch.ChannelExec; 
import com.jcraft.jsch.JSch; 
import com.jcraft.jsch.Session; 

public class MainWindowController { 

    @FXML private TextField ip_text_field, username_text_field, password_text_field; 
    @FXML private Label output; 

    String ip, username, pass; 


    private Main main; 

    public void setMain(Main main){ 
     this.main = main; 
    } 

    public String getIP(){ip = ip_text_field.getText(); return ip;} 
    public String getUsername(){username = username_text_field.getText(); return username;} 
    public String getPassword(){pass = password_text_field.getText(); return pass;} 

    public void connectButtonFunction(){     

     try{ 
      String command = "lsb_release -a | grep -i Description && uname -mrs"; 

      String host = getIP(); 
      String user = getUsername(); 
      String password = getPassword(); 

      JSch jsch = new JSch(); 
      Session session = jsch.getSession(user, host, 22); 
      Properties config = new Properties(); 
      config.put("StrictHostKeyChecking", "no"); 
      session.setConfig(config);; 
      session.setPassword(password); 
      session.connect(); 

      Channel channel = session.openChannel("exec"); 
      ((ChannelExec)channel).setCommand(command); 
      channel.setInputStream(null); 
      ((ChannelExec)channel).setErrStream(System.err); 

      InputStream input = channel.getInputStream(); 
      channel.connect(); 

      //System.out.println("Channel Connected to machine " + host + " server with command: " + command); 

      try{ 
       InputStreamReader inputReader = new InputStreamReader(input); 
       BufferedReader bufferedReader = new BufferedReader(inputReader); 
       String line = null; 

       while((line = bufferedReader.readLine()) != null){ 
        //System.out.println(line); 
        output.setText(line);     

       } 

       bufferedReader.close(); 
       inputReader.close(); 
      }catch(IOException ex){ 
       ex.printStackTrace(); 
      } 

      channel.disconnect(); 
      session.disconnect(); 


     }catch(Exception ex){ 
      ex.printStackTrace(); 
     }   
    } 
} 

這是怎麼了看起來像標籤。

enter image description here

回答

1

的原因,它只能顯示最後一個命令,當您使用Label是因爲setText重寫標籤文本每次。

因此,像這樣將解決這個問題:

String buffer = ""; 
while((line = bufferedReader.readLine()) != null){ 
    buffer += line + "\n"; // assuming `line` doesn't end with a newline already   
} 
output.setText(buffer); 

對於TextArea問題(你也將面臨Label),你可以通過調用output.setWrapText(true)

注意使文本換行:可以不需要額外的緩衝區就可以撥打appendText而不是setText,而不需要TextArea