简答题:本题中,鼠标在窗口中单击一下,就在单击的位置生成一个小矩形,如果在小矩形上双击鼠标左键,则删除小矩形。 import j

  • 题目分类:Java
  • 题目类型:简答题
  • 号外号外:注册会员即送体验阅读点!
题目内容:
本题中,鼠标在窗口中单击一下,就在单击的位置生成一个小矩形,如果在小矩形上双击鼠标左键,则删除小矩形。
import java.awt.*;
import java.awt.event.*;
import javax swing.*;
class MousePanel extends JPanel extends MouseMo-
tionListener
{public MousePanel()
{addMouseListener(new MouseAdapter()
{public void mousePressed(MouseEvent evt)
{int X=evt.getX();
int Y=evt.getY();
current=find(x,y);
if(current<0)
add(x,y);
}
public void mouseClicked(MouseEvent evt)
{int X=evt.getX();
int Y=evt.getY();
if(evt.getClickCount()>=2)
{remove(current);
}
}
});
addMouseMotionListener(this);
}
public void paintComponent(Graphics g)
{super.paintComponent()
for(int i=0;i<nsquares;i++)
draw(g,i);
}
public int find(int X,int y)
(for(int i=0;i<nsquares;i++)
if(squares[i].x-SQUARELENGTH/2<=
x&&
X<=squares[i].x+SQuARELENGTH/2
&&squares[i].Y-SQUARELENGTH/2<
=Y
&&y<=squares[i].Y+SQUARELENGTH
/2)
return i ;
return-1 ;
}
public void draw(Graphics g,int i)
{g.drawRect(squares[i].X-SQUARE-
LENGTH/2。
squares[i].Y-SQUARELENGTH/2,
SQUARELENGTH,
SQUARELENGTH);
}
public void add(int X,int Y)
{if(nsquares<MAXNSQUARES)
{squares[nsquares]=new Point(x,y);
current=nsquares ;
nsquares++;
repaint();
}
}
public void remove(int n)
{if(n<0 ‖ n>=nsquares)return;
Nsquares- -;
squares[n]=squares[nsquares];
if(current= =n)current= -l;
repaint();
}
public void mouseMoved(MouseEvent evt)
{}
public void mouseDragged(MouseEvent evt)
{}
private static final int SQUARELENGTH=10:
private static final int MAXNSQUARES=100;
private Point[]squares=new Point[MAX-
NSQUARES];
private int nsquares=0;
private int current=-l;
}
class MouseFrame extends JFramc
{public MouseFrame()
{setTitle("java3");
setSize(300,200);
addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{System.exit(0);
}
});
Container contentPane=getContentPane();
contentPane.add(MousePanel())
}
}
public class java3
{public static void main(String[]args)
{JFrame frame=new MouseFrame();
frame.show();
}
}
参考答案:【答案仅供学习,请勿对照自行用药等】
答案解析:

________________

________________

查看答案

Why was the man relieved at last?

Why was the man relieved at last?A.Because the telegram was a false one. B.Becau

查看答案

What's the woman?

What's the woman?A.An operator. B.A manager. C.A student. D.A secretary

查看答案

本题的功能是监听鼠标左右键的单击,以及面板中滚动条的添加。在窗口的画板中单击鼠标左键,在单击的位置绘制一个圆,当绘制的圆

本题的功能是监听鼠标左右键的单击,以及面板中滚动条的添加。在窗口的画板中单击鼠标左键,在单击的位置绘制一个圆,当绘制的圆大于画板的大小时,画板就添加滚动条,在画

查看答案

What does meir daughter do?

What does meir daughter do?A.She is a doctor. B.She is a teacher. C.She is a stu

查看答案