-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathComboBoxDemo.java
executable file
·77 lines (65 loc) · 2.58 KB
/
ComboBoxDemo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ComboBoxDemo extends JFrame {
// Declare an array of Strings for flag titles
private String[] flagTitles = {"Canada", "China", "Denmark",
"France", "Germany", "India", "Norway", "United Kingdom",
"United States of America"};
// Declare an ImageIcon array for the national flags of 9 countries
private ImageIcon[] flagImage = {
new ImageIcon("image/ca.gif"),
new ImageIcon("image/china.gif"),
new ImageIcon("image/denmark.gif"),
new ImageIcon("image/fr.gif"),
new ImageIcon("image/germany.gif"),
new ImageIcon("image/india.gif"),
new ImageIcon("image/norway.gif"),
new ImageIcon("image/uk.gif"),
new ImageIcon("image/us.gif")
};
// Declare an array of strings for flag descriptions
private String[] flagDescription = new String[9];
// Declare and create a description panel
private DescriptionPanel descriptionPanel = new DescriptionPanel();
// Create a combo box for selecting countries
private JComboBox jcbo = new JComboBox(flagTitles);
public static void main(String[] args) {
ComboBoxDemo frame = new ComboBoxDemo();
frame.pack();
frame.setTitle("ComboBoxDemo");
frame.setLocationRelativeTo(null); // Center the frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public ComboBoxDemo() {
// Set text description
flagDescription[0] = "The Canadian national flag ...";
flagDescription[1] = "Description for China ... ";
flagDescription[2] = "Description for Denmark ... ";
flagDescription[3] = "Description for France ... ";
flagDescription[4] = "Description for Germany ... ";
flagDescription[5] = "Description for India ... ";
flagDescription[6] = "Description for Norway ... ";
flagDescription[7] = "Description for UK ... ";
flagDescription[8] = "Description for US ... ";
// Set the first country (Canada) for display
setDisplay(0);
// Add combo box and description panel to the frame
add(jcbo, BorderLayout.NORTH);
add(descriptionPanel, BorderLayout.CENTER);
// Register listener
jcbo.addItemListener(new ItemListener() {
@Override /** Handle item selection */
public void itemStateChanged(ItemEvent e) {
setDisplay(jcbo.getSelectedIndex());
}
});
}
/** Set display information on the description panel */
public void setDisplay(int index) {
descriptionPanel.setTitle(flagTitles[index]);
descriptionPanel.setImageIcon(flagImage[index]);
descriptionPanel.setDescription(flagDescription[index]);
}
}