2015-10-18 32 views
1

新來java和我無法看到爲什麼我的動作偵聽器不工作的jcombobox。我想我已經跟着網上的其他例子getSelectedItem,但沒有發生。 僅供參考,我的項目是一個單位轉換器(使用MVC ..喜歡,但這不是我的優先事項)。 任何援助非常感謝。 謝謝,西蒙。JComboBox getSelectedItem

import java.awt.event.ActionEvent; 
    import java.awt.event.ActionListener; 

    import javax.swing.*; 


    public class UnitConverterView extends JFrame{ 

    //variables and components 
    private static final long serialVersionUID = -4673040337179571462L; 
    private JComboBox<String> unitCategory; 

    private JTextField fromValue = new JTextField(7); 
    private JComboBox<String> convertFrom; 
    private JLabel equalsLabel = new JLabel(" = "); 

    private JTextField toValue = new JTextField(7); 
    private JComboBox<String> convertTo; 


    //constructor 
    UnitConverterView(){ 
    //set up the view and components 

     JPanel unitPanel = new JPanel(); 

     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     this.setSize(600,300); 

     String[] categories = {"Length","Weight","Speed","Temperature"}; 
     unitCategory = new JComboBox<>(categories); 

     String[] tofromValues = {" "}; 
     convertFrom = new JComboBox<>(tofromValues); 
     convertTo = new JComboBox<>(tofromValues); 


     unitPanel.add(unitCategory); 

     unitPanel.add(fromValue); 
     unitPanel.add(convertFrom); 
     unitPanel.add(equalsLabel); 
     unitPanel.add(toValue); 
     unitPanel.add(convertTo); 

     this.add(unitPanel); 

    } 

    //get value to convert from 
    public int getMeasurement() { 
     return Integer.parseInt(fromValue.getText()); 
    } 

    //listen for unitCategory to be selected 
    void addUnitCategoryListener(ActionListener listenForUnitCategory) { 
     unitCategory.addActionListener(listenForUnitCategory); 
    } 

class UnitCatListener implements ActionListener { 

     public void actionPerformed(ActionEvent e) { 

      /*String unitSelected = (String) unitCategory.getSelectedItem(); 
      if (e.getSource() == unitCategory) { 
       String unitName = (String) unitCategory.getSelectedItem(); 
       System.out.println("UnitName = " + unitName); 
       changeText(unitName); 
      }*/ 

      JComboBox cb = (JComboBox)e.getSource(); 
      String unitName = (String) cb.getSelectedItem(); 
      System.out.println("UnitName = " + unitName); 

     } 

     void changeText(String name) { 
      toValue.setText(name); 
     } 

    } 



} 
+1

據我可以告訴你,永遠不要添加監聽器。你需要調用'unitCategory.addActionListener(new UnitCatListener())'。 – WillShackleford

+0

感謝您的協助。 –

回答

1

你已經聲明瞭一個方法addUnitCategoryListener()來向偵聽器註冊偵聽器,但是你永遠不會調用這個方法。這就是爲什麼聽衆永遠不會註冊。

在構造函數的末尾添加以下行,那麼你應該罰款:

addUnitCategoryListener(new UnitCatListener()); 
1

要簡單解決你的問題,叫你創建註冊的組件上的監聽方法。添加到您的構造函數:

addUnitCategoryListener(new UnitCatListener()); 

不過,也有你想知道的幾件事:

  • ItemListener通常會做比ActionListenerJComboBox更好的工作。如果用戶選擇已經選擇的項目(基本上什麼都不做),則前一個不會觸發事件。通常在這些情況下你不需要做任何事情。
  • 你並不需要一個額外的方法,只是註冊的監聽器,你可以直接添加到您的構造線

    unitCategory.addActionListener(new UnitCatListener()); 
    

    和刪除自定義方法。

  • 方法changeTextgetMeasurement從不使用。
  • 使用參數化類型:而不是JComboBox使用JComboBox<String>
  • 你不需要equalsLabel作爲一個字段 - 一個局部變量會做 - 因爲你不需要在以後的任何地方引用它(除非你計劃在運行時改變標籤的屬性)。
+0

感謝您的幫助。 getMeasurement是獲取(例如英寸)的值以從中轉換。剛剛意識到int對此沒有好處。 changeText輸出計算值(例如mm)。我不確定你的意見是否等於標籤,因此將進一步研究。 –

+0

@SimonMills搜索[here](https://www.google.co.il/search?client=opera&q=java+field+vs+local+variable)爲什麼'equalLabel'應該可能是一個局部變量。 – user1803551