使用JTextArea模拟文本控制台

我的目标是在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); } } 

谢谢你的帮助。

编辑1:我试图使用带有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。 有关更多信息,请参阅实现文档filter 。

似乎有很多不同的方法来自定义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()方法。

Jar Signer

 JTextArea console=new JTextArea(); console.append("Insert console text here \n") \\ \n for new line