4

我正在 JFrame 表单中编写一个可编辑的组合框,但我想更改背景颜色。

程序是如何工作的:如果我点击按钮“按下”,那么他的背景组合框需要变成黑色。

我试过了:

1.

cbo.setBackground(Color.BLACK);

但它什么也没做

2

cbo.getEditor().getEditorComponent().setBackground(Color.BLACK);

((JTextField) cbo.getEditor().getEditorComponent()).setOpaque(true);

做这个:

图片

代码示例:

public class NewJFrame extends javax.swing.JFrame {

    private JComboBox cboCategorie;

    public NewJFrame() {
        initComponents();

        cboCategorie = new JComboBox();
        cboCategorie.setBounds(10, 10, 250, 26);
        cboCategorie.setVisible(true);
        cboCategorie.setEditable(true);
        this.add(cboCategorie);

    }

private void pressActionPerformed(java.awt.event.ActionEvent evt) {
        cboCategorie.getEditor().getEditorComponent().setBackground(Color.BLACK);
        ((JTextField) cboCategorie.getEditor().getEditorComponent()).setOpaque(true);
}

我正在使用 Java JDK7

有什么建议吗?

4

3 回答 3

6

看我的代码示例

在此处输入图像描述

import java.awt.*;
import java.util.Vector;
import javax.swing.*;
import javax.swing.UIManager;
import javax.swing.plaf.ColorUIResource;
import javax.swing.plaf.metal.MetalComboBoxButton;

public class MyComboBox {

    private Vector<String> listSomeString = new Vector<String>();
    private JComboBox someComboBox = new JComboBox(listSomeString);
    private JComboBox editableComboBox = new JComboBox(listSomeString);
    private JComboBox non_EditableComboBox = new JComboBox(listSomeString);
    private JFrame frame;

    public MyComboBox() {
        listSomeString.add("-");
        listSomeString.add("Snowboarding");
        listSomeString.add("Rowing");
        listSomeString.add("Knitting");
        listSomeString.add("Speed reading");
//
        someComboBox.setPrototypeDisplayValue("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
        someComboBox.setFont(new Font("Serif", Font.BOLD, 16));
        someComboBox.setEditable(true);
        someComboBox.getEditor().getEditorComponent().setBackground(Color.YELLOW);
        ((JTextField) someComboBox.getEditor().getEditorComponent()).setBackground(Color.YELLOW);
//
        editableComboBox.setPrototypeDisplayValue("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
        editableComboBox.setFont(new Font("Serif", Font.BOLD, 16));
        editableComboBox.setEditable(true);
        JTextField text = ((JTextField) editableComboBox.getEditor().getEditorComponent());
        text.setBackground(Color.YELLOW);
        JComboBox coloredArrowsCombo = editableComboBox;
        Component[] comp = coloredArrowsCombo.getComponents();
        for (int i = 0; i < comp.length; i++) {// hack valid only for Metal L&F
            if (comp[i] instanceof MetalComboBoxButton) {
                MetalComboBoxButton coloredArrowsButton = (MetalComboBoxButton) comp[i];
                coloredArrowsButton.setBackground(null);
                break;
            }
        }
//
        non_EditableComboBox.setPrototypeDisplayValue("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
        non_EditableComboBox.setFont(new Font("Serif", Font.BOLD, 16));
//
        frame = new JFrame();
        frame.setLayout(new GridLayout(0, 1, 10, 10));
        frame.add(someComboBox);
        frame.add(editableComboBox);
        frame.add(non_EditableComboBox);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocation(100, 100);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        UIManager.put("ComboBox.background", new ColorUIResource(Color.yellow));
        UIManager.put("JTextField.background", new ColorUIResource(Color.yellow));
        UIManager.put("ComboBox.selectionBackground", new ColorUIResource(Color.magenta));
        UIManager.put("ComboBox.selectionForeground", new ColorUIResource(Color.blue));
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                MyComboBox aCTF = new MyComboBox();
            }
        });
    }
}
于 2012-04-21T10:58:33.643 回答
1

结果很容易。创建渲染器并设置背景颜色。然后覆盖渲染器的 setOpaque 方法以始终将不透明度设置为 true。

class PDFChooser extends JComboBox<String>  {
    PDFChooser() {
        setRenderer(new Renderer());
    }
    class Renderer extends DefaultListCellRenderer {
        @Override 
        public void setOpaque(boolean makeBackGroundVisible) {
            super.setOpaque(true);     // THIS DOES THE TRICK
        }
        @Override
        public Component getListCellRendererComponent(JList<?> list, 
                    Object value, int index, 
                    boolean isSelected, boolean cellHasFocus) {
            setText((String)value);
            setBackground(Color.cyan);
            return this;
        }
    }
}

如果您希望弹出菜单使用 LAF 背景,您可以添加一个 PopupMenuListener 来记录弹出菜单何时弹出。那时,setOpaque 方法会将不透明度设置为 false。

于 2021-05-28T00:13:11.450 回答
0

它对我来说可以更改 JComboBox 中选定项目的颜色。

    JComboBox cmb = new JComboBox();
    cmb.setEditable(true);
    cmb.setEditor(new WComboBoxEditor(getContentPane().getBackground()));

    // To change the arrow button's background        
    cmb.setUI(new BasicComboBoxUI(){
        protected JButton createArrowButton()
        {
            BasicArrowButton arrowButton = new BasicArrowButton(BasicArrowButton.SOUTH, null, null, Color.GRAY, null);
            return arrowButton;
        }
    });
    cmb.setModel(new DefaultComboBoxModel(new String[] { "a", "b", "c" }));


import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionListener;
import javax.swing.ComboBoxEditor;
import javax.swing.JTextField;

public class WComboBoxEditor implements ComboBoxEditor
{
    JTextField tf;

    public WComboBoxEditor(Color background)
    {
        tf = new JTextField();
        tf.setBackground(background);
        tf.setBorder(null);
    }

    public Component getEditorComponent()
    {
        return tf;
    }

    public void setItem(Object anObject)
    {
        if (anObject != null)
        {
            tf.setText(anObject.toString());
        }
    }

    public Object getItem()
    {
        return tf.getText();
    }

    public void selectAll()
    {
        tf.selectAll();
    }

    public void addActionListener(ActionListener l)
    {
        tf.addActionListener(l);
    }

    public void removeActionListener(ActionListener l)
    {
        tf.removeActionListener(l);
    }

}


如果您想更改 JCombobox 中项目的颜色(除了选定的项目),请自定义 ListCellRenderer。

于 2017-09-28T05:51:35.673 回答