gpt4 book ai didi

java - MouseEvent问题 AWT Java

转载 作者:行者123 更新时间:2023-11-29 08:18:46 24 4
gpt4 key购买 nike

我只是想尝试在与鼠标单击相同的位置绘制一个圆圈,但我无法让它工作。这是代码:

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();
}
}

最佳答案

MouseAdapter 中的方法称为 mousePressed ,而不是 mousedPressed

@Override -annotation 可以帮助您避免这样的错误。如果你会用

  @Override
public void mousedPressed ( MouseEvent evt)
{
...

你的代码不会被编译。

关于java - MouseEvent问题 AWT Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2177402/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com