-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUseItemEvent.java
54 lines (51 loc) · 1.24 KB
/
UseItemEvent.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
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.applet.Applet;
public class UseItemEvent extends Applet implements ItemListener
{
Checkbox cDisp;
Button btnDisp;
Choice cFont;
public void init()
{
cDisp = new Checkbox("红色");
btnDisp = new Button("颜色显示");
cFont = new Choice();
cFont.add("10");
cFont.add("12");
cFont.add("14");
// 添加事件
cDisp.addItemListener(this);
cFont.addItemListener(this);
add(cDisp);
add(cFont);
add(btnDisp);
}
// 接口事件
public void itemStateChanged(ItemEvent e)
{
Checkbox temp;
Choice temp2;
Font oldF;
// 复选框
if (e.getItemSelectable() instanceof Checkbox)
{
temp = (Checkbox)(e.getItemSelectable());
// 选中为红色, 否则为蓝色
if (temp.getState())
btnDisp.setBackground(Color.red);
else
btnDisp.setBackground(Color.blue);
}
// 组合框
if (e.getItemSelectable() instanceof Choice)
{
oldF = btnDisp.getFont();
temp2 = (Choice)(e.getItemSelectable());
String s = temp2.getSelectedItem();
// 设置字体
btnDisp.setFont(new Font(oldF.getName(), oldF.getStyle(), Integer.parseInt(s)));
}
}
}