【问题标题】:Creating JButtons and addActionListener dynamically and pass JSON GetString动态创建 JButtons 和 addActionListener 并传递 JSON GetString
【发布时间】:2017-09-17 08:42:53
【问题描述】:

我正在尝试自动填充按钮的 actionListener,但出现错误 jObject = jArray.getJSONObject(i) generator() 函数返回一个 JSONArray。

我也想打印按钮[i] = new JButton("pfdfs"); as 按钮[i] = new 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();
                    }

                   }

    });
    }

等待您的回复

【问题讨论】:

  • 请阅读minimal reproducible example 并完善您的问题 - 例如提供错误消息。
  • 错误在这里 jObject = jArray.getJSONObject(i);错误消息我在封闭范围中定义的局部变量必须是最终的或有效的最终这是我的第一个问题:D
  • 请更新您的问题 - 不要在 cmets 中提供更多详细信息。
  • 您是否尝试过搜索错误消息?这是一个超级常见的问题,之前在这里被问过无数次。
  • 问题的关键是从json数组中获取的方式。 JSONObject jObject = null;试试 { jObject = jArray.getJSONObject(i);

标签: java arrays json eclipse swing


【解决方案1】:
    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);

【讨论】:

    猜你喜欢
    • 2012-06-09
    • 1970-01-01
    • 1970-01-01
    • 2023-03-06
    • 1970-01-01
    • 2018-11-30
    • 1970-01-01
    • 2018-09-05
    • 1970-01-01
    相关资源
    最近更新 更多