2017-09-17 15 views
-1

ボタンのactionListenerを自動的に設定しようとしていますが、エラーが発生します。 jObject = jArray.getJSONObject(i)generator()関数はJSONArrayを返します。JButtonとaddActionListenerを動的に作成してJSONを渡すGetString

また、ボタン[i] =新しいJButton( "pfdfs")を印刷したいと思います。ボタンとして[i] =新しいJButton( "i");

JButton b1= null, b2= null, b3= null, b4= null,b5 = null,b6= null,b7= null,b8= null,b9= null,b0= null; 
    JButton[] buttons = {b1, b2, b3, b4, b5,b6,b7,b8,b9,b0}; 
    JSONArray jArray = generator(); 
    //JSONObject jObject = null; 
    for (int i = 0; i <buttons.length;i++) { 
     buttons[i] = new JButton("pfdfs"); 
     btnPnl.add(buttons[i]); 
     buttons[i].addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       //JSONObject jObject = null; 
       JSONObject jObject = null; 
        // jObject = null; 
        try { 
        jObject = jArray.getJSONObject(i); 

ここでエラーが発生しました。 jObject = jArray.getJSONObject(i);私は外側のスコープで定義されたエラーメッセージローカル変数は、最終的なまたはあなたの応答を待ってい

    } catch (JSONException e2) { 
         // TODO Auto-generated catch block 
         e2.printStackTrace(); 
        } 


        String name = null; 
        try { 
         name= jObject.getString("Name"); 
        } catch (JSONException e2) { 
         // TODO Auto-generated catch block 
         e2.printStackTrace(); 
        } 
        JLabel label2 = new JLabel(); 
        label2.setText(pyetja); 
        label2.setFont(new Font("Arial", Font.PLAIN, 22)); 
        name.removeAll(); 
        name.repaint(); 
        name.add(label2); 

        Image image = null; 
        String photoID = null; 
        try { 
         fotoID = jObject.getString("photoID"); 
        } catch (JSONException e2) { 
         // TODO Auto-generated catch block 
         e2.printStackTrace(); 
        } 
        File image2 = new File("D:\\photoFolder\\IMAGE ("+photoID+").jpg"); 
        try { 
         image = ImageIO.read(image2); 
        } catch (IOException e2) { 
         // TODO Auto-generated catch block 
         e2.printStackTrace(); 
        } 
        JLabel image= new JLabel(new ImageIcon(image)); 
        Photo.removeAll(); 
        Photo.repaint(); 
        Photo.add(image); 


        try { 
         String test= jObject.getString("test"); 
        } catch (JSONException e1) { 
         // TODO Auto-generated catch block 
         e1.printStackTrace(); 
        } 

        } 

    }); 
    } 

効果的に最終でなければなりません

+0

[MCVE]読んで、あなたの質問を強化してください - 例えば、エラーメッセージを提供することにより、 。 – GhostCat

+0

ここにエラーがあります jObject = jArray.getJSONObject(i); エラーメッセージ 囲みスコープで定義されたローカル変数iは、最終的に、または効果的に最終的でなければなりません。 ここに私の最初の質問があります:D – user8269715

+0

コメントをさらに更新してください。 – GhostCat

答えて

0
for (int i = 0; i <buttons.length;i++) { 
    final int k=i; //should create a final variable to get index. 
    buttons[i] = new JButton("pfdfs"); 
    btnPnl.add(buttons[i]); 
    buttons[i].addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      //JSONObject jObject = null; 
      JSONObject jObject = null; 
       // jObject = null; 
       try { 
       jObject = jArray.getJSONObject(k); 
関連する問題