2016-03-04 33 views
-1

對於一個任務,我正在嘗試創建一個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; 
     } 

目前下拉類是不完整的,並且它被設置爲打印測試的形成,但無論我做什麼,我只得到「選擇形成「印刷爲我做的任何選擇。

我在做什麼錯誤或失蹤?

回答

1

所以,與其然後打印selected這永遠不會改變,你需要檢查JComboBox本身,並要求它選擇的值是什麼...

public class Dropdown implements ActionListener { 

    public String selected; 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     JComboBox cb = (JComboBox) e.getSource(); 
     System.out.println(cb.getSelectedItem()); 
    } 

    public Dropdown(String selected) { 
     this.selected = selected; 

    } 
} 

然後,您可以,更新selected值的值這實際上是由JComboBox

0

您從不更新selected的值以響應組合框更改。

+0

如何更新的選擇的價值?當他們在同一個班級時我沒有遇到問題,但是當我將他們分開時纔開始發生。 – Jinjinbug

1

您可以使用ItemListener來實現此目的。

formation.addItemListener(new Dropdown()); 

和變化的Dropdown類定義這個

public class Dropdown implements ItemListener { 
    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 itemStateChanged(ItemEvent e) { 
     if (e.getStateChange() == ItemEvent.SELECTED) { 
      String selected = ((JComboBox)e.getSource()) 
           .getSelectedItem().toString(); 
      System.out.println(selected); 
      // write here if else ladder or switch case 
     } 
    } 
} 

它會工作。

UPDATE

這可以通過ActionListener太實現。我在這裏列出了ActionListenerItemListener中的一些差異。

ActionListener

  • ActionListener當您更改元素的組合框,即使元素是相同以前被調用。
  • 使用動作偵聽器actionPerformed方法在您更改元素時只調用一次。

ItemListener

  • ItemListener當您更改組合框中的元素被調用,這是不一樣的前面。
  • 當你使用ItemListener,itemStateChanged方法被調用兩次,所以你必須檢查條件if (e.getStateChange() == ItemEvent.SELECTED) {,否則該方法在你選擇新元素時執行兩次。
+0

爲什麼「ItemListener」比「ActionListener」更好?你真的需要知道彈出窗口中選定的項目何時更改嗎?基本上,你所做的一切正是ActionListener所做的,而不需要檢查事件的原因 – MadProgrammer

+0

Oooh,我喜歡comparions,不錯 – MadProgrammer

+0

如果你沒有添加第一個評論..我從來沒有檢查過差別..所以謝謝你。 – ELITE

0

將這行代碼插入到程序中,以便每次運行該代碼段時都會刷新jComboBox選擇。這樣,您總能獲得當前選擇的電流值的JComboBox:

jComboBox.setName(""); 

例如,在你的代碼中插入它像這樣:

public Dropdown(String selected) 
    { 
     this.selected = selected; 
     jComboBoxcb.setName(""); 
    }