
Source code :
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ComboBoxTest extends JFrame { private JComboBox cmbImage; private JLabel label; private String arrGambar[] = {"Blushing.png", "Cool.png", "Happy.png","Smile.png","Winking.png"}; private Icon arrIcon[] = { new ImageIcon(arrGambar[0]), new ImageIcon(arrGambar[1]), new ImageIcon(arrGambar[2]), new ImageIcon(arrGambar[3]), new ImageIcon(arrGambar[4]) }; public ComboBoxTest() { super ("Mencoba Combo Box"); Container container = getContentPane(); container.setLayout(new FlowLayout()); cmbImage = new JComboBox (arrGambar); cmbImage.setMaximumRowCount(3); cmbImage.addItemListener( new ItemListener() { public void itemStateChanged (ItemEvent e) { if (e.getStateChange() == ItemEvent.SELECTED) label.setIcon(arrIcon[cmbImage.getSelectedIndex()]); } } //end anonimous inner class ); container.add(cmbImage); label = new JLabel (arrIcon[0]); container.add(label); setSize (350,400); setLocationRelativeTo(null); setVisible(true); } public static void main (String args[]) { ComboBoxTest test = new ComboBoxTest (); test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } |
Berikut ini tampilannya:
0 komentar:
Post a Comment