java-使用JTextArea模拟文本控制台
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java-使用JTextArea模拟文本控制台,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2772字,纯文字阅读大概需要4分钟。
内容图文
![java-使用JTextArea模拟文本控制台](/upload/InfoBanner/zyjiaocheng/693/5a943befc1c54e478e392d851895a61d.jpg)
我的目标是在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 {
}
}
解决方法:
I want to intercept input, and act on
it
然后,您可能应该使用DocumentFilter.有关更多信息,请参见Implementing a Document Filter.
内容总结
以上是互联网集市为您收集整理的java-使用JTextArea模拟文本控制台全部内容,希望文章能够帮你解决java-使用JTextArea模拟文本控制台所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。