Java 使用JTextArea模拟文本控制台

Java 使用JTextArea模拟文本控制台,java,swing,console,jtextarea,Java,Swing,Console,Jtextarea,我在这里的目标是在Java中获得一个类似控制台的行为组件,不一定是在JTextArea中,但这似乎是首先尝试的合乎逻辑的事情。使用JTextArea提供的方法,输出足够简单,但输入是另一回事。我想截取输入,然后逐个字符地执行操作。我发现了一些使用DocumentListener处理模糊相关内容的示例,但它似乎不允许我轻松检查刚刚输入的内容,这正是我需要决定如何处理它的原因 我这样做对吗?有更好的方法吗 我附上我的申请代码的相关部分 public class MyFrame extends JFr

我在这里的目标是在Java中获得一个类似控制台的行为组件,不一定是在JTextArea中,但这似乎是首先尝试的合乎逻辑的事情。使用JTextArea提供的方法,输出足够简单,但输入是另一回事。我想截取输入,然后逐个字符地执行操作。我发现了一些使用DocumentListener处理模糊相关内容的示例,但它似乎不允许我轻松检查刚刚输入的内容,这正是我需要决定如何处理它的原因

我这样做对吗?有更好的方法吗

我附上我的申请代码的相关部分

public class MyFrame extends JFrame {
    public MyFrame() {
        Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
        Dimension frameSize=new Dimension((int)(screenSize.width/2),(int)(screenSize.height/2));
        int x=(int)(frameSize.width/2);
        int y=(int)(frameSize.height/2);
        setBounds(x,y,frameSize.width,frameSize.height);


        console = new JTextArea("",25,80);
        console.setLineWrap(true);
        console.setFont(new Font("Monospaced",Font.PLAIN,15));
        console.setBackground(Color.BLACK);
        console.setForeground(Color.LIGHT_GRAY);
        console.getDocument().addDocumentListener(new MyDocumentListener());

        this.add(console);

    }

    JTextArea console;

}

class MyDocumentListener implements DocumentListener
{
    public void insertUpdate(DocumentEvent e)
    {
        textChanged("inserted into");
    }
    public void removeUpdate(DocumentEvent e)
    {
        textChanged("removed from");
    }
    public void changedUpdate(DocumentEvent e)
    {
        textChanged("changed");
    }
    public void textChanged(String action)
    {
        System.out.println(action);
    }
}
谢谢你的帮助

EDIT1:我曾尝试使用带有DocumentFilter的JTextPane来执行此操作,但当我输入某些内容时,DocumentFilter中的方法无法运行。我附上修改后的代码:

public class MyFrame extends JFrame {
    public MyFrame() {
        Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
        Dimension frameSize=new Dimension((int)(screenSize.width/2),(int)(screenSize.height/2));
        int x=(int)(frameSize.width/2);
        int y=(int)(frameSize.height/2);
        setBounds(x,y,frameSize.width,frameSize.height);

        console = new JTextPane();
        //console.setLineWrap(true);
        console.setFont(new Font("Monospaced",Font.PLAIN,15));
        console.setBackground(Color.BLACK);
        console.setForeground(Color.LIGHT_GRAY);
        StyledDocument styledDoc = console.getStyledDocument();
            if (styledDoc instanceof AbstractDocument) {
            doc = (AbstractDocument)styledDoc;
            doc.setDocumentFilter(new DocumentSizeFilter());
        }

        this.add(console);

    }

    JTextPane console;
    AbstractDocument doc;

}

class DocumentSizeFilter extends DocumentFilter {

        public DocumentSizeFilter() {

    }

    public void insertString(FilterBypass fb, int offs, String str, AttributeSet a) throws BadLocationException {
        System.out.println(str);
        if (str.equals("y")) {
            System.out.println("You have pressed y.");
        }
    }

    public void replace(FilterBypass fb, int offs, int length, String str, AttributeSet a)  throws BadLocationException {

    }

}
我想截取输入,并采取行动 它


那么您可能应该使用DocumentFilter。有关更多信息,请参阅。

定制Swing文本组件的方法似乎有很多种。当我做了类似于您的事情时,我成功地创建了一个自定义文档:

import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;

public class CustomDocument extends PlainDocument {
    @Override
    public void insertString(int offset, String string, AttributeSet attributeSet) throws BadLocationException {
        // Parse input - in this case convert everything to upper case
        string = string.toUpperCase();
        super.insertString(offset, string, attributeSet);
    }
}
以下是测试代码的主要方法:

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setBounds(100, 100, 300, 300);
    frame.add(new JTextArea(new CustomDocument()));
    frame.setVisible(true);
}

我正在使用一个文本区域作为我构建的应用程序中的控制台,该应用程序将对jar文件进行签名。JTextArea有一个append()方法


我尝试过这样做,但由于某种原因,结果并不令人满意。我已将新方法编辑到我的帖子中。@abu dhabi,你是否复制了教程中的完整示例?我没有看到你的replace()方法的代码。这是通过GUI编辑文本组件时调用的方法。当您使用Document.insertString()方法直接在程序中提取文档时,会调用insertString()方法。我将代码复制到replace()方法,但也没有从那里调用它。您是否确实从教程中复制了代码并对其进行了测试?这种方法似乎不起作用;我在@Override处得到一个错误下划线,表示我没有重写或重放任何方法。我在attributeSet变量中也遇到了一个错误,这对于这个方法来说似乎是无效的——但是,在super.insertString()方法的调用中放入null会使它编译。我还得到了一个运行时异常,它应该是一个StyledDocument而不是PlainDocument,但通过更改回JTextArea解决了这个问题。最后,当我在//parse input part之后输入任何内容时,它不会被调用。如果需要样式文档,请扩展DefaultStyledDocument或HTMLDocument而不是PlainDocument。
JTextArea console=new JTextArea();
console.append("Insert console text here \n") \\  \n for new line