标签:jtextarea java console swing
我的目标是在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.
标签:jtextarea,java,console,swing 来源: https://codeday.me/bug/20191011/1892109.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。