2015-10-18 60 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