不知道为什么我无法让 addMouseListener(this())、addMouseMotionListener(this()) 工作
这是我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
this
后面不需要括号。它就像一个变量名,而不是一个方法。You don't need the parentheses after
this
. It's like a variable name, not a method.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