如何显示在我的组合框中选择的颜色

时间:2013-04-14 03:11:28

标签: java

所以我试图制作一个漂亮的窗口,显示从组合框中选择的颜色。但我得到了窗口和组合框,由于某种原因我的颜色没有显示。任何人都可以帮助我吗?

另据我所知,我的组合框位于窗口的顶部。我想在颜色下面显示它。

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

2 个答案:

答案 0 :(得分:3)

让我们假设你真的应该使用Swing API而不是AWT API(并且看到你只是在学习我认为这是一个不错的假设)。

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

基本上。我JComboBox有自定义ListCellRendererActionListener

ListCellRenderer以我想要的方式呈现项目,ActionListener侦听器更改组合框。

选择新项目后,它将根据所选项目更改组合框的背景。

此处演示的概念对于理解Swing(以及一般的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开始

答案 1 :(得分: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);

    }
}