问答题
本程序是一个Applet,页面中有一个面板和两个按钮:“全部清除”和“部分清除”,面板上有上下两个字符串,单击“部分清除”按钮时,画板上方的字符串消失,单击“全部清除”按钮时,则画板上的所有字符串都消失,如图所示。
import java.awt.*;
import java.awt.event.*;
class MyCanvas extends Canvas
{ int n=-1;
MyCanvas()
{ setSize(150,120);
setBackground(Color.pink);
}
public void paint(Graphics g)
{ g.setColor(Color.red);
g.drawString("欢迎学习 Java编程语言",10,12);
g. drawString ("Java 语言同C++有很多的不同点", 10, 40);
}
public void setN(int n)
{ this.n=n;
}
public void update(Graphics g)
{ int width=0, height=0;
width=getSize().width;
height=getSize().height;
if(n==0)
{ ;
}
else if (n==1)
{ g.clearRect(2,2,width, 40);
}
}
}
public class exam_81 extends java.applet.Applet implements ActionListener
{ Button b1,b2;MyCanvas canvas;
public void init ()
{ canvas=new MyCanvas ();
b1=new Button ("全部消除");
b1.addAetionListener (this);
b2=new Button ("部分消除");
b2.addActionListener (this);
add (b1);
add (b2);
add (canvas);
}
public void actionPerformed(ActionEvent e)
{ if (e.getSource () ==b1)
{ ______;
}
if (e.getSource () ==b2)
{ canvas.setN (1);canvas.repaint ();
}
}
}
exam_81.html:
<html>
<head><title>exam_81</title></head>
<body>
<applet code="exam_81.class" width="400" height="500">
</applet>
</body>
</html>
【参考答案】
第1处:g.clearRect(0,0,width,height)
第2处:canvas.setN(0);ca......
(↓↓↓ 点击下方‘点击查看答案’看完整答案 ↓↓↓)