對於一個任務,我正在嘗試創建一個Fantasy Football應用程序;我試圖根據JComboBox中選擇的內容來改變陣型。但是,無論我做什麼選擇,它總是停留在第一個索引上,不會更新爲新的選擇。我有兩個類,Fantasy和Dropdown(JComboBox的ActionListener),既然它聲明我需要使用兩個單獨的類,我不能將它們合併到一個類中。getSelectedItem/JComboBox的索引不更新
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;
}
目前下拉類是不完整的,並且它被設置爲打印測試的形成,但無論我做什麼,我只得到「選擇形成「印刷爲我做的任何選擇。
我在做什麼錯誤或失蹤?
如何更新的選擇的價值?當他們在同一個班級時我沒有遇到問題,但是當我將他們分開時纔開始發生。 – Jinjinbug