简体   繁体   中英

Java : How do I set the background color of a frame to the corresponding selected color name in a combo box

Please, I'm trying to set the background color of a frame to the selected color name in a combo box. Although I used the repaint() method, it still doesn't work.

Is it that the JFrame class can't be colored? Or, is there another way of doing this? What follows is what I tried:

Please can anyone help with some snippets?

I suggest creating your own JPanel and setting it as the content pane. (Note where container is declared and the 2nd line of the constructor). This is generally considered "cleaner".

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class GUI1410 extends JFrame implements ActionListener
{

    private JComboBox comboBox;
    private JCheckBox background, foreground;
    private JButton okButton, cancelButton;
    private String[] colors = { "RED", "GREEN", "BLUE" };
    private Color color[] = { Color.RED, Color.GREEN, Color.BLUE };

    private JPanel checkBoxPanel, buttonPanel;
    Container container = new JPanel();

    public GUI1410()
    {

        super("ColorSelect");
        setContentPane(container);
        this.getContentPane().setBackground(color[0]);

        comboBox = new JComboBox(colors);
        comboBox.setMaximumRowCount(2);
        comboBox.addItemListener(new ItemListener() {

            @Override
            public void itemStateChanged(ItemEvent event)
            {

                if (event.getStateChange() == ItemEvent.SELECTED)
                {
                    container.setBackground(color[comboBox.getSelectedIndex()]);
                    container.repaint();
                }

            }

        });

        background = new JCheckBox("Background");
        foreground = new JCheckBox("Foreground");

        okButton = new JButton("Ok");
        okButton.addActionListener(this);
        cancelButton = new JButton("Cancel");
        cancelButton.addActionListener(this);

        checkBoxPanel = new JPanel();
        checkBoxPanel.add(background);
        checkBoxPanel.add(foreground);

        buttonPanel = new JPanel();
        buttonPanel.add(okButton);
        buttonPanel.add(cancelButton);

        container.add(comboBox, BorderLayout.NORTH);
        container.add(checkBoxPanel, BorderLayout.CENTER);
        container.add(buttonPanel, BorderLayout.SOUTH);

        setVisible(true);
        setBounds(400, 200, 300, 120);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

    @Override
    public void actionPerformed(ActionEvent event)
    {

        if (event.getSource() == okButton)
        {
            container.setBackground(color[comboBox.getSelectedIndex()]);
        }
        else if (event.getSource() == cancelButton)
        {
            System.exit(0);
        }

    }

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM