0% found this document useful (0 votes)
18 views1 page

Import Import Import Public Class Extends Implements: Image CMB BTN Arr

Uploaded by

Hassan Chaaban
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views1 page

Import Import Import Public Class Extends Implements: Image CMB BTN Arr

Uploaded by

Hassan Chaaban
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Ex2.

java Wednesday, July 28, 2021, 6:46 PM

1 import javax.swing.*;
2 import java.awt.*;
3 import java.awt.event.*;
4
5 public class Ex2 extends JFrame implements ActionListener, ItemListener {
6
7 JLabel image;
8 JComboBox<String> cmb;
9 JButton btn;
10 String[] arr = { "Tree", "Fish", "House" };
11
12 public Ex2() {
13 setDefaultCloseOperation(EXIT_ON_CLOSE);
14 image = new JLabel();
15 image.setPreferredSize(new Dimension(100, 100));
16 cmb = new JComboBox<String>(arr);
17 cmb.addItemListener(this);
18 cmb.addActionListener(this);
19 btn = new JButton("Close");
20 btn.addActionListener(this);
21 JPanel p1 = new JPanel();
22 JPanel p2 = new JPanel();
23 JPanel p3 = new JPanel();
24 p1.add(image);
25 p2.add(cmb);
26 p3.add(btn);
27 add("North", p1);
28 add("Center", p2);
29 add("South", p3);
30 setSize(200, 500);
31 setVisible(true);
32 }
33
34 public static void main(String[] args) {
37
38 public void itemStateChanged(ItemEvent e) {
39 if (e.getStateChange() == ItemEvent.SELECTED) {
40 Object item = e.getItem();
41 image.setIcon(new ImageIcon(cmb.getSelectedItem() + ".jpg"));
42 }
43 }
44
45 public void actionPerformed(ActionEvent e) {
46 if (e.getSource() == btn)
47 dispose();// System.exit(0);
48 else {
49 image.setIcon(new ImageIcon(cmb.getSelectedItem() + ".jpg"));
50 }
51 }
52
53 }
54

Page 1

You might also like