我一直在试图弄清楚如何制作一个基于数组内容动态创建的弹出窗口,并且我几乎可以确定我错过了一些至关重要的东西帮助我解决并充分理解这个问题。用JRadioButtons使用HashMap填充JPanel
我究竟在做什么?
我有一个程序,通过某些目录循环,收集所有的文件夹名称,并将其存储在ArrayList
。当我尝试使用该动态创建窗口时出现问题ArrayList
。我不确定如何解决这个问题。
什么是通过流程
我目前我有3个班。视图,模型和控制类。包含文件夹的数组存储在模型类中。我通过控制类检索它。我在ActionListener
以及HashMap
内创建了一个新的JPanel。我循环通过HashMap
添加String
名称和JRadioButton
我试图填充窗口,但我真的不知道如何。
这里是我的工作的一段代码:
public void actionPerformed(ActionEvent e) {
if (e.getSource() == theView.viewButton) {
System.out.println("View Button clicked");
theView.setBotTextArea("");
theView.setBotTextArea("Viewing...");
JPanel radioPanel = new JPanel();
// Method that gather folder names and stores it in an array
theModel.listAllFolders();
// Make the categories array and store the names
ArrayList<String> categories = theModel.getListOfCategories();
// Create a hashmap with names and JRadioButtons
HashMap<String, JRadioButton> buttonMap = new HashMap<String, JRadioButton>();
// loop to fill up the HashMap
for (int i = 0; i < categories.size(); i++) {
buttonMap.put(categories.get(i), new JRadioButton(categories.get(i)));
}
for (Entry<String, JRadioButton> entry : buttonMap.entrySet()) {
// Not sure how to retrieve the hashmap data to create and
fill up the window
}
}
我新的极端,以包含HashMap(我努力学习的话),所以我甚至不知道这是否是一个好主意开始。我一直坚持这项任务近3天。在过去,我试图使用数组来完成类似的任务,但我几乎可以肯定,这是我的一个巨大的逻辑错误,阻止我完成它。
我很感谢在这个问题上的任何新的见解。
为什么'ArrayList - > HashMap - > JRadioButton'?为什么不是更简单的'ArrayList - > JRadioButton'?或者,如果你需要创建一个HashMap,那么为什么不用一个for循环来创建JRaidoButton,它填充HashMap,将JRradioButton放到一个ButtonGroup中,并将JRadioButton放到一个JPanel上你将JRadioButtons添加到JPanel?)? –
我的逻辑是我将来可能会在右侧'JRadioButton'中分配正确的名称。 'HashMap'似乎是防止这种情况发生的最佳选择。尽管如此,我可能会过度复杂化。编辑:你介意在你的编辑中显示一段代码吗?这几乎是我一直想要做的。 – Lotix
您可能会为我们过度复杂化它。再次,请给我们更多的细节。我敢肯定,你知道如何将JRadioButtons添加到JPanel,对吗?只需调用'myPanel.add(myRadioButton)',所以我不确定将您的JRadioButton放到JPanel中有什么困惑。 –