这是我的问题:Traffic 是一个 JTextArea,我在其中插入来自串行端口的文本,并且我确实实现了 DocumentListener:
Java 代码:
Traffic.getDocument (). AddDocumentListener (new MyDocumentListener ());
JTextArea“交通”告诉我我想要什么,程序运行没有任何问题。现在我想做的是逐行(这就是测试的目的)对插入的内容(仅在插入事件中)采取行动,我将举一个例子:
==> 当我收到“ON-HOOK”时,如果我收到“OFF-HOOK”,我会将图像放在电话挂机的 JPanel 中,我将图像放在电话摘机的 JPanel 中,然后列表会...
我不明白怎么做,怎么在我想要的时候调用paintComponent,让它画出我想要的,因为我只能在paintComponent()中进行图形操作。这是 DocumentListener 的类:
protected class MyDocumentListener extends JPanel implements javax.swing.event.DocumentListener
{
@Override
public void changedUpdate(javax.swing.event.DocumentEvent e) {
// text has been altered in the textarea
}
@Override
public void insertUpdate(javax.swing.event.DocumentEvent e)
{
// text has been added to the textarea
try {
if (!Traffic.getText(Traffic.getLineStartOffset(Traffic.getLineCount()-1),Traffic.getLineEndOffset(Traffic.getLineCount()-1)-Traffic.getLineStartOffset(Traffic.getLineCount()-1)).contains(">>"))
{
if (Traffic.getLineCount() == (lastreplace + 2) )
{
System.err.println(Traffic.getText(Traffic.getLineStartOffset(lastreplace),Traffic.getLineEndOffset(lastreplace) - Traffic.getLineStartOffset(lastreplace)));
lastreplace +=1;
}
else
{
System.err.println(Traffic.getText(Traffic.getLineStartOffset(lastreplace),
Traffic.getLineEndOffset(lastreplace) -
Traffic.getLineStartOffset(lastreplace)));
}
}
Traffic.setCaretPosition(Traffic.getDocument().getLength());
} catch (BadLocationException ex) {
Logger.getLogger(TrafficSerialPort.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Override
public void removeUpdate(javax.swing.event.DocumentEvent e) {
// text has been removed from the textarea
}
}
我尝试使用一种名为"PortArchitecture (..)"
will draw what I want and I use的方法,getGraphics()
但有人告诉我不应该使用它paintComponent()
,现在我真的卡住了,请帮忙。