Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
关于JavaGUI中的ItemEvent_Java_Swing_User Interface_Jcombobox - Fatal编程技术网

关于JavaGUI中的ItemEvent

关于JavaGUI中的ItemEvent,java,swing,user-interface,jcombobox,Java,Swing,User Interface,Jcombobox,我正在用JavaGUI做一个小程序 以下是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 { Flo

我正在用JavaGUI做一个小程序 以下是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一起工作的吗?
谢谢

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

  itemStateChanged(this);


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

pizzaBox.addItemListener(this);

有关详细信息,请参见此。

问题:

1.您没有为
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);
      }
   }
}

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

itemStateChanged(ItemEvent[] list)
给出了错误

如果要处理
itemStateChanged
,则需要实现
itemlistener
并将
actionListner
作为

pizzaBox.addItemListener(this);
还将
ItemListener
的处理程序方法添加为

public void itemStateChanged(....)
{
    //Handling Code  goes here
}
或者您也可以实现
ActionListner
有关如何使用combobox的更多信息,请访问

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