使用 HashMap 使用 JRadioButtons 填充 JPanel
Filling a JPanel with JRadioButtons using HashMap
我一直在努力弄清楚如何制作一个基于数组内容动态创建的弹出窗口 window,我几乎可以肯定我遗漏了一些可能有用的重要信息我解决并完全理解这个问题。
我到底想做什么?
我有一个循环遍历特定目录的程序,收集所有文件夹名称并将其存储在 ArrayList
中。当我尝试使用 ArrayList
动态创建 window 时出现问题。我不确定如何解决这个问题。
我目前的流程是什么
我有 3 个 class。视图、模型和控制 class。包含文件夹的数组存储在模型 class 中。我通过控件 class 检索它。我在我的 ActionListener
中创建了一个新的 JPanel 以及一个 HashMap
。我循环 HashMap
添加 String
名称和 JRadioButton
我尝试填充 window 但我真的不知道如何。
这是我正在使用的代码片段:
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
}
}
我是 HashMaps 的新手(我正在尝试学习它)所以我什至不确定开始使用它是否是个好主意。我已经坚持这项任务将近 3 天了。过去我尝试使用数组来完成类似的任务,但我几乎可以肯定这是我的一个巨大的逻辑错误,阻止我完成它。
如果您对此事有任何新见解,我将不胜感激。
ArrayList<String> categories = theModel.getListOfCategories();
for (String val : categories) {
JRadioButton jb = new JRadioButton(val);
radioPanel.add(jb);
}
//now you need to add the radioPanel JPanel to an existing Container, or call setContentPane(radioPanel);
如果您在 JRadioButton 中显示的文本与放置在 HashMap 中的文本相同,我认为不需要 HashMap。只需确保使用正确的文本设置 JRadioButton 的 actionCommand 字符串,将所有 JRadioButton 添加到同一个 ButtonGroup,并且当您需要选择时,从 ButtonGroup 的 getSelection() 方法返回的 ButtonModel 获取 actionCommand。
例如
for (String text : fileList) {
JRadioButton btn = new JRadioButton(text);
btn.setActionCommand(text); // radiobuttons don't do this by default
buttonGroup.add(btn); // ButtonGroup to allow single selection only
myRadioPanel.add(btn); // JPanel usually uses a GridLayout
}
// if myRadioPanel is already in the GUI, then revalidate and repaint it
稍后获取选择(如果通过另一个按钮的 ActionListener 完成:
ButtonModel model = buttonGroup.getSelection();
if (model != null) {
selectedText = model.getActionCommand();
}
或者如果使用添加到单选按钮的 ActionListener,则只需获取 ActionEvent 的 actionCommand 属性。
至于将 JRadioButtons 添加到 JPanel,我很确定您已经知道如何执行此操作。如果某个步骤让您感到困惑,那是您还没有告诉我们。
我一直在努力弄清楚如何制作一个基于数组内容动态创建的弹出窗口 window,我几乎可以肯定我遗漏了一些可能有用的重要信息我解决并完全理解这个问题。
我到底想做什么?
我有一个循环遍历特定目录的程序,收集所有文件夹名称并将其存储在 ArrayList
中。当我尝试使用 ArrayList
动态创建 window 时出现问题。我不确定如何解决这个问题。
我目前的流程是什么
我有 3 个 class。视图、模型和控制 class。包含文件夹的数组存储在模型 class 中。我通过控件 class 检索它。我在我的 ActionListener
中创建了一个新的 JPanel 以及一个 HashMap
。我循环 HashMap
添加 String
名称和 JRadioButton
我尝试填充 window 但我真的不知道如何。
这是我正在使用的代码片段:
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
}
}
我是 HashMaps 的新手(我正在尝试学习它)所以我什至不确定开始使用它是否是个好主意。我已经坚持这项任务将近 3 天了。过去我尝试使用数组来完成类似的任务,但我几乎可以肯定这是我的一个巨大的逻辑错误,阻止我完成它。
如果您对此事有任何新见解,我将不胜感激。
ArrayList<String> categories = theModel.getListOfCategories();
for (String val : categories) {
JRadioButton jb = new JRadioButton(val);
radioPanel.add(jb);
}
//now you need to add the radioPanel JPanel to an existing Container, or call setContentPane(radioPanel);
如果您在 JRadioButton 中显示的文本与放置在 HashMap 中的文本相同,我认为不需要 HashMap。只需确保使用正确的文本设置 JRadioButton 的 actionCommand 字符串,将所有 JRadioButton 添加到同一个 ButtonGroup,并且当您需要选择时,从 ButtonGroup 的 getSelection() 方法返回的 ButtonModel 获取 actionCommand。
例如
for (String text : fileList) {
JRadioButton btn = new JRadioButton(text);
btn.setActionCommand(text); // radiobuttons don't do this by default
buttonGroup.add(btn); // ButtonGroup to allow single selection only
myRadioPanel.add(btn); // JPanel usually uses a GridLayout
}
// if myRadioPanel is already in the GUI, then revalidate and repaint it
稍后获取选择(如果通过另一个按钮的 ActionListener 完成:
ButtonModel model = buttonGroup.getSelection();
if (model != null) {
selectedText = model.getActionCommand();
}
或者如果使用添加到单选按钮的 ActionListener,则只需获取 ActionEvent 的 actionCommand 属性。
至于将 JRadioButtons 添加到 JPanel,我很确定您已经知道如何执行此操作。如果某个步骤让您感到困惑,那是您还没有告诉我们。