滚动或调整框架时,JPanel中的绘图消失 [英] Drawing in JPanel disappears when scrolling or ressizing the frame

查看:132
本文介绍了滚动或调整框架时,JPanel中的绘图消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大问题,我无法解决,我在JFrame中有一个JPanel.在此面板中,我绘制了许多东西(如图表),但它可以工作,但是当我滚动面板或调整框架大小时,图纸消失了!

I have a big problem that I can't solve it I have a JPanel in a JFrame. In this panel I draw many things (like a diagram) it works, but when I scroll the panel or when I resize the frame the drawings disappears!

我应该如何保留图纸?

代码:

  private void full_simulation_button(java.awt.event.ActionEvent evt) {

   if (jTextField1.getText().equals("")){
  JOptionPane jop2 = new JOptionPane();
  jop2.showMessageDialog(null, "You should enter you trace file !",         "Attention", JOptionPane.WARNING_MESSAGE);
   }
   else {

  Graphics g = jPanel5.getGraphics();  

g.setColor(Color.RED);
g.drawRect(150,10,100,20);  
g.fillRect(150,10,100,20);
g.drawLine(200,10 , 200, 2000);
g.setColor(Color.BLACK);
g.drawString("UE",190 ,25 );

g.setColor(Color.BLUE); 
g.drawRect(350,10,100,20); 
g.fillRect(350,10,100,20);
g.drawLine(400,10 , 400, 2000);
g.setColor(Color.BLACK);
g.drawString("Node B",380 ,25 );

 g.setColor(Color.GREEN);
g.drawRect(550,10,100,20);    
g.fillRect(550,10,100,20);
g.drawLine(600,10 , 600, 2000);
g.setColor(Color.BLACK);
g.drawString("RNC",590 ,25 );

g.setColor(Color.YELLOW); 
g.drawRect(750,10,100,20);    
g.fillRect(750,10,100,20);
g.drawLine(800,10 , 800, 2000);
g.setColor(Color.BLACK);
g.drawString("CN",790 ,25 );

System.out.println(new java.io.File("").getAbsolutePath());
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
try {
     final DocumentBuilder builder = factory.newDocumentBuilder();      
     final Document document= builder.parse(new File(jTextField1.getText()));
     //Affiche la version de XML
     System.out.println("la version du XML est "+ document.getXmlVersion());
    //Affiche l'encodage
    System.out.println("l'encodage utilisé est"+document.getXmlEncoding()); 
    //Affiche s'il s'agit d'un document standalone      
    System.out.println("le document est standolone:"+document.getXmlStandalone());

    final Element racine = document.getDocumentElement();
    System.out.println("le racime du fichier est :"+racine.getNodeName());

    final NodeList racineNoeuds = racine.getChildNodes();

    final int nbRacineNoeuds = racineNoeuds.getLength();

    for (int i = 0; i<nbRacineNoeuds; i++) {
          if(racineNoeuds.item(i).getNodeType() == Node.ELEMENT_NODE) {
                 final Element nd = (Element) racineNoeuds.item(i);
                 final Node noeud = racineNoeuds.item(i);
                 System.out.println(noeud.getNodeName());



    final NodeList msg = nd.getElementsByTagName("PARA");
    final int nbre = msg.getLength();
    int marge=0;
    int rang =1;
    String type =null;
    String direction=null;
    for(int j = 0; j<nbre ; j++){

        final Element message = (Element) msg.item(j);


        g.setColor(Color.BLACK);
        if ( message.getAttribute("name").equals("Message Type")){ 
        type = message.getAttribute("value");
       // System.out.println(type);
        if (type.startsWith("RRC")){

          g.drawLine(200,50+marge , 600, 50+marge);
          g.drawString(rang+". "+type,220 ,45+marge );
          marge=marge+30;
          rang=rang+1; 
         }
        if (type.startsWith("NBAP")){
          g.drawLine(400,50+marge , 600, 50+marge);
          g.drawString(rang+". "+type,420 ,45+marge );
          marge=marge+30;
          rang=rang+1; 

         }
        if (type.startsWith("RANAP")){
            g.drawLine(600,50+marge , 800, 50+marge);
            g.drawString(rang+". "+type,620 ,45+marge );
            marge=marge+30;
            rang=rang+1; 

         }
        }
        if ( message.getAttribute("name").equals("Message Direction")){ 
        direction = message.getAttribute("value");
        //System.out.println(direction);
        if ((direction.equals("From-UE"))&&(type.startsWith("RRC"))){
          g.drawString(">",595, 50+marge-25 );
          System.out.println(type);

         }
        if ((direction.equals("To-UE"))&&(type.startsWith("RRC"))){
          g.drawString("<",200, 50+marge-25 );
          System.out.println(type);

         }
        if ((direction.equals("From-NodeB"))&&(type.startsWith("NBAP"))){
          g.drawString(">",595, 50+marge-25 );
          System.out.println(type);

         }
        if ((direction.equals("To-NodeB"))&&(type.startsWith("NBAP"))){
          g.drawString("<",400, 50+marge-25 );
          System.out.println(type);

         }
        if ((direction.equals("From-CN"))&&(type.startsWith("RANAP"))){
          g.drawString("<",600, 50+marge-25 );
          System.out.println(type);

         }
        if ((direction.equals("To-CN"))&&(type.startsWith("RANAP"))){
          g.drawString(">",795, 50+marge-25 );
          System.out.println(type);

         }
  }

}
         }   

}

}

        catch (final ParserConfigurationException e) {
         e.printStackTrace();
            }
        catch (final SAXException e) {
          e.printStackTrace();
           }
       catch (final IOException e) {
        e.printStackTrace();
         }
           }
          }                                       

推荐答案

切勿在提供的挥杆功能(例如paintComponent(Graphics g))之外绘制.调整大小或滚动时,组件将重新绘制.在此过程中,将调用每个涉及组件的paintComponent方法.由于您的绘图发生在其外部,因此不会按预期重新绘制面板.覆盖paintComponent方法并将所有绘图代码复制到其中.

You should never paint outside of the provided swing functions like paintComponent(Graphics g). When resizing or scrolling the component will be repainted. In this process the paintComponent method of every involved component is called. Since your drawing happens outside of it, the panel is not redrawn as intended. Override the paintComponent method and copy all your drawing code into it.

class MyPanel extends JPanel
{
    @Override
    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        g.setColor(Color.RED);
        g.drawRect(150,10,100,20);  
        g.fillRect(150,10,100,20);
        g.drawLine(200,10 , 200, 2000);
        g.setColor(Color.BLACK);
        g.drawString("UE",190 ,25 );
        /* ... All drawing code ... */
    }
}

这篇关于滚动或调整框架时,JPanel中的绘图消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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