@ Override
public Component getListCellRendererComponent (JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
/ / check if this cell represents the current DnD drop location
JList.DropLocation dropLocation = myList.getDropLocation ();
if (dropLocation! = null
&& ! dropLocation.isInsert ()
&& dropLocation.getIndex () == index) {
/ / check if this cell is selected
} else if (isSelected) {
} else {
};
return this;
}
I want to be a drop-down selection box , which the option is in the form of pictures how to write ? Write a Icon [] array of objects stored pictures , how to appear in the drop-down box inside ? How to write the above code ?
------ Solution ---------------------------------------- ----
http://www.java2s.com/Tutorial/ Java/0240__Swing/Comboboxcellrenderer.htm
------ Solution ----------------------------- ---------------
achieve ListCellBenderer interface to draw a list of unit
http://www.blogjava.net/zeyuphoenix/archive/2010 / 04/12/318014.html
------ Solution ---------------------------- ----------------
JCombobox default drop-down display and display items are text , in order to display additional content such as pictures or something more complex , you need to set up a new Renderer, JCombobox of Renderer need to implement the interface ListCellRenderer .
------ For reference only -------------------------------- -------
you made reference to the class content, written in the case. . . Or not . . . How to change ?
public class mousergister extends JPanel {
JPanel jp;
JComboBox setImg;
ComboBoxRenderer renderer;
public static void main(String[] args) {
new mousergister();
}
public mousergister() {
BaseFrame bf=new BaseFrame("用户注册");
bf.add(this);
this.setLayout(null);
renderer = new ComboBoxRenderer();
renderer.setPreferredSize(new Dimension(100, 50));
Object elements[][] = {
{ new MyIcon("images/1.jpg",this)},
{ new MyIcon("images/2.gif",this) },
{ new MyIcon("images/3.jpg",this)},
};
setImg = new JComboBox(elements);
setImg.setRenderer(renderer);
setImg.setBounds(120, 25, 75, 60);
add(setImg);
bf.setLocation(500, 100);// 设置窗口在屏幕的位置
bf.setSize(320, 420);
bf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
bf.setResizable(false);// 设置游戏窗口大小不可改变
bf.setVisible(true);
}
private class ComboBoxRenderer extends JLabel implements ListCellRenderer {
// 这样要是实现接口的方法:
/*
*
* This method finds the image and text corresponding to the selected
*
* value and returns the label, set up to display the text and image.
*/
@Override
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
ImageIcon image=null;
if (value instanceof Object[]) {
Object[] values = (Object[]) value;
image = (ImageIcon)values[0];
}
if (image != null) {
this.setIcon(image);
}
return this;
}
}
private class MyIcon extends ImageIcon{
private Image m;
private String str;
private mousergister jp;
public MyIcon(String str,mousergister jp) {
Toolkit tool;
tool = jp.renderer.getToolkit();
m = tool.getImage(str);
}
public void paintIcon(Component lab, Graphics g){
lab=jp.renderer;
g.drawImage(m, 20, 20, lab);
}
}
}
------ For reference only ----------------------------------- ----
problems have been solved , thank you everyone to help .
没有评论:
发表评论