关于java GUI中的ItemEvent

时间:2013-12-07 07:22:17

标签: java swing user-interface jcombobox

我正在用Java GUI做一个小程序 这是mu代码:

    // DebugFourteen3
// User selects pizza topping and sees price
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

//use correct spelling of class name
public class DebugFourteen3 extends JFrame
{ 
   FlowLayout flow = new FlowLayout();
   JComboBox pizzaBox = new JComboBox();
   JLabel toppingList = new JLabel("Topping List");
   JLabel aLabel = new JLabel("Paulos's American Pie");
   JTextField totPrice = new JTextField(10);
   int[] pizzaPrice = {7,10,10,8,8,8,8};
   int totalPrice = 0;
   String output;
   int pizzaNum;
   public DebugFourteen3()
   {
      super("Pizza List");
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setLayout(flow);
      add(toppingList);
      pizzaBox.addItem("cheese");
      pizzaBox.addItem("sausage");
      pizzaBox.addItem("pepperoni");
      pizzaBox.addItem("onion");
      pizzaBox.addItem("green pepper");
      pizzaBox.addItem("green olive");
      pizzaBox.addItem("black olive");
      add(pizzaBox);
      add(aLabel);
      add(totPrice);
      itemStateChanged(this);
   }

   public static void main(String[] arguments)
   {
      JFrame frame = new DebugFourteen3();
      frame.setSize(200, 150);
      frame.setVisible(true);
   }

   public void itemStateChanged(ItemEvent[] list)
   {
      Object source = list.getSource();
      if(source == pizzaBox)
      {
         int pizzaNum = pizzaBox.getSelectedIndex();
         totalPrice = pizzaPrice[pizzaNum];
         output = "Pizza Price $" + totalPrice;
         totPrice.setText(output);
      }
   }
}

编译器在第35行遇到错误,它说itemStateChanged()应该接收一个类型为ItemEvent []的参数但是我传递“this”(它自己的类)

任何人都可以解释itemStateChanged如何与JComboBox一起使用? 感谢

4 个答案:

答案 0 :(得分:0)

您必须在组合框中添加侦听器,而不是直接调用itemStateChanged方法。添加侦听器将在click事件发生时启用回调,并且jvm将在为事件注册组合框时自动调用itemStateChanged方法。替换此行

  itemStateChanged(this);

pizzaBox.addItemListener(this);

答案 1 :(得分:0)

首先,您需要使用ItemListener实施JFrame。与pizzaBox添加以下行,这将通知覆盖public void itemStateChanged(ItemEvent ev)方法ItemListener

pizzaBox.addItemListener(this);

有关详细信息,请参阅此tutorial

答案 2 :(得分:0)

问题:

1.您没有为itemListener注册JComboBox 2. itemStateChanged事件将ItemEvent作为参数而非ItemEvent[]数组

1.替换这个:

itemStateChanged(this);

用这个:

pizzaBox.addItemListener(this);

2.替换这个:

public void itemStateChanged(ItemEvent[] list)

有了这个:

public void itemStateChanged(ItemEvent list)

完整代码:

// DebugFourteen3
// User selects pizza topping and sees price
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

//use correct spelling of class name
public class DebugFourteen3 extends JFrame
{ 
   FlowLayout flow = new FlowLayout();
   JComboBox pizzaBox = new JComboBox();
   JLabel toppingList = new JLabel("Topping List");
   JLabel aLabel = new JLabel("Paulos's American Pie");
   JTextField totPrice = new JTextField(10);
   int[] pizzaPrice = {7,10,10,8,8,8,8};
   int totalPrice = 0;
   String output;
   int pizzaNum;
   public DebugFourteen3()
   {
      super("Pizza List");
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setLayout(flow);
      add(toppingList);
      pizzaBox.addItem("cheese");
      pizzaBox.addItem("sausage");
      pizzaBox.addItem("pepperoni");
      pizzaBox.addItem("onion");
      pizzaBox.addItem("green pepper");
      pizzaBox.addItem("green olive");
      pizzaBox.addItem("black olive");
      add(pizzaBox);
      add(aLabel);
      add(totPrice);
      pizzaBox.addItemListener(this);
   }

   public static void main(String[] arguments)
   {
      JFrame frame = new DebugFourteen3();
      frame.setSize(200, 150);
      frame.setVisible(true);
   }

   public void itemStateChanged(ItemEvent list)
   {
      Object source = list.getSource();
      if(source == pizzaBox)
      {
         int pizzaNum = pizzaBox.getSelectedIndex();
         totalPrice = pizzaPrice[pizzaNum];
         output = "Pizza Price $" + totalPrice;
         totPrice.setText(output);
      }
   }
}

答案 3 :(得分:0)

此处thisDebugFourteen3不是ItemEvent类型。这就是为什么

itemStateChanged(ItemEvent[] list)

给出错误。

如果您要处理itemStateChanged,则需要实施ItemLisntener并将actionListner添加为

pizzaBox.addItemListener(this);

还将ItemLisntener的Handler方法添加为

public void itemStateChanged(....)
{
    //Handling Code  goes here
}

或者您也可以实施ActionListner 有关如何使用组合框的更多信息,请访问here