import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
public class ComboBox extends JDialog implements ActionListener, ItemListener {
final String[] authors = { "A", "B", "C", "D", "E", "F" };
final String[] images = { "a.jpg", "b.jpg", "c.jpg", "d.jpg","e.jpg", "f.jpg" };
JLabel display = new JLabel();
JComboBox combobox = new JComboBox(authors);
JButton button = new JButton("Close");
ImageIcon icon = new ImageIcon(ClassLoader.getSystemResource("a.jpg"));
public ComboBox() {
setLayout(new FlowLayout());
add(display);
combobox.setSelectedIndex(-1);
combobox.addItemListener(this);
add(combobox);
button.addActionListener(this);
add(button);
setSize(300, 300);
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
new ComboBox();
}
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
JComboBox combo = (JComboBox) e.getSource();
int index = combo.getSelectedIndex();
display.setIcon(new ImageIcon(ClassLoader.getSystemResource(images[index])));
}
}
}
|