2

请看下面的代码

import java.awt.event.*;
import javax.swing.*;
import java.awt.*;

public class JCombo extends JFrame
{
    JComboBox com1;

    public JCombo()
    {


        com1 = new JComboBox();

        com1.addItem("Select");
        com1.addItem("One");
        com1.addItem("two");
        com1.addItem("Three");

        com1.addItemListener(new Com1Action());

        this.setLayout(new FlowLayout());
        this.add(com1);

        this.pack();
        this.validate();
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    private class Com1Action implements ItemListener
    {
        public void itemStateChanged(ItemEvent ae)
        {
            if(ae.getStateChange() == ItemEvent.SELECTED)
            {
                com1.getSelectedItem();
            }
        }
    }

    public static void main(String[]args)
    {
        try
        {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            new JCombo();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

    }
}

在这里,我想在选择项目时为所选项目应用颜色。我怎样才能做到这一点?

前任:

User selects "One" - Now "One" changes to blue
  User selects "Two" - Now "Two" changes to blue. "One" is also blue as well, because we changed the colour at the first place
    User selected "Three" - Now "Three" changes to blue. "One" and "Two" remains blue as well

更新

我用自定义渲染器重新编码。现在它是突出显示选定的项目,但是一旦鼠标移动,颜色就会恢复到原始状态。换句话说,这里唯一发生的事情是更改突出显示颜色,而不是永久地将颜色应用于所选项目

import java.awt.event.*;
import javax.swing.*;
import java.awt.*;

public class JCombo extends JFrame
{
    JComboBox com1;

    public JCombo()
    {


        com1 = new JComboBox();
        com1.setLightWeightPopupEnabled(true);

        com1.addItem("One");
        com1.addItem("two");
        com1.addItem("Three");

        com1.setRenderer(new MyCellRenderer());

        com1.addItemListener(new Com1Action());

        this.setLayout(new FlowLayout());
        this.add(com1);

        this.pack();
        this.validate();
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    private class Com1Action implements ItemListener
    {
        public void itemStateChanged(ItemEvent ae)
        {
            if(ae.getStateChange() == ItemEvent.SELECTED)
            {
                com1.getSelectedItem();

            }
        }
    }

   class MyCellRenderer extends JLabel implements ListCellRenderer<Object> 
   {
     public MyCellRenderer() 
     {
         setOpaque(true);
     }

     public Component getListCellRendererComponent(JList<?> list,
                                                   Object value,
                                                   int index,
                                                   boolean isSelected,
                                                   boolean cellHasFocus) {

         setText(value.toString());

         Color background = Color.white;
         Color foreground = Color.black;

         // check if this cell represents the current DnD drop location
         JList.DropLocation dropLocation = list.getDropLocation();

         if (dropLocation != null
                 && !dropLocation.isInsert()
                 && dropLocation.getIndex() == index) {



         // check if this cell is selected
         } else if (isSelected) {
             background = Color.RED;
             foreground = Color.WHITE;

         // unselected, and not the DnD drop location
         } else {
         };

         setBackground(background);
         setForeground(foreground);

         return this;
     }
 }


    public static void main(String[]args)
    {
        try
        {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            new JCombo();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

    }
}
4

4 回答 4

4

这需要提供自定义渲染器;API 包含一个示例

附录:唯一发生的事情是更改突出显示颜色,而不是永久地将颜色应用于所选项目。

永久更改某些内容意味着有一个地方来存储该信息。我看到两个选择:

  • state向您选择的字段添加一个字段,ComboBoxModel并使用它来调节渲染器中的背景颜色。您可以使用list.getModel().

  • 正如@mKorbel在此处所建议的那样,切换到JListJTable使用ListSelectionModel允许多项选择的 a。

于 2012-12-02T17:31:50.383 回答
2

如果您只想更改所选项目的前景色,请使用以下代码

combobox.getEditor().getEditorComponent().setForeground(Color.GREEN);

如果你想改变所有项目的前景色,包括选中的项目,那么也可以使用上面的代码以及下面的代码。

   combobox.setRenderer(new DefaultListCellRenderer() {
@Override
public void paint(Graphics g) {
    setBackground(Color.WHITE);
    setForeground(Color.GREEN);
    super.paint(g);
}

});

于 2015-05-13T06:46:07.803 回答
0

你试过这个.setBackground(Color c)方法吗?

Java 的好处在于它的文档非常详尽。

在此处查看 JComponent 的文档:JComponent Docs

于 2012-12-02T17:21:25.763 回答
0

主要有两种方式

  • 1)在 UIManager 表单 JCombobox 中更改键值或
  • 2) 覆盖 DefaultListCellRenderer,如果 isSelected

    UIManager.put("ComboBox.background", new ColorUIResource(Color.yellow));
    UIManager.put("ComboBox.selectionBackground", new ColorUIResource(Color.magenta));
    UIManager.put("ComboBox.selectionForeground", new ColorUIResource(Color.blue));
    
于 2013-10-30T06:40:41.533 回答