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) 就可以了
追问
失效了。上面代码就是这个意思
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询