简体   繁体   English

从控制台打印行到 GUI 文本框/lebel

[英]print line from console to the GUI textbox/lebel

I'm trying to build user interface for my automatic test in webdriver.我正在尝试为 webdriver 中的自动测试构建用户界面。

My question how can I print all the console line in the label or textbox ?我的问题如何打印标签或文本框中的所有控制台行?

Method set on the button:按钮上设置的方法:

    public void AutologinTest(ActionEvent event){
    try {
        Runtime rt = Runtime.getRuntime();
        Process pr = rt.exec("C:\\lottotest2\\workspace\\Lotteryscript\\Autologin.bat");
        BufferedReader input = new BufferedReader(
                new InputStreamReader(pr.getInputStream()));
        String line = null;
        while ((line = input.readLine()) != null)
            System.out.println(line);

    } catch (Exception e) {
        System.out.println(e.toString());
        e.printStackTrace();
    }

You can use System.setOut(PrintStream stream):您可以使用 System.setOut(PrintStream stream):

PrintStream ps = new PrintStream(
    new OutputStream() {
        public void write(int c){
            myLabel.setText(myLabel.getText() + (char) c);
        }
    }
);

System.setOut(ps);

System.out.println("Hello!"); // will print on the label

You can do something like this to redirect STDOUT to TextArea or other control:您可以执行以下操作将STDOUT重定向到TextArea或其他控件:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.stage.Stage;

import java.io.PrintStream;

public class Main22 extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {
        TextArea textArea = new TextArea();

        primaryStage.setScene(new Scene(textArea));
        primaryStage.show();

        System.setOut(new PrintStream(System.out) {
            @Override
            public void write(byte[] buf, int off, int len) {
                super.write(buf, off, len);

                String msg = new String(buf, off, len);

                textArea.setText(textArea.getText() + msg);
            }
        });

        System.out.println("bla-bla-bla");
        System.out.println("Yet one line!");
    }
}

As you can see I just override write method of PrintStream and append incoming text message for text in TextArea .如您所见,我只是覆盖了PrintStream write方法,并为TextArea文本附加了传入的文本消息。

应用程序截图

FYI: I don't recommend to use TextArea for display logs because it have very poor performance when trying to process large text.仅供参考:我不建议将TextArea用于显示日志,因为它在尝试处理大文本时性能非常差。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM