2013-11-27 128 views
3

我是非常基礎的Java學生。我們完成了一項任務,根據使用幾個if語句選擇的單選按鈕來改變背景顏色。這工作得很好。我決定將選擇過程更改爲組合框並使用開關盒。在我看來,這個過程在switch case方法中if語句失敗。我試圖更好地理解事情的工作方式。代碼:Java開關盒未按預期運行

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
import java.util.*; 

class Lab17_4combo extends JFrame implements ActionListener 
{ 
    Container container; 
    JComboBox colors; 

    public Lab17_4combo() 
    { 
     super("ComboBox "); 
     container = this.getContentPane(); 
     container.setLayout(new FlowLayout()); 
     setSize(300,200); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     String[] selectColor = {"Red", "Yellow", "Blue", "Green", "Magenta"}; 
     JComboBox colors = new JComboBox(selectColor); 
     colors.setSelectedIndex(-1); 
     colors.addActionListener(this); 

     container.add(colors); 
     setVisible(true); 
    } 
    public void actionPerformed(ActionEvent e) 
    { 
     int chgColor; 

     if(e.getSource() == colors) 
     { 
      chgColor = colors.getSelectedIndex(); 

      switch(chgColor) 
      { 
       case 0: 
        container.setBackground(Color.red); 
       case 1: 
        container.setBackground(Color.yellow); 
       case 2: 
        container.setBackground(Color.blue); 
       case 3: 
        container.setBackground(Color.green); 
       case 4: 
        container.setBackground(Color.magenta); 
      } 
     }else 
      { 
       container.setBackground(Color.magenta); 
      } 

    } 
    public static void main(String[] args) 
    { 
     Lab17_4combo s = new Lab17_4combo(); 
    } 
} 

我把其他的東西,以檢查它是否失敗的if。我假設這是問題所在,但我不知道如何解決問題。任何幫助將不勝感激。原來的任務已經完成,這是我自己的實驗。我不是要求任何人爲我做作業。乾杯

編輯 - 我已經對代碼建議的更改(感謝所有的建議)。無論我從組合框中進行選擇,容器的背景顏色都不會改變。我假設代碼中有其他地方存在錯誤,但我無法找到它們。我的期望是容器的背景顏色會根據我從組合框中所做的選擇而改變。這沒有發生。

修改後的代碼:

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
import java.util.*; 

class Lab17_4combo extends JFrame implements ActionListener 
{ 
    Container container; 
    JComboBox colors; 

    public Lab17_4combo() 
    { 
     super("ComboBox "); 
     container = this.getContentPane(); 
     container.setLayout(new FlowLayout()); 
     setSize(300,200); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     String[] selectColor = {"Red", "Yellow", "Blue", "Green", "Magenta"}; 
     JComboBox colors = new JComboBox(selectColor); 
     colors.setSelectedIndex(-1); 
     colors.addActionListener(this); 

     container.add(colors); 
     setVisible(true); 
    } 
    public void actionPerformed(ActionEvent e) 
    { 
     int chgColor; 

     if(e.getSource() == colors) 
     { 
      chgColor = colors.getSelectedIndex(); 

      switch(chgColor) 
      { 
       case 0: 
        container.setBackground(Color.red); 
        break; 
       case 1: 
        container.setBackground(Color.yellow); 
        break; 
       case 2: 
        container.setBackground(Color.blue); 
        break; 
       case 3: 
        container.setBackground(Color.green); 
        break; 
       case 4: 
        container.setBackground(Color.magenta); 
        break; 
      } 
     } 
    } 
    public static void main(String[] args) 
    { 
     Lab17_4combo s = new Lab17_4combo(); 
    } 
} 

用我有限的Java方面的知識,我無法看到的錯誤(或多個)的可能。任何幫助將是appreciated.Cheers

+1

你忘了'之開關語句中的每個'case'之後添加'break'的意見。 – Prateek

+0

@Spud:退房http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html –

+0

謝謝大家。我已在每個案例陳述後添加了中斷,但背景顏色仍未更改。代碼中的其他位置有錯誤嗎? – Spud

回答

1

正如我在評論中提及了你忘了在每個案件的增加break

  switch(chgColor) 
      { 
       case 0: 
        container.setBackground(Color.red); 
        break; 
       case 1: 
        container.setBackground(Color.yellow); 
        break; 
       case 2: 
        container.setBackground(Color.blue); 
        break; 
       case 3: 
        container.setBackground(Color.green); 
        break; 
       case 4: 
        container.setBackground(Color.magenta); 
        break; 
       default: 
       //What if none of above condition is satisfied. 
      } 

編輯: - 檢查代碼

class Lab17_4combo extends JFrame implements ActionListener 
    { 
     Container container; 
     JComboBox colors;// you declared colors. 

     public Lab17_4combo() 
     { 
      super("ComboBox "); 
      container = this.getContentPane(); 
      container.setLayout(new FlowLayout()); 
      setSize(300,200); 
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

      String[] selectColor = {"Red", "Yellow", "Blue", "Green", "Magenta"}; 

      //JComboBox colors = new JComboBox(selectColor);// You declared colors again. Change  this line to 

      colors = new JComboBox(selectColor); 
      colors.setSelectedIndex(-1); 
      colors.addActionListener(this); 

      container.add(colors); 
      setVisible(true); 
     } 
+0

不錯,夥計!所以我經常無法看到所有樹木的森林。只要您指出,它就非常有意義。感謝您的支持。這是一個我會記得的教訓 – Spud

3

你忘了每個case

break聲明試試這個:

switch(chgColor) 
      { 
       case 0: 
        container.setBackground(Color.red); 
        break; 
       case 1: 
        container.setBackground(Color.yellow); 
        break; 
       case 2: 
        container.setBackground(Color.blue); 
        break; 
       case 3: 
        container.setBackground(Color.green); 
        break; 
       case 4: 
        container.setBackground(Color.magenta); 
        break; 
       default: 
       //You may add a default case here. 
      } 

編輯: -

我覺得你的條件

if(e.getSource() == colors) 

永遠不會是真的,這就是爲什麼你得到這個問題。您可以嘗試比較像這樣:

if(e.getSource().equals(colors)) 

始終使用.equals方法,當你比較的對象。

+0

謝謝你的迴應。我已經採納了你的建議,但仍然沒有改變背景顏色。我刪除了else語句,但對於組合框中的每個選擇,顏色保持不變。我在代碼中的其他地方犯了什麼錯誤? – Spud

+0

@Rahul也行不通。由於某種原因,他的'顏色'是'null' – Prateek

+0

@Prateek: - 嗯..是的,你是對的! –

0

你需要確保並添加一個休息;在每一行後,如下所示:

 switch(chgColor) 
     { 
      case 0: 
       container.setBackground(Color.red); 
       break; 
      case 1: 
       container.setBackground(Color.yellow); 
       break; 
      case 2: 
       container.setBackground(Color.blue); 
       break; 
      case 3: 
       container.setBackground(Color.green); 
       break; 
      case 4: 
       container.setBackground(Color.magenta); 
       break; 
     } 
    }else 
     { 
      container.setBackground(Color.magenta); 
     } 
1

您應該在每種情況之後使用break

 case 0: 
      container.setBackground(Color.red); 
      break; 
     case 1: 
      container.setBackground(Color.yellow); 
      break; 
      .... 
0

看起來你總是給出的if條件和switch語句洋紅色結束。

爲每個case聲明使用中斷。

switch聲明中,對於每個case,您沒有break;。因此,即使switch找到一個有效的匹配case,它執行的話,在你的代碼(因爲你不破控制出來的話)的,它最終傳遞控制的情況下匹配的情況下,始終結束在最後的情況下。