【问题标题】:Draw Graphics on JPanel as insert opertaion on JTextArea are made在 JPanel 上绘制图形作为对 JTextArea 的插入操作
【发布时间】:2012-12-31 21:21:33
【问题描述】:

大家好,提前致谢。 所以我的问题是我想根据 JtextArea 中插入的内容在 JPanel 上绘制一些东西,这是正确的吗? : 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 not prompt Line
            if (!Traffic.getText(Traffic.getLineStartOffset(Traffic.getLineCount()-1),
              Traffic.getLineEndOffset(Traffic.getLineCount()-1)-              Traffic.getLineStartOffset(Traffic.getLineCount()-1)).contains(">>"))
      {//if a line after a replace has been inserted

          if (Traffic.getLineCount()  == (lastreplace + 2) ) 
          {
        System.err.println(Traffic.getText(Traffic.getLineStartOffset(lastreplace),
                            Traffic.getLineEndOffset(lastreplace) - 
                                    Traffic.getLineStartOffset(lastreplace)));
               PortArchitecture (Traffic.getText(Traffic.getLineStartOffset(lastreplace),
                            Traffic.getLineEndOffset(lastreplace) - 
                                    Traffic.getLineStartOffset(lastreplace)));

               lastreplace +=1;

          }


            else//last line when it's not detected by the previous condition it'll be consumed here
          {
               System.err.println(Traffic.getText(Traffic.getLineStartOffset(lastreplace),
                            Traffic.getLineEndOffset(lastreplace) - 
                                    Traffic.getLineStartOffset(lastreplace)));
             PortArchitecture (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

}
 }

检查条件并在 JPanel 上绘制的方法:

public void PortArchitecture (String line) throws BadLocationException 
{

 Graphics2D gfx = (Graphics2D)graphicPanel.getGraphics();
    String [] valuesId = new String [2];
    valuesId = line.split(":");
    /**switch (valuesId[0])
    {
        case "Port Number" : draw();
    }*/
    if(valuesId[0].contains("Port number"))
    {

     gfx.drawString("(PN)", 30, 10);
     gfx.drawString(valuesId[1],30, 140);
}
    if(valuesId[0].contains("Port Extension"))
    {
        gfx.drawString("(EXT)", 100, 10);
        gfx.drawString(valuesId[1], 100, 140);
      //  draw(g);
}
    if(valuesId[0].contains("Forward Extension"))
    {
        gfx.drawLine(140,80,140,110);
      //  draw(g);
}
    if(valuesId[0].contains("ON-HOOK"))
    {
      //  draw(g);
}
    if(valuesId[0].contains("OFF-HOOK"))
    {
       // draw(g);
}
    if(valuesId[0].contains("Forward status"))
    {
        if(valuesId[1].contains("FORWARDED"))
        {
           // draw(g);
        }
        else
        {
           // draw(g);
        }
}
    if(valuesId[0].contains("Dialing"))
    {
        //get dialed number
        //draw(g);
}

}

还有一件事:我有一些关于显示一些图纸的结果,但是当我最小化应用程序时,所有的图形都消失了。我知道我需要调用 repaint() 但我不知道何时以及如何。 请帮忙!

【问题讨论】:

    标签: graphics jpanel graphics2d documentlistener


    【解决方案1】:

    所以我学得很辛苦,但所有绘图都必须在 paintComponent() 方法中。所以在我的情况下,我需要 3 个类(这适用于任何有兴趣的人): - 第一个初始化 JTextArea 并将 documentListener 添加到它。 - 第二个是 DocumentListener 类。 - 第三个是从 JPanel(或 JComponent)扩展的类,并包含方法 paintComponent()。

    希望对你有帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-17
      • 2014-11-02
      • 1970-01-01
      相关资源
      最近更新 更多