Java2实用教程(第二版)程序代码——第十一章 面板和画布
1
//例子1
2
import java.applet.*;import java.awt.*;import java.awt.event.*;
3
class Mypanel extends Panel implements ActionListener
4
{ Button button1,button2,button3;
5
Color backColor;
6
Mypanel() //构造方法。当创建面板对象时,面板被初始化为有三个按钮。
7
{ button1=new Button("确定");button2=new Button("取消");button3=new Button("保存");
8
add(button1);add(button2);add(button3);
9
setBackground(Color.pink); //设置面板的底色。
10
backColor=getBackground(); //获取底色。
11
button1.addActionListener(this);button2.addActionListener(this);
12
}
13
public void actionPerformed(ActionEvent e)
14
{ if(e.getSource()==button1)
15
{ setBackground(Color.cyan);
16
}
17
else if(e.getSource()==button2)
18
{ setBackground(backColor);
19
}
20
}
21
}
22
public class Example11_1 extends Applet
23
{ Mypanel panel1,panel2,panel3;
24
Button button;
25
public void init()
26
{ panel1=new Mypanel();panel2=new Mypanel();panel3=new Mypanel();
27
button=new Button("我不在那些面板里");
28
add(panel1);add(panel2);add(panel3);
29
add(button);
30
}
31
}
32![]()
33
//例子2
34
import java.awt.*;import java.applet.*;
35
public class Example11_2 extends Applet
36
{ Panel p ;
37
ScrollPane scrollpane;
38
public void init()
39
{ p=new Panel();
40
scrollpane=new ScrollPane(ScrollPane.SCROLLBARS_ALWAYS);
41
p.add(new Button("one"));p.add(new Button("two"));
42
p.add(new Button("three"));p.add(new Button("four"));
43
scrollpane.add(p);//scrollpane添加一个面板。
44
add(scrollpane);//小程序添加滚动窗口。
45
}
46
}
47![]()
48
//例子3
49
import java.awt.*;import java.applet.*;
50
class Mycanvas extends Canvas
51
{ String s;
52
Mycanvas(String s)
53
{ this.s=s;
54
setSize(90,80);
55
setBackground(Color.cyan);
56
}
57
public void paint(Graphics g)
58
{ if(s.equals("circle"))
59
g.drawOval(20,25,30,30);
60
else if(s.equals("rect"))
61
g.drawRect(30,35,20,20);
62
}
63
}
64
public class Example11_3 extends Applet
65
{ Mycanvas canvas1,canvas2;
66
public void init()
67
{ canvas1=new Mycanvas("circle");canvas2=new Mycanvas("rect");
68
add(canvas1);
69
Panel p=new Panel();p.setBackground(Color.pink);
70
p.add(canvas2) ;
71
add(p);
72
}
73
}
74![]()
75
//例子4
76
import java.awt.*;
77
import java.applet.*;
78
import java.awt.event.*;
79
class Mycanvas extends Canvas
80
{ int x,y,r;
81
int red,green,blue;
82
Mycanvas()
83
{ setSize(100,100);
84
setBackground(Color.cyan);
85
}
86
public void setX(int x)
87
{ this.x=x;
88
}
89
public void setY(int y)
90
{ this.y=y;
91
}
92
public void setR(int r)
93
{ this.r=r;
94
}
95
public void paint(Graphics g)
96
{ g.drawOval(x,y,2*r,2*r);
97
}
98
}
99
public class Example11_4 extends Applet implements ActionListener
100
{ Mycanvas canvas;
101
TextField inputR,inputX,inputY;
102
Button b;
103
public void init()
104
{ canvas=new Mycanvas();
105
inputR=new TextField(6);
106
inputX=new TextField(6);
107
inputY=new TextField(6);
108
add(new Label("输入圆的位置坐标:"));
109
add(inputX);
110
add(inputY);
111
add(new Label("输入圆的半径:"));
112
add(inputR);
113
b=new Button("确定");
114
b.addActionListener(this);
115
add(b);
116
add(canvas);
117
}
118
public void actionPerformed(ActionEvent e)
119
{ int x,y,r;
120
try {
121
x=Integer.parseInt(inputX.getText());
122
y=Integer.parseInt(inputY.getText());
123
r=Integer.parseInt(inputR.getText());
124
canvas.setX(x);
125
canvas.setY(y);
126
canvas.setR(r);
127
canvas.repaint();
128
}
129
catch(NumberFormatException ee)
130
{
131
x=0;y=0;r=0;
132
}
133
}
134
}
135![]()

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

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135
