问答题
本程序是一个Applet,功能是监听鼠标在面板上的移动。页面上有三个不同颜色的面板,当鼠标移到其中一个面板上时,该面板颜色发生改变,当鼠标移开后,该面板颜色又恢复原色,如图所示。
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class exam_12 extends Applet
{
private ChapllPanel pl=new ChapllPanel("Have A Try",Color.yellow,new
Color(150,150,150));
private ChapllPanel p2=new ChapllPanel("You, The Best",Color.orange,new
Color(130,130,130));
private ChapllPanel p3=new ChapllPanel("You Can Do It",Color.yellow, new
Color(150,150,150));
private Label blankLabel=new Label(" ");
private Label msg=new Label("Roll over the panel.");
public void Init()
{
setLayout (new
GridLayout (2,3));
add (p1);
add (p2);
add (p3);
add (blankLabel);
add (msg); }
}
class ChapllPanel
extends Panel implements MouseActionListener
{
int x, y;
String message=new String();
Color color;
Color clr2;
public ChapllPanel(String msg, Color clr,Color clr2)
{
message=msg;
this.clr2=clr2;
color=clr;
setBackground(clr2);
addMouselistener(this);
}
public void mousePressed(MouseEvent e)
{
}
public void mouseClicked(MouseEvent e)
{
}
public void mouseEntered(MouseEvent e)
{
setBackground(color);
}
public void mouseExited(MouseEvent e)
{
setBackground(clr2);
}
public void mouseReleased(MouseEvent e)
{
}
public void paint(Graphics g)
{
g.setFont(new Font("Arial", Font.BOLD, 16));
g.drawString (message, 10,30);
}
}
exam_12.html:
<html>
<head><title>exam_12</title></head>
<body>
<applet code=exam_12.class width=500 height=100></applet>
</body>
</html>
【参考答案】
第1处:public void init()
第2处:extends Panel implements Mou......
(↓↓↓ 点击下方‘点击查看答案’看完整答案 ↓↓↓)