Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. class Combobox extends JPanel implements ActionListener{
  5. JLabel selection = new JLabel("Choose your option");
  6. JLabel picture = new JLabel();
  7. public Combobox() {
  8. String[] petStrings = { "rabbit", "puppy", "kitten", "chipmunk"};
  9. JComboBox petList = new JComboBox(petStrings);
  10. petList.addActionListener(this);
  11. picture.setPreferredSize(new Dimension(200, 200));
  12. add(selection);
  13. add(petList);
  14. add(picture);
  15. }
  16.  
  17. /** Listens to the combo box. */
  18. public void actionPerformed(ActionEvent e) {
  19. JComboBox cb = (JComboBox)e.getSource();
  20. String petName = (String)cb.getSelectedItem();
  21. selection.setText("You selected "+petName);
  22. ImageIcon icon = new ImageIcon(petName+".jpg");
  23. picture.setIcon(icon);
  24.  
  25. }
  26. }
  27.  
  28. public class SimpleJcomboBox {
  29. public static void main(String[] args) {
  30. JFrame frame = new JFrame("Cute Pets");
  31. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  32. Combobox c = new Combobox();
  33. frame.getContentPane().add(c);
  34. frame.pack();
  35. frame.setVisible(true);
  36. }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement