java下拉菜单
importjava.awt.*;importjavax.swing.*;importjava.awt.event.*;classtestextendsJFrame{JC...
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class test extends JFrame
{
JComboBox jcb;
String[] name={"red","green","yellow"};
Icon i1=new ImageIcon("r.png");
Icon i2=new ImageIcon("g.png");
Icon i3=new ImageIcon("y.png");
Icon[] icons={i1,i2,i3};
JPanel p;
test()
{
Container con=this.getContentPane();
con.setLayout(new BorderLayout());
p=new JPanel();
jcb=new JComboBox(name);
jcb.setMaximumRowCount(2);
jcb.setEditable(true);
jcb.setPreferredSize(new Dimension(200,30));
jcb.setRenderer(new MyCellRenderer(icons));
p.add(jcb);
con.add(p,"North");
setExtendedState(JFrame.MAXIMIZED_BOTH);
setVisible(true);
validate();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main (String[] args)
{
new test();
}
}
class MyCellRenderer extends JLabel implements ListCellRenderer
{
Icon[] icons;
MyCellRenderer(Icon[] icons)
{
this.icons=icons;
setOpaque(true);
}
public Component getListCellRendererComponent(JList list,Object value,int index,boolean isSelected,boolean cellHasFocus)
{
if((index!=-1)&&(index<icons.length))
{
setIcon(icons[index]);
}
if(isSelected)
{
setBackground(list.getBackground());
setForeground(list.getForeground());
}
else
{
setBackground(list.getBackground());
setForeground(list.getForeground());
}
return this;
}
}
问题代码如上。
问题:
程序能运行,但是为什么下拉菜单不显示图片。(图片的位置已放对)
该代码!
高手解决!
我晕。我都说了路径没问题了。。 展开
import javax.swing.*;
import java.awt.event.*;
class test extends JFrame
{
JComboBox jcb;
String[] name={"red","green","yellow"};
Icon i1=new ImageIcon("r.png");
Icon i2=new ImageIcon("g.png");
Icon i3=new ImageIcon("y.png");
Icon[] icons={i1,i2,i3};
JPanel p;
test()
{
Container con=this.getContentPane();
con.setLayout(new BorderLayout());
p=new JPanel();
jcb=new JComboBox(name);
jcb.setMaximumRowCount(2);
jcb.setEditable(true);
jcb.setPreferredSize(new Dimension(200,30));
jcb.setRenderer(new MyCellRenderer(icons));
p.add(jcb);
con.add(p,"North");
setExtendedState(JFrame.MAXIMIZED_BOTH);
setVisible(true);
validate();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main (String[] args)
{
new test();
}
}
class MyCellRenderer extends JLabel implements ListCellRenderer
{
Icon[] icons;
MyCellRenderer(Icon[] icons)
{
this.icons=icons;
setOpaque(true);
}
public Component getListCellRendererComponent(JList list,Object value,int index,boolean isSelected,boolean cellHasFocus)
{
if((index!=-1)&&(index<icons.length))
{
setIcon(icons[index]);
}
if(isSelected)
{
setBackground(list.getBackground());
setForeground(list.getForeground());
}
else
{
setBackground(list.getBackground());
setForeground(list.getForeground());
}
return this;
}
}
问题代码如上。
问题:
程序能运行,但是为什么下拉菜单不显示图片。(图片的位置已放对)
该代码!
高手解决!
我晕。我都说了路径没问题了。。 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询