[英]MouseEvent problem AWT Java
I'm simply trying to I'm simply trying to draw a circle in the same location as mouse click, but I can't get it to work. 我只是想在与鼠标单击相同的位置绘制一个圆,但是我无法使其正常工作。 Here is the code:
这是代码:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class LineApp
{
public static void main ( String args[])
{
MainWindow mainWdw = new MainWindow();
}
}
class MainWindow extends Frame
{
private Point recPoint;
private boolean mouseClick;
MainWindow()
{
mouseClick = false;
setBackground( Color.BLACK );
setSize( 400,300 );
setLocation( new Point( 300,300));
addComponentListener(new ComponentCatcher());
addWindowListener(new WindowCatcher());
addMouseListener(new MouseCatcher());
setVisible( true );
}
public void paint( Graphics gc )
{
gc.setColor(Color.ORANGE);
if(mouseClick)
{
gc.setColor(Color.ORANGE);
gc.fillOval(recPoint.x, recPoint.y, 30,30);
}
}
class WindowCatcher extends WindowAdapter
{
public void windowClosing( WindowEvent evt)
{
evt.getWindow().dispose();
System.exit (0);
}
}
class ComponentCatcher extends ComponentAdapter
{
public void componentResized (ComponentEvent evt)
{
repaint();
}
}
class MouseCatcher extends MouseAdapter
{
public void mousedPressed ( MouseEvent evt)
{
Point mousePt = new Point();
mousePt = evt.getPoint();
recPoint = new Point(mousePt.x,mousePt.y);
mouseClick = true;
repaint();
}
}
} }
The method in MouseAdapter
is called mousePressed
, not mousedPressed
. MouseAdapter
的方法称为mousePressed
,而不是mousedPressed
。
The @Override
-annotation can help you to avoid errors like that. @Override
-annotation可以帮助您避免此类错误。 If you would have used 如果你会用
@Override
public void mousedPressed ( MouseEvent evt)
{
...
your code would not have compiled. 您的代码将无法编译。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.