不知道为什么我无法让 addMouseListener(this())、addMouseMotionListener(this()) 工作

发布于 2024-10-07 04:41:36 字数 2465 浏览 4 评论 0原文

这是我的 RocketShip 的面板,我想向其中添加 MouseListener 和 MouseMotionListener,但它告诉我“this”是一个无效名称。我的教科书有时会使用这个,教授也建议我们在这种情况下使用“这个”,所以我对问题是什么感到困惑。例如,我的代码如下。感谢您提供的任何建议。

    import java.applet.*;
import java.awt.*;           
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JPanel;

//I cannot understand how to draw my new rocketship or how to update the coordinates for mouse moved, mouse dragged, mouse pressed or mouse released
//also not sure how to work with setShooting
public class RocketShipPanel extends JPanel implements MouseListener,MouseMotionListener
{
    private final int WIDTH = 300, HEIGHT = 300;
    RocketShip ship = new RocketShip();

    public RocketShipPanel(){ //constructor
        addMouseListener (this());
        addMouseMotionListener (this());

        setBackground (Color.black);
        setPreferredSize (new Dimension(WIDTH, HEIGHT));
    }


    public void paintComponent (Graphics page)
    {
        super.paintComponent(page);

        ship.draw(page, 50);


    }

    public void mousePressed (MouseEvent event)
    {
        ship.setShooting(true);
        repaint();
    }
    public void mouseReleased (MouseEvent event) 
    {
        ship.setShooting(false);
        repaint();
    }
    public void mouseMoved (MouseEvent event)
    {

        Point point1 = event.getPoint();
        int x = point1.x;
        int y = point1.y;
        ship.move(x, y);

        repaint();
    }
    public void mouseDragged (MouseEvent event)
    {

        Point point1 = event.getPoint();
        int x = point1.x;
        int y = point1.y;
        ship.move(x, y);

        //pointList.add(event.getPoint());

        repaint();
    }
    //--------------------------------------------------------------
    //  Provide empty definitions for unused event methods.
    //--------------------------------------------------------------

    public void mouseEntered (MouseEvent event) {}
    public void mouseExited (MouseEvent event) {}
    public void mouseClicked(MouseEvent event) {}


    public static void main (String[] args)
    {
        JFrame frame = new JFrame ("RocketShip");
        frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

        frame.getContentPane().add (new RocketShipPanel());

        frame.pack();
        frame.setVisible(true);
    }
}

So this is the panel for my RocketShip and I want to add my MouseListener and my MouseMotionListener to it but it is telling me that "this" is an invalid name. My textbook uses this sometimes and the professor also recommended that we use "this" in this instance so I am stuck about what the problem is. My code is below for example. Thanks for any advice you might offer.

    import java.applet.*;
import java.awt.*;           
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JPanel;

//I cannot understand how to draw my new rocketship or how to update the coordinates for mouse moved, mouse dragged, mouse pressed or mouse released
//also not sure how to work with setShooting
public class RocketShipPanel extends JPanel implements MouseListener,MouseMotionListener
{
    private final int WIDTH = 300, HEIGHT = 300;
    RocketShip ship = new RocketShip();

    public RocketShipPanel(){ //constructor
        addMouseListener (this());
        addMouseMotionListener (this());

        setBackground (Color.black);
        setPreferredSize (new Dimension(WIDTH, HEIGHT));
    }


    public void paintComponent (Graphics page)
    {
        super.paintComponent(page);

        ship.draw(page, 50);


    }

    public void mousePressed (MouseEvent event)
    {
        ship.setShooting(true);
        repaint();
    }
    public void mouseReleased (MouseEvent event) 
    {
        ship.setShooting(false);
        repaint();
    }
    public void mouseMoved (MouseEvent event)
    {

        Point point1 = event.getPoint();
        int x = point1.x;
        int y = point1.y;
        ship.move(x, y);

        repaint();
    }
    public void mouseDragged (MouseEvent event)
    {

        Point point1 = event.getPoint();
        int x = point1.x;
        int y = point1.y;
        ship.move(x, y);

        //pointList.add(event.getPoint());

        repaint();
    }
    //--------------------------------------------------------------
    //  Provide empty definitions for unused event methods.
    //--------------------------------------------------------------

    public void mouseEntered (MouseEvent event) {}
    public void mouseExited (MouseEvent event) {}
    public void mouseClicked(MouseEvent event) {}


    public static void main (String[] args)
    {
        JFrame frame = new JFrame ("RocketShip");
        frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

        frame.getContentPane().add (new RocketShipPanel());

        frame.pack();
        frame.setVisible(true);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

川水往事 2024-10-14 04:41:36

this 后面不需要括号。它就像一个变量名,而不是一个方法。

You don't need the parentheses after this. It's like a variable name, not a method.

爱已欠费 2024-10-14 04:41:36

this是JAVA中的关键字,不应将其视为方法,它是对当前对象的引用。您可以在以下链接中了解此关键字 - http://docs。 oracle.com/javase/tutorial/java/javaOO/thiskey.html

this is a keyword in JAVA and it should not be treated as a method, it is a reference to the current object. You can learn about this keyword in the following link - http://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文