使用JTextArea来模拟文本控制台 [英] Using JTextArea to simulate a text console

查看:261
本文介绍了使用JTextArea来模拟文本控制台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是在Java中获取一个类似控制台的组件,而不一定在JTextArea中,但这似乎是一个合乎逻辑的事情。输出很简单,使用JTextArea提供的方法,但输入是另一回事。我想拦截输入,并行动它 - 逐个字符。我已经找到一些使用DocumentListener的模糊相关的例子,但它似乎不允许我很容易检查什么是刚刚键入,这是我需要决定如何采取行动。



我是否正确?有更好的方法吗?



我包含我的应用程序代码的相关部分。

  public class MyFrame extend 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控制台;

}

class MyDocumentListener implements DocumentListener
{
public void insertUpdate(DocumentEvent e)
{
textChanged 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控制台;
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。有关详细信息,请参见实现文档过滤器。 p>

My objective here is to obtain a console-like-behaving component in Java, not necessarily in JTextArea, but this seemed like a logical thing to try first. Output is simple enough, using the methods provided by the JTextArea, but input is another thing. I want to intercept input, and act on it - character by character. I've found some examples on using a DocumentListener for something vaguely related, but it doesn't seem to allow me to easily check what was just typed in, which is what I need to decide how to act upon it.

Am I going about this correctly? Is there a better method for this?

I enclose the pertinent parts of my application code.

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);
    }
}

Thanks for any help.

EDIT1: I have attempted to do this using a JTextPane with a DocumentFilter, but when I input something, the method in the DocumentFilter isn't getting run. I enclose the modified code:

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 {

    }

}

解决方案

I want to intercept input, and act on it

Then you should probably be using a DocumentFilter. See Implementing a Document Filter for more information.

这篇关于使用JTextArea来模拟文本控制台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆