2

我有问题。我正在从套接字读取数据,当我向我的应用程序读取一些有意义的数据时,我会调用一个方法来用我想要的摆动对象重新绘制屏幕。这种方法可以正常工作,只是有时它不会出现。第一次调用该方法时屏幕上没有任何内容(我已经运行了调试并且它通过了该方法)。愚蠢的部分是,如果我最小化和最大化应用程序,它会再次正常工作。此外,如果再次执行该方法,它可以正常工作。我将代码放在这里只是为了文档:

public void GraphicONENodeJoined (Nodes NodesList)
 {
     SensorsMapGraf [] sensorsMap;
     boolean FLAG;
     int ymin=0, ymax=30;
     FLAG = TabExistingNode_NEW_nodeRefresh (NodesList);
     if (FLAG)
     {
         ValorTeste++;
         if (ValorTeste > 1)
         {
             String [] s = new String[ValorTeste];
             JButton [] jbb = new JButton[ValorTeste];
             JCheckBox [] ch = new JCheckBox[ValorTeste];
             for (int i=0 ; i < ValorTeste-1 ; i++)
             {
                 s[i] =  nods[i];
                 jbb[i] = jb[i];
                 ch[i] = CheckBoxNodes[i];      
             }
             jb = new JButton[ValorTeste];
             CheckBoxNodes = new JCheckBox[ValorTeste];
             nods =  new String[ValorTeste];

             for (int i=0 ; i < ValorTeste ; i++)
             {
                 nods[i] = s[i];
                 jb[i] = jbb[i];

                 CheckBoxNodes[i] = ch[i];
             }       
         }

            nods[ValorTeste-1]+= NodesList.GETSourceNodeID();
            //PARA FAZER O UPDATE DAS TABS E DOS GRÁFICOS
            JPanel meio = new JPanel();         //JPanel que contem os graficos de cada sensor

            String s = new String();
            s += NodesList.GETSourceNodeID();

            JPanel meio_cima = new JPanel();    //JPanel que contem  o botão view (permitirá escolher quais os sensores a ver)
            meio_cima.setAlignmentX(LEFT_ALIGNMENT);
            meio_cima.setPreferredSize(new Dimension(dim.width, 25));
            meio.setBounds (pan1.getBounds());
            GridBagLayout g = new GridBagLayout();
            GridBagConstraints c = new GridBagConstraints();

            meio.setLayout(g);
            meio.setOpaque(true);
            //botão view (permitirá escolher quais os sensores a ver)
            jb[nods.length-1] = new JButton("View");
            jb[nods.length-1].setPreferredSize(new Dimension(100,20));
            jb[ValorTeste -1].addActionListener(this);
            meio_cima.add(jb[ValorTeste-1]);
            //Panel qu albergará o panel meio e o panel meio_cima
            JPanel principal = new JPanel();
            //inicia o array de sensores que cada Tab tem de mostrar
            sensorsMap = new SensorsMapGraf[NodesList.GETnumSensoresActivos()];

            int indice = 0;
            for (int j=0 ; j < 12 ; j++)
            { 
                //nicely to view in the graphics
                if ((j == 0) || (j == 1) || (j == 2) || (j == 3) || (j == 6) || (j == 7))
                {
                    ymin = 0;
                    ymax = 3;
                }
                if (j == 8)
                {
                    ymin = 0;
                    ymax = 100;
                }
                if (j == 9)
                {
                    ymin = 0;
                    ymax = 100;
                }
                if (j == 10)
                {
                    ymin = 0;
                    ymax = 4;
                }
                if (j == 11)
                {
                    ymin = 0;
                    ymax = 130;
                }
                if (j == 4 )
                {
                    ymin = 0;
                    ymax = 6000;
                }
                if (j == 5)
                {
                    ymin = 0;
                    ymax =700;
                }

                if (NodesList.GETSensores(j).GETligado()) // se estiver ligado
                {
                    //panel que albergará o grafico 
                    JPanel p = new JPanel();
                    p.setBounds(meio.getBounds());

                    if (NodesList.GETnumSensoresActivos() <= 4)
                    {
                        if (NodesList.GETnumSensoresActivos() <= 4  && NodesList.GETnumSensoresActivos() > 1)
                            sensorsMap[indice] = new SensorsMapGraf(true,NodesList.GETSensores(j).GETnome(),NodesList.GETSensores(j).GETunidade(),meio.getBounds(),(int)meio.getWidth()/2-20,(int)meio.getHeight()/2-20,ymin,ymax); // cria o sensor
                        else
                            sensorsMap[indice] = new SensorsMapGraf(true,NodesList.GETSensores(j).GETnome(),NodesList.GETSensores(j).GETunidade(),meio.getBounds(),(int)meio.getWidth()/NodesList.GETnumSensoresActivos(),(int)meio.getHeight()/NodesList.GETnumSensoresActivos()-35,ymin,ymax); // cria o sensor
                    }

                    if (NodesList.GETnumSensoresActivos() > 4 && NodesList.GETnumSensoresActivos() <= 8)
                        sensorsMap[indice] = new SensorsMapGraf(true,NodesList.GETSensores(j).GETnome(),NodesList.GETSensores(j).GETunidade(),p.getBounds(),(int)meio.getWidth()/4-15,(int)meio.getHeight()/2 - 45,ymin,ymax); // cria o sensor

                    if (NodesList.GETnumSensoresActivos() > 8 && NodesList.GETnumSensoresActivos() <= 12)
                        sensorsMap[indice] = new SensorsMapGraf(true,NodesList.GETSensores(j).GETnome(),NodesList.GETSensores(j).GETunidade(),p.getBounds(),(int)meio.getWidth()/4-20,(int)meio.getHeight()/3-35,ymin,ymax); // cria o sensor

                        if (indice == 0)
                        {
                            c.fill = GridBagConstraints.HORIZONTAL;
                            c.gridx = 0;
                            c.gridy = 0;
                            meio.add(sensorsMap[indice].GetGraph().GETDemoPanel(),c);  //adidionar o panel p ao panel meio
                        }
                        if (indice == 1)
                        {
                            c.fill = GridBagConstraints.HORIZONTAL;
                            c.gridx = 1;
                            c.gridy = 0;
                            meio.add(sensorsMap[indice].GetGraph().GETDemoPanel(),c);  //adidionar o panel p ao panel meio
                        }
                        if (indice == 2)
                        {
                            c.fill = GridBagConstraints.HORIZONTAL;
                            if (NodesList.GETnumSensoresActivos() <= 4)
                            {
                                c.gridx = 0;
                                c.gridy = 1;
                            }
                            else
                            {
                                c.gridx = 2;
                                c.gridy = 0;
                            }
                            meio.add(sensorsMap[indice].GetGraph().GETDemoPanel(),c);  //adidionar o panel p ao panel meio
                        }
                        if (indice == 3)
                        {
                            c.fill = GridBagConstraints.HORIZONTAL;
                            if (NodesList.GETnumSensoresActivos() > 4)
                            {
                                c.gridx = 3;
                                c.gridy = 0;
                            }
                            else
                            {
                                c.gridx = 1;
                                c.gridy = 1;
                            }
                            meio.add(sensorsMap[indice].GetGraph().GETDemoPanel(),c);  //adidionar o panel p ao panel meio
                        }
                        if (indice == 4)
                        {

                            c.fill = GridBagConstraints.HORIZONTAL;
                            c.gridx = 0;
                            c.gridy = 1;
                            meio.add(sensorsMap[indice].GetGraph().GETDemoPanel(),c);  //adidionar o panel p ao panel meio
                        }
                        if (indice == 5)
                        {
                            c.fill = GridBagConstraints.HORIZONTAL;
                            c.gridx = 1;
                            c.gridy = 1;
                            meio.add(sensorsMap[indice].GetGraph().GETDemoPanel(),c);  //adidionar o panel p ao panel meio
                        }
                        if (indice == 6)
                        {
                            c.fill = GridBagConstraints.HORIZONTAL;
                            c.gridx = 2;
                            c.gridy = 1;
                            meio.add(sensorsMap[indice].GetGraph().GETDemoPanel(),c);  //adidionar o panel p ao panel meio
                        }
                        if (indice == 7)
                        {
                            c.fill = GridBagConstraints.HORIZONTAL;
                            c.gridx = 3;
                            c.gridy = 1;
                            meio.add(sensorsMap[indice].GetGraph().GETDemoPanel(),c);  //adidionar o panel p ao panel meio
                        }
                        if (indice == 8)
                        {
                            c.fill = GridBagConstraints.HORIZONTAL;
                            c.gridx = 0;
                            c.gridy = 2;
                            meio.add(sensorsMap[indice].GetGraph().GETDemoPanel(),c);  //adidionar o panel p ao panel meio
                        }
                        if (indice == 9)
                        {
                            c.fill = GridBagConstraints.HORIZONTAL;
                            c.gridx = 1;
                            c.gridy = 2;
                            meio.add(sensorsMap[indice].GetGraph().GETDemoPanel(),c);  //adidionar o panel p ao panel meio
                        }
                        if (indice == 10)
                        {
                            c.fill = GridBagConstraints.HORIZONTAL;
                            c.gridx = 2;
                            c.gridy = 2;
                            meio.add(sensorsMap[indice].GetGraph().GETDemoPanel(),c);  //adidionar o panel p ao panel meio
                        }
                        if (indice == 11)
                        {
                            c.fill = GridBagConstraints.HORIZONTAL;
                            c.gridx = 3;
                            c.gridy = 2;
                            meio.add(sensorsMap[indice].GetGraph().GETDemoPanel(),c);  //adidionar o panel p ao panel meio
                        }
                    indice++;
                }
            }

            //adicionar o meio_cima e o meio panel principal
            principal.add(meio_cima);
            principal.add(meio);

            Tabs.addTab(s, principal); // adicionar tab
            String ss = new String();
            ss += NodesList.GETSourceNodeID();

            GraphicsMap.add(new CentralMap(ValorTeste-1, ValorTeste-1, sensorsMap,ss,NodesList.GETLogicalNodeID()));  // adição de uma Tab
            GraphicsMap.get(ValorTeste-1).SetnumSensorsToShow(NodesList.GETnumSensoresActivos());

            posx = 20;
            posy = 20;

            CheckBoxNodes[ValorTeste-1] = new JCheckBox(s);
            CheckBoxNodes[ValorTeste-1].setBackground(null);
            CheckBoxNodes[ValorTeste-1].setLayout(new GridLayout(ValorTeste, 1, 0, 30));
            CheckBoxNodes[ValorTeste-1].setBounds(posx, posy, aux1-60, 30);
            CheckBoxNodes[ValorTeste-1].setSelected(false);
            CheckBoxNodes[ValorTeste-1].setOpaque(true);
            CheckBoxNodes[ValorTeste-1].setVisible(true);

            //Evento a tratar
            CheckBoxNodes[ValorTeste-1].addItemListener (this);

            pan3.add (CheckBoxNodes[ValorTeste-1]);
            pan1.add (Tabs);
     }
  }

我已经尝试重新绘制 pan2 但它没有做任何事情。

提前致谢

4

1 回答 1

4

您发布了许多与问题完全无关的代码,并且代码令人困惑,但听起来好像您可能遇到了 Swing 并发问题。确保在后台线程上读取数据并在 Swing 事件线程或 EDT 上写入 Swing 组件。不过,为了获得更好的帮助,请发布与问题相关的代码。sscce效果最好。

此外,如果在 GUI 中添加或删除组件,请务必先调用revalidate()容器,然后再调用repaint().

于 2012-07-11T14:38:42.460 回答