java中如何改变jcombobox选项的背景色? 20
我把jlabel设置为combobox的选择项,我让鼠标停留在某个选项上时,改变该选项的背景色,如何实现?下面是我写的一个渲染器,不管是扩展jlabel还是jpanel,...
我把jlabel设置为combobox的选择项,我让鼠标停留在某个选项上时,改变该选项的背景色,如何实现?
下面是我写的一个渲染器,不管是扩展jlabel还是jpanel,没有改变背景色
class StateLabelRenderer extends JPanel implements ListCellRenderer
{
private JLabel iconlabel ;
private JTextField jTextField ;
private Border selectedBorder = BorderFactory.createLineBorder(Color.green,1);
private Border emptyBorder = BorderFactory.createEmptyBorder();
private Color defaultBkgColor = new Color(200,200,169);;
private Color selectedColor = new Color(248,147,29);;
public StateLabelRenderer()
{
iconlabel = new JLabel() ;
jTextField = new JTextField() ;
this.setLayout(new FlowLayout());
this.add(iconlabel);
this.add(jTextField);
}
@Override
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
// TODO Auto-generated method stub
this.iconlabel.setIcon(((IconItem)(value)).getIcon());
this.jTextField.setText(((IconItem)(value)).getText());
this.setOpaque(false);
if (isSelected == true)
{
this.setBackground(selectedColor);
this.setBorder(selectedBorder);
}
else
{
this.setBackground(defaultBkgColor);
this.setBorder(emptyBorder);
}
return this;
}
public void setText(String string){
this.jTextField.setText(string);
}
public void setIcon(Icon icon){
this.iconlabel.setIcon(icon);
}
} 展开
下面是我写的一个渲染器,不管是扩展jlabel还是jpanel,没有改变背景色
class StateLabelRenderer extends JPanel implements ListCellRenderer
{
private JLabel iconlabel ;
private JTextField jTextField ;
private Border selectedBorder = BorderFactory.createLineBorder(Color.green,1);
private Border emptyBorder = BorderFactory.createEmptyBorder();
private Color defaultBkgColor = new Color(200,200,169);;
private Color selectedColor = new Color(248,147,29);;
public StateLabelRenderer()
{
iconlabel = new JLabel() ;
jTextField = new JTextField() ;
this.setLayout(new FlowLayout());
this.add(iconlabel);
this.add(jTextField);
}
@Override
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
// TODO Auto-generated method stub
this.iconlabel.setIcon(((IconItem)(value)).getIcon());
this.jTextField.setText(((IconItem)(value)).getText());
this.setOpaque(false);
if (isSelected == true)
{
this.setBackground(selectedColor);
this.setBorder(selectedBorder);
}
else
{
this.setBackground(defaultBkgColor);
this.setBorder(emptyBorder);
}
return this;
}
public void setText(String string){
this.jTextField.setText(string);
}
public void setIcon(Icon icon){
this.iconlabel.setIcon(icon);
}
} 展开
1个回答
2014-05-29
展开全部
JPanel或JLabel 覆盖 paint(Graphics) 就可以了
追问
失效了。上面代码就是这个意思
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
微测检测5.10
2023-05-10 广告
2023-05-10 广告
您好!建议咨 深圳市微测检测有限公司,已建立起十余个专业实验室,企业通过微测检测就可以获得一站式的测试与认 证解决方案;(EMC、RF、MFi、BQB、QI、USB、安全、锂电池、快充、汽车电子EMC、汽车手机互 联、语音通话质量),认证遇...
点击进入详情页
本回答由微测检测5.10提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询