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

2 回答 2

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

    }

}

您确实应该熟悉使用 Swing 创建 UI。如果这超出了您的想象,请从涵盖基础知识的Trails开始

于 2013-04-14T03:54:58.477 回答
1

您需要设置 contentPane 的背景,而不是JFrame背景。然后,您不能Color.getColor用于在这种情况下检索您的颜色对象。看到这个

这是工作代码:

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

    }
}
于 2013-04-14T03:22:57.650 回答