[英]For Loop based on item chosen in combobox
例如我在组合框中有这样的项目:
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");
组合框中的每个组件都具有等效的字符串数组,如下所示:
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;
}
和循环应该对用户在组合框中选择的内容起作用,而我不知道该怎么做。 谁能帮我?
创建一个包含两个属性的自定义对象:
然后,您将需要创建一个自定义渲染器,以便在组合框中显示“值”。 有关执行此操作的方法,请参见带有渲染器的组合框 。
然后,将ActionListener添加到组合框,并在处理代码中获得所选项目并将其转换为自定义类。 现在,您可以从您的类访问字符串数组,并将其显示在文本区域中。
以及使用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");
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.