为什么我的组合框不能执行我在ItemListener中声明的内容?当我单击组合框中的一项时,程序挂起,我需要关闭整个BlueJ。请看一下我的代码中有什么地方错了
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class HulaHoops {
private Scanner inp = new Scanner(System.in);
public static void main(String[]args) {
EventQueue.invokeLater(new Runnable()
{
@Override
public void run()
{
new HulaHoops();
}
});
}
public HulaHoops() {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
String choices[] = {"Shoes","Comb","Ball"};
JComboBox combo = new JComboBox(choices);
combo.setBackground(Color.gray);
combo.setForeground(Color.red);
panel.add(combo);
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,100);
frame.setVisible(true);
combo.addItemListener(new ItemListener () {
@Override
public void itemStateChanged(ItemEvent e)
{
String item = (String)e.getItem();
if (e.getStateChange () == ItemEvent.SELECTED)
{
System.out.println("You chose " +item);
if (item == "Shoes")
{
System.out.println("Enter quantity: ");
int bilang = inp.nextInt();
int total = bilang * 99;
String order = bilang + " " + "Shoes " + " " + total;
System.out.print("" + order);
}
else if (item == "Comb")
{
System.out.println("Enter quantity: ");
int bilang = inp.nextInt();
int total1 = bilang * 99;
String order1 = bilang + " " + "Comb " + " " + total1;
System.out.print("" + order1);
}
else if (item == "Ball")
{
System.out.println("Enter quantity: ");
int bilang = inp.nextInt();
int total2 = bilang * 99;
String order2 = bilang + " " + "Ball " + " " + total2;
System.out.print("" + order2);
}
}
}
});
}}
发布于 2013-03-07 01:11:22
您所做的基本错误是使用==操作符比较两个String对象。
您应该改用以下条件:
if (item .equals("Shoes"))
if (item .equals("Comb"))
if (item .equals("Ball"))作为JOptionPane的简短演示..我在这里放了你的代码的修改版本…我希望它能帮助你理解如何正确处理这种情况……
//combo box, the program hangs I need to close the entire BlueJ. Please take a look what is wrong in my code
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class HulaHoops {
private Scanner inp = new Scanner(System.in);
public static void main(String[]args) {
EventQueue.invokeLater(new Runnable()
{
@Override
public void run()
{
new HulaHoops();
}
});
}
public HulaHoops() {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
String choices[] = {"Shoes","Comb","Ball"};
JComboBox combo = new JComboBox(choices);
combo.setBackground(Color.gray);
combo.setForeground(Color.red);
panel.add(combo);
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,100);
frame.setVisible(true);
combo.addItemListener(new ItemListener () {
@Override
public void itemStateChanged(ItemEvent e)
{
String item = (String)e.getItem();
if (e.getStateChange () == ItemEvent.SELECTED)
{
System.out.println("You chose " +item);
if (item.equals("Comb"))
{
Object val = JOptionPane.showInputDialog("Please input quantity of comb");
System.out.println(val);
Integer bilang= Integer.valueOf((String)val);
int total = bilang * 99;
String order = bilang + " " + "Shoes " + " " + total;
System.out.print("" + order);
}
}
}
});
}
}发布于 2013-03-07 01:09:59
问题是您阻塞了调用Scanner#nextInt的EDT,等待来自ItemListener中的System.in的输入
int bilang = inp.nextInt(); 不要使用Scanner来读取Swing应用程序中的用户输入。这只会阻止EDT,直到控制台窗口中提供数据为止。有许多方法可以读取输入,例如使用JOptionPane#showInputDialog。有关此选项的更多信息,请参阅How to Make Dialogs。
除此之外,在比较String内容时使用.equals而不是==运算符。后者比较Object引用。
https://stackoverflow.com/questions/15253571
复制相似问题