import java.awt.*;import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.border.*;class BookEntry{
private final String title;
private final String imagepath;
private ImageIcon Images;
public BookEntry(String title,String imagepath){
this.title=title;
this.imagepath=imagepath;
}
public String getTitle(){return title;}
public ImageIcon getImage(){
if(Images==null)
{
Images=new ImageIcon(imagepath);
}
return Images;
}
public String toString(){return title;}
} class ComboBoxEditorExample implements ComboBoxEditor{
Map map;
ImagePanel panels;
ImageIcon questionIcon;
public ComboBoxEditorExample(Map m,BookEntry defaultChoice){
map=m;
panels=new ImagePanel(defaultChoice);
questionIcon=new ImageIcon("question.gif");
}
public void setItem(Object anObject){
if(anObject!=null)
{
panels.setText(anObject.toString());
BookEntry entry=(BookEntry)map.get(anObject.toString());
if(entry!=null)
panels.setIcon(entry.getImage());
else
panels.setIcon(questionIcon);
}
}
public Component getEditorComponent(){return panels;}
public Object getItem(){return panels.getText();}
public void selectAll(){panels.selectAll();}
public void addActionListener(ActionListener l){
panels.addActionListener(l);
}
public void removeActionListener(ActionListener l){
panels.removeActionListener(l);
}
class ImagePanel extends JPanel{
JLabel imageIconLabel;
JTextField textfields;
public ImagePanel(BookEntry initialEntry){
setLayout(new BorderLayout());
imageIconLabel=new JLabel(initialEntry.getImage());
textfields=new JTextField(initialEntry.getTitle());
textfields.setColumns(45);
add(imageIconLabel,BorderLayout.WEST);
add(textfields,BorderLayout.EAST);
}
public void setText(String s){textfields.setText(s);}
public String getText(){return (textfields.getText());}
public void setIcon(Icon i){
imageIconLabel.setIcon(i);
repaint();
}
public void selectAll(){textfields.selectAll();}
public void addActionListener(ActionListener l){
textfields.addActionListener(l);
}
public void removeActionListener(ActionListener l){
textfields.removeActionListener(l);
}
}
}
public class EditableComboBox extends JPanel{
private BookEntry books[]={ new BookEntry("1.....","1.gif"),new BookEntry("2.....","2.gif"),new BookEntry("3.....","3.gif"),
new BookEntry("4.....","4.gif"),new BookEntry("5.....","5.gif"),new BookEntry("6.....","6.gif"),
new BookEntry("7.....","7.gif"),new BookEntry("8.....","8.gif"),new BookEntry("9.....","9.gif"),
new BookEntry("10.....","10.gif"),new BookEntry("11.....","11.gif"),new BookEntry("12.....","12.gif"),
new BookEntry("13.....","13.gif")};
Map bookMap=new HashMap();
public EditableComboBox(){
for(int i=0;i<books.length;i++)
{bookMap.put(books[i].getTitle(),books[i]);
}
setLayout(new BorderLayout());
JComboBox bookCombo=new JComboBox(books);
bookCombo.setEditable(true);
bookCombo.setEditor(new ComboBoxEditorExample(bookMap,books[0]));
bookCombo.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){
System.out.println("You chose"+((JComboBox)e.getSource()).getSelectedItem()+"!");
}});
bookCombo.setActionCommand("Hello");
add(bookCombo,BorderLayout.CENTER);
}
public static void main(String agrs[]){
JFrame frames=new JFrame("Combo Box Example");
frames.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frames.setContentPane(new EditableComboBox());
frames.setVisible(true);
frames.pack();
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货