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 に答える