我试图根据属性文件中定义的URL的数量创建动态JButton
和JLabel
。创建动态JLabel和JButton
我迄今为止尝试是:
AppResources.properties file
urls=http://google.com,http://stackoverflow.com,http://gmail.com
urlLabels=Internet Users,MPLS Users,Physical Access (Not Advised)
在我的Java程序我读的属性文件并基于comma separator
分割字符串,现在我需要相应地生成按钮和标签。像first URL Label --> first URL as Button
等。
到目前为止,我已经做到了这一点的方法是:
String url = properties.getProperty("urls");
String urlLabel = properties.getProperty("urlLabels");
String[] jButton = url.split(",");
String[] jLabel = urlLabel.split(",");
for (int i = 0; i < jLabel.length; i++) {
JLabel labels = new JLabel(jLabel[i]);
panel.add(labels);
for (int j = 0; j < jButton.length; j++) {
JButton button = new JButton(jButton[j]);
panel.add(button);
}
}
但它打印按钮三次的标签。如何解决这个问题?还有如何编写这些按钮的动作侦听器?
非常感谢..它的工作.. –