问答题
本程序是一个Applet,页面中有一个按钮“横向移动”和一个标签“我可以被种植”,初始单击按钮时,按钮做横向向右移动,碰到标签后标签消失,并且按钮的名称变为“纵向移动”,如果继续单击按钮,则按钮做纵向向下移动,如图所示。
import java.awt. *;
import java.awt.event.*;
public class exam_80 extends java.applet.Applet implements ActionListener
{ Button button;
Label label;
public void init()
{ button=new Button("横向移动"); button.setBackground(Color.red);
button.addActionListener(this);
label=new Label ("我可以被碰掉", Label. CENTER);
label.setBackground(Color.blue);
add(button); add(label);
}
public void actionPerformed(ActionEvent e)
{ ______ rect=button.getBounds();
if(rect.intersects(______))
{ label.setVisible(false);
}
if(label.isVisible())
{ button.setLocation(rect.x+3,rect.y);
}
else
{ button.setLocation(rect.x,rect.y+3);
button.setLabel("纵向走动");
}
}
}
exam_80.html:
<html>
<head><title>exam_80</title></head>
<body>
<applet code="exam_80.class" width="400" height="500">
</applet>
</body>
</html>
【参考答案】
第1处:Rectangle
第2处:label.getBounds()