【发布时间】:2016-03-04 04:26:42
【问题描述】:
我正在尝试创建一个 Fantasy Football 应用程序;我正在尝试根据在 JComboBox 中选择的内容来更改格式。但是,无论我做什么选择,它总是停留在第一个索引上,并且无论我做什么都不会更新到新的选择。
我有两个类,Fantasy 和 Dropdown(JComboBox 的 ActionListener),并且由于声明我需要使用两个单独的类,所以我不能将它们合并到一个类中。
public class Fantasy extends JFrame
{
String[] formationoptions = {"Select Formation", "4-4-2", "4-3-3", "3-5-2", "5-3-2", "3-4-3", "4-5-1"};
JComboBox<String> formation = new JComboBox<String>(formationoptions);
public Fantasy()
{
super("Fantasy Football");
this.setLayout(new BorderLayout());
this.setSize(400, 600);
this.add(formation, BorderLayout.NORTH);
formation.setSize(400, 25);
this.setVisible(true);
formation.addActionListener(new Dropdown((String) formation.getSelectedItem()));
}
}
Dropdown.java
public class Dropdown implements ActionListener
{
public String selected;
public String a = "Select Formation";
public String b = "4-4-2";
public String c = "4-3-3";
public String d = "3-5-2";
public String e = "5-3-2";
public String f = "3-4-3";
public String g = "4-5-1";
@Override
public void actionPerformed(ActionEvent e)
{
if (selected.equals(a))
{
System.out.println(a);
}
if (selected.equals(b))
{
System.out.println(b);
}
if (selected.equals(c))
{
System.out.println(c);
}
if (selected.equals(d))
{
System.out.println(d);
}
if (selected.equals(e))
{
System.out.println(e);
}
if (selected.equals(f))
{
System.out.println(f);
}
if (selected.equals(g))
{
System.out.println(g);
}
}
public Dropdown(String selected)
{
this.selected = selected;
}
目前Dropdown类不完整,它被设置为打印队形进行测试,但无论我做什么,我所做的任何选择都只会打印“选择队形”。
我做错了什么或错过了什么?
【问题讨论】:
标签: java swing actionlistener jcombobox