如何获取终端结果并设置JTextArea来读取终端? [英] How to take terminal results and set a JTextArea to read the terminal?

查看:103
本文介绍了如何获取终端结果并设置JTextArea来读取终端?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近完成了一个GUI,用户可以输入标准,并获得符合上述条件的结果。该程序结果明智,但我在GUI中获取textField以读取终端窗口结果时遇到问题。我的GUI代码如下:

I recently finished up a GUI where a user can input criteria, and get results adhering to said conditions. The program works result wise, but I'm having trouble getting my textField in my GUI to read my terminal window result. My code for the GUI is as followed:

package project205;
import javax.swing.*;

import java.awt.*;
import java.awt.event.*;

public class HouseListGUI extends JFrame
{
//GUI Components

HouseList availableHouses = new HouseList("src/houses.txt");

 JLabel  cLab   = new JLabel("Criteria");
 JLabel  minLab   = new JLabel("Min");
 JLabel  maxLab   = new JLabel("Max");
 JLabel  pLab   = new JLabel("Price");
 JLabel  aLab   = new JLabel("Area");
 JLabel  bLab   = new JLabel("Bedrooms");

 JTextField  pMin   = new JTextField(10);
 JTextField  pMax   = new JTextField(10);
 JTextField  aMin   = new JTextField(10);
 JTextField  aMax   = new JTextField(10);
 JTextField  bMin   = new JTextField(10);
 JTextField  bMax   = new JTextField(10);

 JTextArea  results    = new JTextArea(20, 40);

 JButton sButton   = new JButton("Search");
 JButton qButton   = new JButton("Quit");

public HouseListGUI()
{


        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(400,300);
        setVisible(true);  





Container c = getContentPane();
c.setLayout(new GridLayout(5,3,10,10));
c.add(cLab);
c.add(minLab);
c.add(maxLab);
c.add(pLab);
c.add(pMin);
c.add(pMax);
c.add(aLab);
c.add(aMin);
c.add(aMax);
c.add(bLab);
c.add(bMin);
c.add(bMax);
c.add(sButton);
c.add(results);
c.add(qButton);

sButton.addActionListener(
  new ActionListener()
  {
    public void actionPerformed(ActionEvent e)
    {
    String pmn = pMin.getText();
    String pmx = pMax.getText();
    String amn = aMin.getText();
    String amx = aMax.getText();
    String bmn = bMin.getText();
    String bmx = bMax.getText();

    int pmn1 = Integer.parseInt(pmn);
    int pmx1 = Integer.parseInt(pmx);
    int amn1 = Integer.parseInt(amn);
    int amx1 = Integer.parseInt(amx);
    int bmn1 = Integer.parseInt(bmn);
    int bmx1 = Integer.parseInt(bmx);


    Criteria theCriteria = new Criteria(pmn1, pmx1, amn1, amx1, bmn1, bmx1);

    availableHouses.printHouses(theCriteria);

    results.setText("");

    }
  });


qButton.addActionListener(
        new ActionListener()
        {
             public void actionPerformed(ActionEvent e)
             {
                 System.exit(0);
             }
        }
        );
}
public static void main(String[] args)
{

    //HouseList availableHouses = new HouseList("src/houses.txt");
    HouseListGUI g1 = new HouseListGUI();
    g1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    g1.setSize(1000,500);
    g1.show();
}

}

我想要的结果代码行
availableHouses.printHouses(theCriteria);
返回一个类型void
将在textArea
结果中打印

I would like the results of the code line availableHouses.printHouses(theCriteria); which returns a type void to be printed in the textArea results

我尝试使用类型转换来转换虚空作为字符串并将其放入结果中,即:

I've tried using type cast to cast the void as a string and putting that into results, ie:

   results.setText((String)availableHouses.printHouses(theCriteria);

但是Java没有。

TL; DR:

如何通过类型为void的方法调用获取终端,并将其作为类型String打印到textArea中在GUI的上下文中?

How might one take a terminal resulting from a method call of type void, and printing that as a type String into a textArea in the context of a GUI?

进一步说明:

我的终端窗口如下所示:

my terminal window looks like this:

http://imgur.com/wiewXan

我的GUI如下所示:

http://imgur.com/wdd0ieK

我希望我的终端位于最后一行的中间框中。

and I want my terminal to be in the middle box in the last row.

感谢您的任何建议/提示!

Thanks for any advice/tips!

推荐答案

您可以捕获并重定向发送到系统的内容.out 提供您自己的 OutputStream 。这是一个万无一失的解决方案,因为任何印在标准上的东西都会被捕获,但你可以根据需要打开和关闭...

You can capture and redirect the content sent to System.out by supplying your own OutputStream. This is, by no stretch of the imagination, a foolproof solution, as anything printed to the standard out will be captured, but you could turn in on and off as you needed...

我在没有调试语句到日志文件的应用程序中使用它作为调试窗口,以及从生成任何文件中过滤和停止 System.out 输出...

I've used this as a debug window in applications that did not have debug statements to a log file as well as filtering and stopping System.out from generating any output...

在这个例子中,我使用...

In this example I use ...

PrintStream ps = System.out;
System.setOut(new PrintStream(new StreamCapturer(capturePane, ps)));
doSomeProcessing();
System.setOut(ps);

开始和停止捕获 System.out ,您可以在 StreamCapturer 中放置一个标志,打开和关闭回显功能,这可能是解决问题的更好方法......

To start and stop capturing the System.out, you could place a flag in StreamCapturer that turned the echoing feature on and off, which might be better way to approach the problem...

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class RediretStdOutTest {

    public static void main(String[] args) {
        new RediretStdOutTest();
    }

    public RediretStdOutTest() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                final CapturePane capturePane = new CapturePane();
                JButton processButton = new JButton("Process");
                processButton.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        System.out.println("Look at me, I'm not in your UI");
                        PrintStream ps = System.out;
                        System.setOut(new PrintStream(new StreamCapturer(capturePane, ps)));
                        doSomeProcessing();
                        System.setOut(ps);
                        System.out.println("Neither am I");
                    }
                });

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(capturePane);
                frame.add(processButton, BorderLayout.SOUTH);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    protected void doSomeProcessing() {

        for (int index = 0; index < 10; index++) {
            System.out.println("--> " + index);
        }

    }

    public class CapturePane extends JPanel implements Consumer {

        private JTextArea output;

        public CapturePane() {
            setLayout(new BorderLayout());
            output = new JTextArea(10, 20);
            add(new JScrollPane(output));
        }

        @Override
        public void appendText(final String text) {
            if (EventQueue.isDispatchThread()) {
                output.append(text);
                output.setCaretPosition(output.getText().length());
            } else {

                EventQueue.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        appendText(text);
                    }
                });

            }
        }
    }

    public interface Consumer {

        public void appendText(String text);
    }

    public class StreamCapturer extends OutputStream {

        private StringBuilder buffer;
        private Consumer consumer;
        private PrintStream old;

        public StreamCapturer(Consumer consumer, PrintStream old) {
            buffer = new StringBuilder(128);
            this.old = old;
            this.consumer = consumer;
        }

        @Override
        public void write(int b) throws IOException {
            char c = (char) b;
            consumer.appendText(Character.toString(c));
            old.print(c);
        }
    }
}

现在,说了这么多,我会修复你的方法,以便它返回一个结果,因为它会更干净......

Now, having said all that, I would fix you method so that it returned a result, as it would be cleaner...

这篇关于如何获取终端结果并设置JTextArea来读取终端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆