-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComboBoxDemo.java
51 lines (43 loc) · 1.54 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
//uses combo boxs to choose from a selection of choices
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ComboBoxDemo extends JFrame implements ActionListener{
//JFrame width and height
public static final int WIDTH = 300;
public static final int HEIGHT = 200;
//JLabel message
JLabel message;
/**
* main
* creates the object JFrame and sets it to visible
*/
public static void main(String[] args){
ComboBoxDemo gui = new ComboBoxDemo();
gui.setVisible(true);
}
/**
* ComboBoxDemo
* creates the ComboBox object, adds to JFrame.
*/
public ComboBoxDemo(){
super("Combo Box Demo");
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
String[] petStrings = {"Bird", "Cat", "Dog", "Rabbit", "Pig"};
JComboBox petList = new JComboBox(petStrings);//combo box stuff
petList.setSelectedIndex(4);
petList.addActionListener(this);
message = new JLabel("I chose " + petStrings[4] + ".");
message.setFont(message.getFont().deriveFont(Font.ITALIC));//sets font type to italic
message.setHorizontalAlignment(JLabel.CENTER);
add(petList, BorderLayout.NORTH);
add(message, BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent e){
JComboBox cb = (JComboBox)e.getSource();
String petName = (String)cb.getSelectedItem();
message.setText("I chose " + petName + ".");
}
}