【问题标题】:JavaFX Label not updating correctly the textJavaFX 标签未正确更新文本
【发布时间】:2016-09-17 22:12:10
【问题描述】:

我正在尝试创建一个连接到 Linux 虚拟机(使用JSch)的应用程序,并向 Linux 询问一些关于自身的问题,例如操作系统名称和内核版本。我已经成功了,并且应用程序可以工作..但只能在 Eclipse 控制台中。

如果我尝试将其打印在标签或 TextArea 上...奇怪的事情正在发生。例如,如果我尝试在标签上打印出来,那么它只会打印出最后一个命令。如果我用 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();
        }        
    }
}

这是标签的样子。

【问题讨论】:

    标签: javafx label textarea


    【解决方案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) 来启用文本换行

    注意:您可以在 TextArea 上调用 appendText 而不是 setText,而无需额外的缓冲区。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-05
      • 2011-08-06
      • 1970-01-01
      相关资源
      最近更新 更多