[英]about ItemEvent in java GUI
I'm doing a small program with Java GUI here is mu code: 我正在用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);
}
}
}
the compiler gets me error on line 35, it says itemStateChanged() should receive a argument which type is ItemEvent[] but I'm passing "this"(the class it self) 编译器在第35行给我错误,它说itemStateChanged()应该接收一个类型为ItemEvent []的参数,但我正在传递“ this”(它本身是类)
can anyone explain how the itemStateChanged works with the JComboBox? 谁能解释itemStateChanged如何与JComboBox一起使用? thanks
谢谢
You have to add the listener on the combo box instead of calling the itemStateChanged
method directly. 您必须在组合框上添加侦听器,而不是直接调用
itemStateChanged
方法。 Adding the listener will enable the callbacks when the click event happens, and jvm will call the itemStateChanged
method automatically as combo box is registered for the event. 添加监听器将在click事件发生时启用回调,并且jvm将在为该事件注册组合框时自动调用
itemStateChanged
方法。 Replace this line 替换此行
itemStateChanged(this);
with 与
pizzaBox.addItemListener(this);
At first, you need to implements ItemListener
with JFrame
. 首先,您需要使用
JFrame
实现ItemListener
。 Than add following line with pizzaBox
, this will notify overrided public void itemStateChanged(ItemEvent ev)
method ItemListener
. 与
pizzaBox
添加以下行pizzaBox
,这将通知重写的public void itemStateChanged(ItemEvent ev)
方法ItemListener
。
pizzaBox.addItemListener(this);
Problem : 问题:
1.you are not registering itemListener
for JComboBox
1.您没有为
JComboBox
注册itemListener
2. itemStateChanged
event takes ItemEvent
as argument not ItemEvent[]
array 2.
itemStateChanged
事件将ItemEvent
作为参数而不是ItemEvent[]
数组
1.Replace this : 1.替换这个:
itemStateChanged(this);
with this: 有了这个:
pizzaBox.addItemListener(this);
2.Replace This: 2.替换此:
public void itemStateChanged(ItemEvent[] list)
With this: 有了这个:
public void itemStateChanged(ItemEvent list)
Complete Code: 完整的代码:
// 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);
}
}
}
Here this
ie DebugFourteen3
is not a type ItemEvent
. 下面
this
即DebugFourteen3
不是一个类型ItemEvent
。 Thats why 这就是为什么
itemStateChanged(ItemEvent[] list)
gives error. 给出错误。
If you want to handle itemStateChanged
then you need to implement ItemLisntener
and add actionListner
to it as 如果要处理
itemStateChanged
则需要实现ItemLisntener
并将actionListner
添加为
pizzaBox.addItemListener(this);
also add Handler method of ItemLisntener
as 还将
ItemLisntener
Handler方法添加为
public void itemStateChanged(....)
{
//Handling Code goes here
}
Or you can implement ActionListner
also For more information on how to use combobox you can visit here 或者,您也可以实现
ActionListner
有关如何使用组合框的更多信息,请访问此处
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.