jframe.addmouselistener do note not
我目前正在用java编写一个应用程序,需要接收用户的输入。我尝试了很多方法来实现输入,但没有一个起作用。如果有帮助的话,我正在使用图形界面渲染对象。我尝试了几种方法来实现鼠标交互:
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class Window extends Canvas {
JFrame frame;
boolean mousePressed = false;
public Window(String windowName, int windowWidth, int windowHeight, Main main) {
frame = new JFrame(windowName);
frame.setSize(new Dimension(windowWidth, windowHeight));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
System.out.println("Clicked!");
}
@Override
public void mousePressed(MouseEvent e) {
System.out.println("Clicked!");
}
@Override
public void mouseReleased(MouseEvent e) {
System.out.println("Clicked!");
}
@Override
public void mouseEntered(MouseEvent e) {
System.out.println("Clicked!");
}
@Override
public void mouseExited(MouseEvent e) {
System.out.println("Clicked!");
}
});
frame.add(main);
frame.setVisible(true);
main.start();
}
}
但是
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class Window extends Canvas implements MouseListener {
JFrame frame;
boolean mousePressed = false;
public Window(String windowName, int windowWidth, int windowHeight, Main main) {
frame = new JFrame(windowName);
frame.setSize(new Dimension(windowWidth, windowHeight));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.addMouseListener(this);
frame.add(main);
frame.setVisible(true);
main.start();
}
@Override
public void mouseClicked(MouseEvent e) {
System.out.println("Clicked!");
}
@Override
public void mousePressed(MouseEvent e) {
System.out.println("Clicked!");
}
@Override
public void mouseReleased(MouseEvent e) {
System.out.println("Clicked!");
}
@Override
public void mouseEntered(MouseEvent e) {
System.out.println("Clicked!");
}
@Override
public void mouseExited(MouseEvent e) {
System.out.println("Clicked!");
}
}
,当我运行该应用程序时,它不会打印任何内容。 这是我的主要课程:
import java.awt.*;
import java.awt.image.BufferStrategy;
public class Main extends Canvas implements Runnable {
final String windowName = "Hand Written Digits Reader";
final int windowWidth = 500;
final int windowHeight = 500;
boolean running = false;
Window window;
Thread thread;
NeuralNetwork neuralNetwork = new NeuralNetwork();
public Main() {
window = new Window(windowName, windowWidth, windowHeight, this);
}
public void start() {
thread = new Thread(this);
thread.start();
running = true;
}
public void stop() {
try {
thread.join();
running = false;
} catch(Exception e) {
e.printStackTrace();
}
}
public void run() {
long lastTime = System.nanoTime();
double fps = 1.0;
double ns = 100000000 / fps;
double delta = 0;
while(running)
{
long now = System.nanoTime();
delta += (now - lastTime) / ns;
lastTime = now;
while(delta >= 1)
{
tick();
delta--;
}
render();
}
stop();
}
public void tick() {
}
public void render() {
BufferStrategy bs = this.getBufferStrategy();
if (bs == null) {
this.createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
neuralNetwork.drawGUI(g, window.frame, window.mousePressed);
g.dispose();
bs.show();
}
public static void main(String[] args) {
new Main();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将
Mouselistener
添加到您有兴趣监视的组件中,在这种情况下,这将是您的main
类。jframe
从来都不是Mouselistener
的好候选者,它是一个复合组件,这意味着它在顶部显示了许多组件(来自 root panes )
您的
窗口
类是有意义的,无需从canvas
延伸,并且由于window> window
已经是AWT的课程,这也令人困惑。无需
main
来调用窗口
,这只是添加了更多的混乱。组件不应创建自己的窗口,这不是责任,而是应该将其添加到呼叫者在创建时选择的容器。这个...
永远不会工作,因为循环正在等待
运行
befalse
和join
是阻止呼叫,该线程将永远退出。另外,请考虑运行
是在不同线程上使用的,并且您最终可能会进入“肮脏的读/写”方案。我以前看过此代码,所以无论您从哪里复制它,都停下来找到更好的来源。
您的主循环也很危险。它正在创建一个“野生环”,这些循环可以消耗CPU周期,从而饿死其他线程(或者应尽可能频繁地运行),并且通常不会引起性能问题的终点。相反,您应该(至少)使用
thread.yield
或更好的,thread.thread.sleep
。请记住,屏幕只能刷新一秒钟,因此更新超出该范围的UI只是在浪费时间。 “体面”目标可能是60fps。
delta +=(现在 - lastime) / ns; < / code>对我没有意义(但我很愚蠢),您真正想知道的是运行最后更新所需的时间循环以及需要等待多长时间才能维持帧速率,
double delta =(现在 - lastime) / ns; < / code>更有意义(对我来说)。
您对
BufferStrategy
的使用也是错误的。 BufferStrategy 有一个很好的例子,您应该如何使用它。同样,我不知道您从哪里获得了原始代码,但是我不建议将来使用该来源。
可运行的示例...
Add the
MouseListener
to the component you are interested in monitoring, in this case, that would be yourMain
class.JFrame
is never a good candidate for aMouseListener
, it's a composite component, meaning that it has a number of components presented on top if it, any of which could consume mouse events(from How to Use Root Panes)
Your
Window
class makes sense, there's no need for it to extend fromCanvas
, and sinceWindow
is already a class in AWT, it's also confusing.There's no need for
Main
to be callingWindow
, this is just adding more confusion. A component shouldn't be creating it's own window, that's not it's responsibility, instead, it should be added to a container of callers choosing when its created.This...
is never going to work, since the loop is waiting for
running
to befalse
andjoin
is blocking call, there's no way that the thread will ever exit. Also consider thatrunning
is been used across different threads and it's possible that you'll end up in a "dirty read/write" scenario.I've seen this code before, so where ever you are copying it from, stop and find a better source.
Your main loop is also dangerous. It's creating a "wild loop", these kind of loops can consume CPU cycles, starving other threads from running (or running as often as they should) and generally cause no end of performance issues. Instead, you should be (at the very least), making use of
Thread.yield
or, even better,Thread.sleep
.Remember, a screen can only refresh so many times a second, so updating the UI beyond that range is just wasting time. A "decent" target might be 60fps.
delta += (now - lastTime) / ns;
doesn't make sense to me (but I be dumb), what you really want to know is the amount of time it took to run the last update cycle and how long you need to wait in order to maintain the frame rate,double delta = (now - lastTime) / ns;
makes more sense (to me).Your use of
BufferStrategy
is also, well, wrong. The JavaDocs forBufferStrategy
has an excellent example how you should be using it.Again, I don't know where you got the original code from, but I don't recommending using that source in the future.
Runnable example...