我正在用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一起使用? 感谢
答案 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)
此处this
,DebugFourteen3
不是ItemEvent
类型。这就是为什么
itemStateChanged(ItemEvent[] list)
给出错误。
如果您要处理itemStateChanged
,则需要实施ItemLisntener
并将actionListner
添加为
pizzaBox.addItemListener(this);
还将ItemLisntener
的Handler方法添加为
public void itemStateChanged(....)
{
//Handling Code goes here
}
或者您也可以实施ActionListner
有关如何使用组合框的更多信息,请访问here