Java 使用组合框更改面板的背景色

Java 使用组合框更改面板的背景色,java,swing,jcombobox,Java,Swing,Jcombobox,我正在尝试使用3组合框来更改面板的背景色,红色、绿色和蓝色在0-255之间可调。框架和组合框确实会显示,但是当我更改不同的颜色值时,actionlistener不起作用。我不知道是什么问题。请帮忙。谢谢大家! public class ComboPanel extends JPanel { private JLabel Label1; private JLabel Label2; private JLabel Label3; private JComboBox red; private JCo

我正在尝试使用3组合框来更改面板的背景色,红色、绿色和蓝色在0-255之间可调。框架和组合框确实会显示,但是当我更改不同的颜色值时,actionlistener不起作用。我不知道是什么问题。请帮忙。谢谢大家!

public class ComboPanel extends JPanel {

private JLabel Label1;
private JLabel Label2;
private JLabel Label3;
private JComboBox red;
private JComboBox green;
private JComboBox blue;
private ActionListener listener;

public ComboPanel()
    {
        colorComboBox();
        listener = new ChoiceListener();
    }

    class ChoiceListener implements ActionListener
    {
        public void actionPerformed(ActionEvent event)
        {
            setColor();
        }
    }

    public JComboBox redBox()
    {
        red = new JComboBox();
        int max_red = 255;
        for (int i = 0; i <= max_red; i++)
        {
            red.addItem(i);
        }
        red.setEditable(false);
        red.addActionListener(listener);
        return red;
    }

    public JComboBox greenBox()
    {
        green = new JComboBox();
        int max_green = 255;
        for (int i = 0; i <= max_green; i++)
        {
            green.addItem(i);
        }
        green.setEditable(false);
        green.addActionListener(listener);
        return green;
    }

    public JComboBox blueBox()
    {
        blue = new JComboBox();
        int max_blue = 255;
        for (int i = 0; i <= max_blue; i++)
        {
            blue.addItem(i);
        }
        blue.setEditable(false);
        blue.addActionListener(listener);
        return blue;
    }

    public void colorComboBox()
    {
        setLayout(new GridLayout(3, 1));
        Label1 = new JLabel("Red");
        Label2 = new JLabel("Green");
        Label3 = new JLabel("Blue");

        add(Label1);
        add(redBox());
        add(Label2);
        add(greenBox());
        add(Label3);
        add(blueBox());
    }

    public void setColor()
    {
        int red_number = (int) red.getSelectedItem();
        int green_number = (int) green.getSelectedItem();
        int blue_number = (int) blue.getSelectedItem();
        setBackground(new Color(red_number, green_number, blue_number));
        repaint();
    }
}
公共类ComboPanel扩展了JPanel{
私人JLabel标签1;
私人JLabel标签2;
私人JLabel标签3;
私人JComboBox红色;
私人JComboxGreen;
私人JComboBox蓝色;
私人行动听者;
公共事务委员会()
{
colorComboBox();
listener=新的ChoiceListener();
}
类ChoiceListener实现ActionListener
{
已执行的公共无效操作(操作事件)
{
setColor();
}
}
公共JComboBox redBox()
{
红色=新JComboBox();
int max_red=255;

对于(int i=0;i当您调用
redBox、
greenBox
blueBox
时,
侦听器
为空`

public ComboPanel()
{
    colorComboBox();
    listener = new ChoiceListener();
}
相反,首先初始化
侦听器

public ComboPanel()
{
    listener = new ChoiceListener();
    colorComboBox();
}

调用
redBox、
greenBox
blueBox
时,
侦听器
为空`

public ComboPanel()
{
    colorComboBox();
    listener = new ChoiceListener();
}
相反,首先初始化
侦听器

public ComboPanel()
{
    listener = new ChoiceListener();
    colorComboBox();
}

调用
redBox、
greenBox
blueBox
时,
侦听器
为空`

public ComboPanel()
{
    colorComboBox();
    listener = new ChoiceListener();
}
相反,首先初始化
侦听器

public ComboPanel()
{
    listener = new ChoiceListener();
    colorComboBox();
}

调用
redBox、
greenBox
blueBox
时,
侦听器
为空`

public ComboPanel()
{
    colorComboBox();
    listener = new ChoiceListener();
}
相反,首先初始化
侦听器

public ComboPanel()
{
    listener = new ChoiceListener();
    colorComboBox();
}

但是,请您解释一下为什么我需要在构造函数中添加ActionListener?我的原始代码在ComboBox部分中添加ActionListener,但它不起作用。您不需要(抱歉,我更新了代码),您需要在尝试使用它之前初始化
ActionListener
)谢谢!我真的很感谢你的帮助!但是,你能解释一下为什么我需要在构造函数中添加ActionListener吗?我的原始代码在ComboBox部分添加了ActionListener,但它不起作用。你没有(很抱歉,我更新了代码),您需要先初始化
ActionListener
,然后再尝试使用它;)谢谢!我非常感谢您的帮助!但是,请您解释一下为什么我需要在构造函数中添加ActionListener?我的原始代码在组合框部分添加ActionListener,但它不起作用。您没有(抱歉,我更新了代码),您需要先初始化
ActionListener
,然后再尝试使用它;)谢谢!我非常感谢您的帮助!但是,请您解释一下为什么我需要在构造函数中添加ActionListener?我的原始代码在组合框部分添加ActionListener,但它不起作用。您没有(抱歉,我更新了代码),您需要在尝试使用之前初始化
ActionListener
)谢谢!我非常感谢您的帮助!