本题的功能是监听鼠标的操作。鼠标置于窗口中单击时(左键或右键),在单击的地方会画一个小矩形.如果将鼠标置于小矩形上,则鼠标光标状态改为小十字。按下鼠标左键可拖曳,双击鼠标左键(或右键)时,小矩形消失。 importjava.awl.*; importjav

admin2013-12-02  28

问题 本题的功能是监听鼠标的操作。鼠标置于窗口中单击时(左键或右键),在单击的地方会画一个小矩形.如果将鼠标置于小矩形上,则鼠标光标状态改为小十字。按下鼠标左键可拖曳,双击鼠标左键(或右键)时,小矩形消失。
importjava.awl.*;
importjava.awt.event.*;
importjava.util.*;
importjava.awt.geom.*;
importjavax.swing.*;
publicclassjava3
(
publicstaticvoidmain(Stringr]args)
{
MouseFrameframe=newMouseFrame();
frame.setDefaultClose()peration(JFrame.EXIT一
()N—CLOSE);
frame.show();
}
}
classMouseFrameextendsJFrame
{
publicMouseFrame()
{
setTitle(”java3”);
setSize(DEFAULT—WIDTH,DEFAULT—
HEIGHT);
MousePanelpanel=newMousePanel();
ContainercontentPane=getContentPane();
contentPane.add(panel);
}
publicstaticfinalintDEFAULTWlDTH=300;
publicstaticfinalintDEFAUT一HEIGHT=2001
}
classMousePaneIextendsJPanel
{
publicMousePanel()
{
squares=newArrayList();
current=null;
addMouseListener(newMouseHandler());
addMouseMotionListener(newMouseMotionHan.
dler(    ));
}
publicvoidpaintComponent(Graphicsg)
{
super.paintComponent(g);
Graphics2Dg2=(Graphics2D)g;
for(inti=0;ig2.draw((Rectangle2D)squares.get(i));
}
publicvoidfind(Point2Dp)
{
for(inti=0li{
Rectangle2Dr=(Rectangle2D)squares.get(i);
if(r.contains(p))returnr;
}
returnnulI;
}
publicvoidadd(Point2Dp)
{
doublex=P.getX();
doubleY=P.getY();
current=newRectangle2@D@Double(
x——SIDELENGTH/2.
y—SIDELENGTH/2,
SlDELENGTH.
SIDELENGTH);
squares.add(current);
repaint();
}
publicvoidremove(Rectangle2Ds)
{
if(s==null)return:
if(s==current)current=nulI;
squares.remove(s)l
repaint();
}
privatestaticfinalintSIDELENGTH=10;
privateArrayListsquares;
privateRectangle2Dcurrent;
privateclassMouseHandlerextendsMouseAction-
Listener
{
publicvoidmousePressed(MouseEventevent)
{
current—find(event.getPoint());
if(current一=null)
add(event.getPoint());
}
publicvoidmouseClicked(MouseEventevent)
{
current=find(event.getPoint());
if(current!=null&&event.getClickCount()
>=2)
remove(current);
}
}
privateclassMouseMotionHandlerimplementsMouse-
MotionListener
{
publicvoidmouseMoved(MouseEventevent)
(
if(find(event.getPoint)=一null)
setCursor(Cursor.getDefauhCursor());
else
setCursor(Cursor.getPredefinedCursor
(Cursor.CROSSHAIR—CURS()R));
}
publicvoidmouseDragged(MouseEventevent)
{
if(current!=null)
{
intX=event.getX();
intY=event.getY();
current.setFrame(
X—SIDELENGTH/2。
y—SIDELENGTH/2,
SIDELENGTH.
SlDELENGTH);
repaint();
}
}
}
}

选项

答案第1处:publicRectangle2Dfind(Point2Dp)第2处:privateclassMouse}~andlerextendsMouse—Adapter第3处;find(event.gelPoint( ))=一null

解析 第1处定义一个返回类型为Rectangle2D的find函数且有一个Point2D型的形参;第2处定义了继承鼠标适配器MouseAdapt的MouseI-tandler;第3处判断find函数的返回值是否为空。
转载请注明原文地址:https://kaotiyun.com/show/FmID777K
0

相关试题推荐
最新回复(0)