[英]Java MouseListener Not Working
I am trying to get the console to print when the mouse is pressed within an object of class RenderCanvas
which extends
JPanel
.当鼠标在
extends
JPanel
的 class RenderCanvas
的 object 中按下时,我试图让控制台打印。 However, I am getting no feedback when I press the mouse down in the window.但是,当我在 window 中按下鼠标时,我没有收到任何反馈。 Any suggestions as to what I might be able to change to make the
MouseListener
work?关于我可以改变什么以使
MouseListener
工作的任何建议?
Here is my code:这是我的代码:
RenderCanvas Class:渲染画布 Class:
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
import java.awt.event.MouseAdapter;
public class RenderCanvas extends JPanel {
private List<Rect> rectangles = new ArrayList<Rect>();
private List<Line> lines = new ArrayList<Line>();
public void renderCanvas() {
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
addRect(0, 0, 200, 200, Color.ORANGE);
System.out.println(e);
}
});
}
public void paintComponent(Graphics g) {
for (Rect rectangle : rectangles) {
rectangle.draw(g);
}
for (Line line : lines) {
line.draw(g);
}
}
public void addRect(int x, int y, int width, int height, Color color) {
Rect rectangle = new Rect(x, y, width, height, color);
this.rectangles.add(rectangle);
}
public void addLine(int y, int width, Color color) {
Line line = new Line(y, width, color);
this.lines.add(line);
}
}
Main Class:主要 Class:
import javax.swing.*;
import java.awt.*;
import java.util.Random;
public class Main {
public static void main(String[] args) {
JFrame window = new JFrame("Window");
RenderCanvas canvas = new RenderCanvas();
window.setContentPane(canvas);
window.setSize(640, 360);
window.setLocation(640, 360);
window.setVisible(true);
}
}
Thanks in advance for any help!提前感谢您的帮助!
public void renderCanvas()
is NOT a constructor. public void
renderCanvas()
不是构造函数。 Change改变
public void renderCanvas()
to至
public RenderCanvas()
Notice the upper-case "R" and the absence of the "void" return type注意大写的“R”和没有“void”返回类型
void RenderCanvas()
is not being called.没有调用
void RenderCanvas()
。 I believe you mean just public RenderCanvas()
instead of public void RenderCanvas
, since you're only using the ctor in the main method.我相信您的意思只是
public RenderCanvas()
而不是public void RenderCanvas
,因为您只在 main 方法中使用 ctor。
I think you're intending this method:我认为您打算使用这种方法:
public void renderCanvas() {
to be a constructor for the RenderCanvas class;成为 RenderCanvas class 的构造函数; it's not, though, for two reasons: it's not capitalized the same way (small r vs capital R) and also it has a return type.
但是,它不是,有两个原因:它的大写方式不同(小 r 与大写 R),而且它有一个返回类型。 Constructors have no return type;
构造函数没有返回类型; the line should look like
这条线应该看起来像
public RenderCanvas() {
Because this isn't a constructor, it's a method, and nobody's calling it, so your event handler is never being added.因为这不是一个构造函数,它是一个方法,没有人调用它,所以你的事件处理程序永远不会被添加。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.