1
//
例子1
2
import java.applet.
*
;import java.awt.
*
;import java.awt.
event
.
*
;
3
class
Mypanel extends Panel implements ActionListener
4![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{ Button button1,button2,button3;
5
Color backColor;
6
Mypanel() //构造方法。当创建面板对象时,面板被初始化为有三个按钮。
7![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ 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![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ if(e.getSource()==button1)
15![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ setBackground(Color.cyan);
16
}
17
else if(e.getSource()==button2)
18![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ setBackground(backColor);
19
}
20
}
21
}
22
public
class
Example11_1 extends Applet
23![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{ Mypanel panel1,panel2,panel3;
24
Button button;
25
public void init()
26![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ 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![None.gif](/Images/OutliningIndicators/None.gif)
33
//
例子2
34
import java.awt.
*
;import java.applet.
*
;
35
public
class
Example11_2 extends Applet
36![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{ Panel p ;
37
ScrollPane scrollpane;
38
public void init()
39![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ 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![None.gif](/Images/OutliningIndicators/None.gif)
48
//
例子3
49
import java.awt.
*
;import java.applet.
*
;
50
class
Mycanvas extends Canvas
51![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{ String s;
52
Mycanvas(String s)
53![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ this.s=s;
54
setSize(90,80);
55
setBackground(Color.cyan);
56
}
57
public void paint(Graphics g)
58![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ 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![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{ Mycanvas canvas1,canvas2;
66
public void init()
67![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ 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![None.gif](/Images/OutliningIndicators/None.gif)
75
//
例子4
76
import java.awt.
*
;
77
import java.applet.
*
;
78
import java.awt.
event
.
*
;
79
class
Mycanvas extends Canvas
80![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{ int x,y,r;
81
int red,green,blue;
82
Mycanvas()
83![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ setSize(100,100);
84
setBackground(Color.cyan);
85
}
86
public void setX(int x)
87![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ this.x=x;
88
}
89
public void setY(int y)
90![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ this.y=y;
91
}
92
public void setR(int r)
93![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ this.r=r;
94
}
95
public void paint(Graphics g)
96![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ g.drawOval(x,y,2*r,2*r);
97
}
98
}
99
public
class
Example11_4 extends Applet implements ActionListener
100![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{ Mycanvas canvas;
101
TextField inputR,inputX,inputY;
102
Button b;
103
public void init()
104![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ 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![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ int x,y,r;
120![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
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![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
131
x=0;y=0;r=0;
132
}
133
}
134
}
135
![None.gif](/Images/OutliningIndicators/None.gif)
2
![None.gif](/Images/OutliningIndicators/None.gif)
3
![None.gif](/Images/OutliningIndicators/None.gif)
4
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
![dot.gif](/Images/dot.gif)
5
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
6
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
7
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](/Images/dot.gif)
8
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
9
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
10
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
11
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
12
![ExpandedSubBlockEnd.gif](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
13
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
14
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](/Images/dot.gif)
15
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](/Images/dot.gif)
16
![ExpandedSubBlockEnd.gif](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
17
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
18
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](/Images/dot.gif)
19
![ExpandedSubBlockEnd.gif](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
20
![ExpandedSubBlockEnd.gif](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
21
![ExpandedBlockEnd.gif](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
22
![None.gif](/Images/OutliningIndicators/None.gif)
23
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
![dot.gif](/Images/dot.gif)
24
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
25
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
26
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](/Images/dot.gif)
27
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
28
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
29
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
30
![ExpandedSubBlockEnd.gif](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
31
![ExpandedBlockEnd.gif](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
32
![None.gif](/Images/OutliningIndicators/None.gif)
33
![None.gif](/Images/OutliningIndicators/None.gif)
34
![None.gif](/Images/OutliningIndicators/None.gif)
35
![None.gif](/Images/OutliningIndicators/None.gif)
36
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
![dot.gif](/Images/dot.gif)
37
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
38
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
39
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](/Images/dot.gif)
40
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
41
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
42
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
43
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
44
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
45
![ExpandedSubBlockEnd.gif](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
46
![ExpandedBlockEnd.gif](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
47
![None.gif](/Images/OutliningIndicators/None.gif)
48
![None.gif](/Images/OutliningIndicators/None.gif)
49
![None.gif](/Images/OutliningIndicators/None.gif)
50
![None.gif](/Images/OutliningIndicators/None.gif)
51
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
![dot.gif](/Images/dot.gif)
52
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
53
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](/Images/dot.gif)
54
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
55
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
56
![ExpandedSubBlockEnd.gif](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
57
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
58
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](/Images/dot.gif)
59
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
60
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
61
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
62
![ExpandedSubBlockEnd.gif](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
63
![ExpandedBlockEnd.gif](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
64
![None.gif](/Images/OutliningIndicators/None.gif)
65
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
![dot.gif](/Images/dot.gif)
66
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
67
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](/Images/dot.gif)
68
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
69
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
70
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
71
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
72
![ExpandedSubBlockEnd.gif](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
73
![ExpandedBlockEnd.gif](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
74
![None.gif](/Images/OutliningIndicators/None.gif)
75
![None.gif](/Images/OutliningIndicators/None.gif)
76
![None.gif](/Images/OutliningIndicators/None.gif)
77
![None.gif](/Images/OutliningIndicators/None.gif)
78
![None.gif](/Images/OutliningIndicators/None.gif)
79
![None.gif](/Images/OutliningIndicators/None.gif)
80
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
![dot.gif](/Images/dot.gif)
81
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
82
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
83
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](/Images/dot.gif)
84
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
85
![ExpandedSubBlockEnd.gif](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
86
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
87
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](/Images/dot.gif)
88
![ExpandedSubBlockEnd.gif](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
89
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
90
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](/Images/dot.gif)
91
![ExpandedSubBlockEnd.gif](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
92
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
93
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](/Images/dot.gif)
94
![ExpandedSubBlockEnd.gif](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
95
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
96
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](/Images/dot.gif)
97
![ExpandedSubBlockEnd.gif](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
98
![ExpandedBlockEnd.gif](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
99
![None.gif](/Images/OutliningIndicators/None.gif)
100
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
![dot.gif](/Images/dot.gif)
101
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
102
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
103
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
104
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](/Images/dot.gif)
105
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
106
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
107
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
108
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
109
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
110
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
111
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
112
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
113
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
114
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
115
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
116
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
117
![ExpandedSubBlockEnd.gif](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
118
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
119
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](/Images/dot.gif)
120
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](/Images/dot.gif)
121
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
122
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
123
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
124
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
125
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
126
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
127
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
128
![ExpandedSubBlockEnd.gif](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
129
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
130
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](/Images/dot.gif)
131
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
132
![ExpandedSubBlockEnd.gif](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
133
![ExpandedSubBlockEnd.gif](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
134
![ExpandedBlockEnd.gif](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
135
![None.gif](/Images/OutliningIndicators/None.gif)