简体   繁体   English

Java-如何扩展InputStream以从JTextField读取?

[英]Java-How to extend InputStream to read from a JTextField?

Working on a project I got into running java applications through a small console-like window. 在一个项目上工作,我通过类似控制台的小窗口运行java应用程序。 Thanks to the wonderful community in here I managed to solve the problem with outputting the data from a proccess but my command-line applications running will constantly give errors as there is no input stream. 感谢这里的精彩社区,我设法通过输出来自proccess的数据解决问题但是我的命令行应用程序运行将不断给出错误,因为没有输入流。

Based on the last helpful reply in that thread I suppose I shall approach similarly the JTextFieldInputStream extends InputStream implementation, but looking in the javadocs and throughout google and the internet for some class that does just that I really found nothing explaining how to do this. 基于该线程中的最后一个有用的回复,我想我将类似地接近JTextFieldInputStream extends InputStream实现,但是查看javadocs以及整个google和互联网中的某些类,我确实没有解释如何执行此操作。

So I am asking for some link, example, tutorial, sample code for it just like in the previous topic. 所以我要求一些链接,示例,教程,示例代码,就像上一个主题一样。 Give me just a class that extends InputStream and can be extended to read from a JTextField when I press Enter and I will do the rest with implementing this and making it work! 给我一个扩展InputStream的类,当我按Enter键时可以扩展为从JTextField读取,我将完成其余的工作并使其工作! Thanks in advance! 提前致谢!

What I don't understand if why you need a JTextField that extends InputStream? 我不明白为什么你需要一个扩展InputStream的JTextField? Basically, what you are looking for is: 基本上,你要找的是:

  1. Add an ActionListener on the JTextField (ie, when use presses Enter , actionPerformed will be invoked) JTextField上添加一个ActionListener (即,当使用按Enter键时 ,将调用actionPerformed
  2. You need to grab the text of the JTextField using getText() 你需要使用getText()获取JTextField的text
  3. You can then "transform" the String text to an InputStream with new ByteArrayInputStream(text.getBytes("UTF-8")); 然后,您可以使用new ByteArrayInputStream(text.getBytes("UTF-8"));String text “转换”为InputStream new ByteArrayInputStream(text.getBytes("UTF-8"));

Here is a small snippet that should get you the basic idea: 这是一个小片段,应该为您提供基本的想法:

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class TestTextField {

    private void initUI() {
        JFrame frame = new JFrame(TestTextField.class.getSimpleName());
        frame.setLayout(new FlowLayout());
        final JTextField textfield = new JTextField(20);
        textfield.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    String text = textfield.getText();
                    InputStream is = new ByteArrayInputStream(text.getBytes("UTF-8"));
                    // Here do something with your input stream (something non-blocking)
                    System.err.println(text);
                } catch (UnsupportedEncodingException e1) {
                    e1.printStackTrace();
                }

            }
        });
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(textfield);
        frame.setSize(300, 300);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new TestTextField().initUI();
            }
        });
    }

}

How about this implementation 这个实现怎么样?

import java.io.IOException;
import java.io.InputStream;

import javax.swing.JTextField;


public class JTextFieldInputStream extends InputStream {
    byte[] contents;
    int pointer = 0;

    public JTextFieldInputStream(final JTextField text) {

        text.addKeyListener(new KeyAdapter() {
            @Override
            public void keyReleased(KeyEvent e) {
                if(e.getKeyChar()=='\n'){
                    contents = text.getText().getBytes();
                    pointer = 0;
                    text.setText("");
                }
                super.keyReleased(e);
            }
        });
    }

    @Override
    public int read() throws IOException {
        if(pointer >= contents.length) return -1;
        return this.contents[pointer++];
    }

}

to use this input stream, do the following 要使用此输入流,请执行以下操作

 InputStream in = new JTextFieldInputStream( someTextField );
 char c;
 while( (c = in.read()) != -1){
    //do whatever with c
 }

does it read only when I hit enter? 只有当我点击进入时它才能读取吗?

it reads when you call in.read() if the return value -1 it means end of the stream 当你调用in.read()时它会读取,如果返回值为-1则表示流的结束

(And will I be able to modify so that the Enter key empties the JTextField?) (并且我能够修改以便Enter键清空JTextField吗?)

you need to add an action listener and this functionality has nothing to do with the job of input stream 您需要添加一个动作侦听器,此功能与输入流的作业无关

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

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