2013-03-06 31 views
0

爲什麼我的組合框不執行我在ItemListener中聲明的事物?當我點擊組合框內的一個項目時,程序掛起,我需要關閉整個BlueJ。請大家看看什麼是錯在我的代碼ItemListener不起作用

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); 
        } 
      } 

     } 
    }); 
} 

}

+0

並總是不使用==比較字符串,而是使用equals()或equalsIgnoreCase()方法。 – 2013-03-06 17:08:51

回答

1

,你正在做的是使用==操作比較兩個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); 
        } 


      } 
    } 
    }); 
    } 
} 
+0

我已經嘗試過一個,但它仍然掛斷後,我在組合框中 – 2013-03-06 17:16:47

+0

@AngelCasiMontoya選擇一個項目:它是不是好的選擇使用掃描儀來阻止EDT ..但作爲一項決議,解決烏爾GUI掛的問題是我的建議你是這樣的:在你選擇組合框中的一些項目後,點擊你正在運行java程序的控制檯,然後輸入值,然後按Enter ...它在我身邊執行..現在來處理這個標準方法要求,您可以使用JOptionPane.inputDialog(...)按照Reimeus ..或任何其他圖形用戶界面(如TextField的主框架本身)的建議..從用戶處獲取輸入。組合框狀態改變時。 – 2013-03-06 17:35:18

+0

謝謝現在我明白了爲什麼我不應該在Swing應用程序中插入'Scanner' :) – 2013-03-07 10:09:19

3

的問題是,你擋住了EDTScanner#nextIntItemListener等待輸入從System.in

int bilang = inp.nextInt(); 

唐使用Scanner來讀取Swing應用程序中的用戶輸入。這隻會阻止EDT,直到數據在控制檯窗口中提供。有許多方法可以讀取輸入,例如使用JOptionPane#showInputDialog。有關此選項的更多信息,請參見How to Make Dialogs


從這個

除此之外,比較String內容時,而不是==運營商使用.equals。後者比較Object參考。

+0

那麼掃描儀的替代代碼是什麼?我應該如何輸入數量? – 2013-03-06 17:15:51

+0

這取決於你想如何輸入數據。我給了你一個'JOptionPane'的鏈接。雖然你可能希望有一個固定的'JTextField'作爲輸入,而不是彈出對話框。 – Reimeus 2013-03-06 17:19:02