2013-04-14 82 views
2

所以我試圖做一個漂亮的窗口顯示從組合框中選擇的顏色。但我得到了窗戶和組合框,出於某種原因我的顏色不顯示。誰能幫我?如何顯示在我的組合框中選擇的顏色

另外從我可以告訴,我的組合框是在我的窗口的頂部。我想讓它顯示在下面的顏色。

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

class Colors extends JFrame implements ItemListener 
{ 
    Choice chooseColor = new Choice(); 
    Label lblQts = new Label("Choose color you like : "); 

    public Colors(String title) 
    { 
     super(title); 
     setLayout(new FlowLayout()); 
     chooseColor.addItem("red"); 
     chooseColor.addItem("green"); 
     chooseColor.addItem("blue"); 

     add(lblQts); 
     add(chooseColor); 

     chooseColor.addItemListener(this); 
    } 

    public void itemStateChanged(ItemEvent e) 
    { 
     String c; 
     Color color; 
     c = chooseColor.getSelectedItem(); 
     color=Color.getColor(c); 

     setBackground(color); 
    } 


} 


public static void main(String[] args) 
{ 
    Colors objColor = new Colors("Color Chooser"); 
    objColor.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    objColor.setSize(400,400); 
    objColor.setVisible(true); 
} 
+1

正如我在你上一篇文章中所建議的,你需要添加一個'Color'對象到組合框,你需要創建一個自定義的渲染器。你不能只添加一個字符串,並期望它神奇地代表一種顏色。 – camickr

+1

爲什麼使用AWT Choice類而不是Swing JComboBox? –

+0

不要混合輕重量組件,這隻會與z排序混淆,並且永遠不會結束 – MadProgrammer

回答

3

讓我們承擔,你真的應該使用Swing的API,而不是AWT API片刻(並視爲你只是剛開始學習,我認爲這是一個體面的假設)。

您可以執行以下操作...

基本上。我有JComboBox定製ListCellRendererActionListener

ListCellRenderer以我想要的方式呈現項目以及ActionListener聽衆更改組合框。

當選擇新項目時,將根據所選項目更改組合框的背景。

這裏展示的概念是理解揮杆的關鍵(和Java一般)

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Component; 
import java.awt.EventQueue; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.ComboBoxEditor; 
import javax.swing.DefaultListCellRenderer; 
import javax.swing.JComboBox; 
import javax.swing.JFrame; 
import javax.swing.JList; 
import javax.swing.JPanel; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class TestComboBox08 { 

    public static void main(String[] args) { 
     new TestComboBox08(); 
    } 

    public TestComboBox08() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException ex) { 
       } catch (InstantiationException ex) { 
       } catch (IllegalAccessException ex) { 
       } catch (UnsupportedLookAndFeelException ex) { 
       } 

       JFrame frame = new JFrame("Test"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setLayout(new BorderLayout()); 
       frame.add(new TestPane()); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 

     }); 
    } 

    public class TestPane extends JPanel { 

     public TestPane() { 
      final JComboBox cb = new JComboBox(); 
      cb.addItem(new SelectableColor("Red", Color.RED)); 
      cb.addItem(new SelectableColor("Green", Color.GREEN)); 
      cb.addItem(new SelectableColor("Blue", Color.BLUE)); 
      cb.setRenderer(new ColorCellRenderer()); 
      add(cb); 
      cb.addActionListener(new ActionListener() { 
       @Override 
       public void actionPerformed(ActionEvent e) { 
        Object value = cb.getSelectedItem(); 
        if (value instanceof SelectableColor) { 
         cb.setBackground(((SelectableColor)value).getColor()); 
        } else { 
         cb.setBackground(null); 
        } 
       } 
      }); 
      cb.setSelectedItem(null); 
     } 

    } 

    public class SelectableColor { 
     private String name; 
     private Color color; 

     public SelectableColor(String name, Color color) { 
      this.name = name; 
      this.color = color; 
     } 

     public String getName() { 
      return name; 
     } 

     public Color getColor() { 
      return color; 
     } 

    } 

    public class ColorCellRenderer extends DefaultListCellRenderer { 

     @Override 
     public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) { 
      super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); 
      if (value instanceof SelectableColor) { 
       SelectableColor sc = (SelectableColor) value; 
       if (!isSelected) { 
        setBackground(sc.getColor()); 
        setOpaque(true); 
       } 
       setText(sc.getName()); 
      } 
      return this; 
     } 

    } 

} 

你真的應該熟悉Creating A UI with Swing。如果這是你的頭,從Trails開始涵蓋的基本知識

+0

哇,我很清楚這件作品在我的頭上。我想我需要閱讀爲我提供的一些資源。感謝您在這項任務上的所有努力和投入。 –

1

您需要設置contentPane的背景,而不是JFrame背景。 然後,您不能使用Color.getColor來檢索此案例的顏色對象。見this

這是工作代碼:

import java.awt.Choice; 
import java.awt.Color; 
import java.awt.FlowLayout; 
import java.awt.Label; 
import java.awt.event.ItemEvent; 
import java.awt.event.ItemListener; 
import java.lang.reflect.Field; 

import javax.swing.JFrame; 

public class Colors extends JFrame implements ItemListener 

{ 
    Choice chooseColor = new Choice(); 
    Label lblQts = new Label("Choose color you like : "); 

    public Colors (String title) { 
     super(title); 
     setLayout(new FlowLayout()); 
     chooseColor.addItem("red"); 
     chooseColor.addItem("green"); 
     chooseColor.addItem("blue"); 

     add(lblQts); 
     add(chooseColor); 

     chooseColor.addItemListener(this); 
    } 

    public void itemStateChanged(ItemEvent e) { 
     String c; 
     Color color; 
     c = chooseColor.getSelectedItem();    
     try { 
      Field field = Class.forName("java.awt.Color").getField(c); 
      color = (Color)field.get(null); 
     } catch (Exception ex) { 
      color = null; // Not defined 
     }  
     getContentPane().setBackground(color); 

    } 

    public static void main(String[] args) { 

     Colors objColor = new Colors ("Color Chooser"); 
     objColor.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     objColor.setSize(400, 400); 
     objColor.setVisible(true); 

    } 
} 
+0

爲什麼要反思?爲什麼不簡單地用某種'Map'將'String'值映射到'Color'? – MadProgrammer

相關問題