-
Notifications
You must be signed in to change notification settings - Fork 0
/
Swing15.java
44 lines (33 loc) · 1.09 KB
/
Swing15.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
import javax.swing.*;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
public class Swing15 {
public static void main(String[] args) {
MyFrame frame = new MyFrame();
frame.setTitle("ItemEvent");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(0, 0, 800, 500);
frame.setVisible(true);
}
static class MyFrame extends JFrame implements ItemListener {
JComboBox cb;
JTextArea ta;
public MyFrame() {
Container c = getContentPane();
c.setLayout(null);
String[] values = {"A", "B", "C", "D"};
cb = new JComboBox(values);
cb.setBounds(50, 50, 100, 30);
c.add(cb);
cb.addItemListener(this);
ta = new JTextArea();
ta.setBounds(200,50,100,100);
c.add(ta);
}
@Override
public void itemStateChanged(ItemEvent e) {
ta.setText(cb.getSelectedItem().toString());
}
}
}