【问题标题】:For Loop based on item chosen in combobox基于组合框中选择的项目的循环
【发布时间】:2014-04-03 17:07:51
【问题描述】:

例如,我在组合框中有这样的项目:

    cIndexchoice.addItem("AAA");
    cIndexchoice.addItem("BBB");
    cIndexchoice.addItem("CCC");
    cIndexchoice.addItem("DDD");
    cIndexchoice.addItem("EEE");
    cIndexchoice.addItem("FFF");
    cIndexchoice.addItem("GGG");
    cIndexchoice.addItem("HHH");
    cIndexchoice.addItem("III");

combobox 中的每个组件都有等效的字符串数组,如下所示:

      String[] AAA = { some strings here };
      String[] BBB = { some strings here };
      String[] CCC = { some strings here };

现在我在这样的数组上进行循环工作(例如 AAA):

        for (int i = 0; i < AAA.length; i++)
        {
            ConstructorURL spolka = new ConstructorURL(startDate, endDate,
                    AAA[i]);
            DataGeting new1 = new DataGeting(spolka.constructURL(),
                    HowManyDaysStrdv, i);
            listEntities[i] = new1;
        }

并且循环应该对用户在组合框中选择的内容起作用,我不知道该怎么做。谁能帮帮我?

【问题讨论】:

  • 你能发帖code that compiles吗?目前尚不清楚现在会发生什么,为什么会出错,以及您希望发生什么。请解释一下。
  • 我的主类:pastebin.com/4hm4ukDj 请查看第 225、226、229、240、246 行。我知道在循环中我应该使用数组而不是字符串,并且代码无法编译。我可以使用 if 但它是非常丑陋的解决方案,我正在寻找一些聪明的东西。而且不知道该怎么做。
  • 再次阅读我在之前评论中发布的链接。您可以在不到 400 行中显示问题。发布一个最小“可编译”代码,显示问题是什么以及您希望它做什么
  • 好吧抱歉我没有注意到你的超链接:)
  • 好吧,也许现在:pastebin.com 如果用户选择表单组合框 BBB,那么 JTextArea 会显示 BBB 数组中的每个元素。我应该在取决于组合框的循环中做什么?

标签: java swing for-loop combobox


【解决方案1】:

创建一个包含两个属性的自定义对象:

  1. 组合框中显示的值
  2. 与该值相关的数组或字符串

然后您需要创建一个自定义渲染器,以便在组合框中显示“值”。请参阅Combo Box With Renderer 了解如何执行此操作。

然后,您将 ActionListener 添加到组合框,并在处理代码中获取所选项目并将其转换为您的自定义类。现在您可以从您的类中访问字符串数组并将它们显示在文本区域中。

【讨论】:

  • 老实说我不明白:(好的,我创建的新对象包含组合框名称和数组 public SectorSelected(String str, String[] sectorList) 但我不知道接下来会发生什么。跨度>
  • @user3361567 i have no idea what next. - 您是否将 SectorSelected 对象添加到组合框并创建自定义渲染器?
  • 不,但我找到了另一种方法,我将使用 hashmap,键将是来自组合框的值。
  • @user3361567,是的,这是我要建议的另一种方法。思路是一样的,但是使用自定义 Object 的重点是所有数据都在对象中,而不是像 Hashmap 这样的外部数据源中。
  • 是的,感谢您的宝贵时间 :) 但我是 java 初学者,最简单的方法是使用 hashmap。
【解决方案2】:

以及使用hashmap的解决方案:

public class testing extends JFrame implements ActionListener
{

    private JTextArea taResultArea;
    private JComboBox cIndexchoice;
    private JButton Start;

    public testing()
    {
        setSize(700, 700);
        setLayout(null);
        taResultArea = new JTextArea();
        taResultArea.setEditable(false);
        taResultArea.setBounds(300, 15, 350, 600);
        add(taResultArea);

        Start = new JButton("Run Simulation");
        setLayout(null);
        Start.setBounds(20, 500, 150, 50);
        // adding buttons to the main window
        add(Start);
        Start.addActionListener(this);

        String[] listOfElements = { "AAA", "BBB", "CCC" };
        cIndexchoice = new JComboBox(listOfElements);
        cIndexchoice.setBounds(20, 50, 100, 20);
        add(cIndexchoice);
        cIndexchoice.addActionListener(this);

    }

    public static void main(String[] args)
    {
        testing window = new testing();
        window.setVisible(true);

    }

    @Override
    public void actionPerformed(ActionEvent e)
    {
        if (e.getSource() == Start)
        {
            String[] AAA = { "A", "AA", "AAA", "AAAA", "AAAAA", "AAAAAA" };
            String[] BBB = { "B", "BB", "BBB", "BBBB", "BBBBB", "BBBBBB" };
            String[] CCC = { "C", "CC", "CCC", "CCCC", "CCCCC", "CCCCCC" };

            HashMap<String, String[]> map = new HashMap<String, String[]>();
            map.put("AAA", AAA);
            map.put("BBB", BBB);
            map.put("CCC", CCC);

            String[] SectorSelected = map.get(cIndexchoice.getSelectedItem().toString());

            System.out.println(Arrays.toString(SectorSelected));

            for (int i = 0; i < SectorSelected.length; i++)
            {
                taResultArea.append(SectorSelected[i] + "\n");
            }
        }

    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-20
    • 1970-01-01
    • 1970-01-01
    • 2013-12-04
    相关资源
    最近更新 更多