【发布时间】: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