简体   繁体   English

动态JTextArea,等待用户输入,如Netbeans Console

[英]Dynamic JTextArea which wait for user input like Netbeans Console

在此输入图像描述

I am creating an IDE in swing where user can compile and run their java program. 我正在创建一个IDE,用户可以编译和运行他们的java程序。 Here, output of the user's program will be shown to a JEditorPane . 这里,用户程序的输出将显示给JEditorPane i am using reflection to do so. 我正在使用反射这样做。

I have set the input stream to a JTextArea by using System.setIn() and output stream to JEdotorPane by using System.setOut() method. 我已使用System.setIn()将输入流设置为JTextArea ,并使用System.setOut()方法将输出流输出到JEdotorPane

Now, the Problem is that when user run a program which have console input like System.in.read(); 现在,问题是当用户运行一个具有控制台输入的程序,如System.in.read(); it do't wait for user input, from JEditorPane . 它不会等待来自JEditorPane用户输入。 It just run the program and read whole data from JTextArea . 它只是运行程序并从JTextArea读取整个数据。 and further process other line of code. 并进一步处理其他代码行。

Now, i want that the program output should wait for the user input and when user provide input in JTextArea and hit enter, then further processing of inputted text is done. 现在,我希望程序输出应该等待用户输入,当用户在JTextArea提供输入并按Enter键时,则完成对输入文本的进一步处理。

Another Way: via CMD 另一种方式:通过CMD

Actually, its little bit complicated to make an JTextPane as dynamic windows... and i have to do some more research on this particular topic... 实际上,将JTextPane制作为动态窗口有点复杂......我还要对这个特定主题做更多研究......

So for now, i just did some changes in the way of showing Output. 所以现在,我只是在显示输出的方式上做了一些改变。 i am creating a separate Process by using Runtime class and execute that process in Command Prompt(CMD). 我正在使用Runtime类创建一个单独的Process,并在命令提示符(CMD)中执行该过程。

So, when the program generates an output. 所以,当程序生成输出时。 it shown on the command prompt... like many IDEs supports this mechanism... for example... Code Block. 它显示在命令提示符上......就像许多IDE支持这种机制一样......例如......代码块。

for now, Its working fine... 现在,它的工作正常......

I'd recommend using a DocumentFilter to listen for updates to the JTextArea . 我建议使用DocumentFilter来监听JTextArea更新。

From there you can veto changes, or insert them only at the end of the document. 从那里您可以否决更改,或仅在文档末尾插入更改。 Also, it'll notify you when updates are made to the document so you can send them to your program. 此外,它会在对文档进行更新时通知您,以便您将它们发送到您的程序。 This should simplify your life. 这应该简化你的生活。

I would strongly advise against using a KeyListener - they're very finicky with things like focus. 我强烈建议不要使用KeyListener - 他们对焦点之类的东西非常挑剔。

Here's an example of adding a DocumentFilter 这是添加DocumentFilter的示例

    final Document d = textArea.getDocument();
    if(d instanceof AbstractDocument){
        ((AbstractDocument)d).setDocumentFilter(new DocumentFilter(){               
            @Override
            public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException{
                //Only allow edits at end (yours will be more complicated)
                System.out.println("Called replace");
                if(offset == d.getLength()){
                    //Edit only goes through if the filter bypass is used
                    fb.replace(offset, length, text, attrs);
                }
            }
        });
    }

Hey why don't you check the source of the NetBeans Output window for "inspiration": OutputTab.java 嘿,为什么不检查“灵感”的NetBeans输出窗口的来源: OutputTab.java

look here also: output2 src dir 再看一下: output2 src dir

hmm, you can probably process the enter key using a KeyListener (If you don't know, you should implement it in the gui (or component) class, and than add it to the component.) and than continue the processing of the rest of the program's input. 嗯,您可以使用KeyListener处理输入键(如果您不知道,您应该在gui(或组件)类中实现它,而不是将其添加到组件中。)然后继续处理其余的程序的输入。 I would think this: 我会这样想:

    @Override
    public void KeyPressed(KeyEvent e){
    if(e.getKey == e.VK_ENTER){
    processText();
    keepProcessing();

    }

I hope this helps! 我希望这有帮助! good luck. 祝好运。

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

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