2

我的目标是在 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 {

    }

}
4

3 回答 3

1

我想拦截输入,并采取行动

那么您可能应该使用 DocumentFilter。有关详细信息,请参阅实现文档过滤器

于 2010-10-07T23:11:52.570 回答
0

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

罐子签名者

JTextArea console=new JTextArea();
console.append("Insert console text here \n") \\  \n for new line
于 2012-02-08T22:14:01.610 回答
0

似乎有很多不同的方法可以自定义 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);
}
于 2010-10-08T11:19:46.180 回答