gpt4 book ai didi

java - 鼠标监听器 - MouseClicked

转载 作者:行者123 更新时间:2023-11-30 05:47:14 29 4
gpt4 key购买 nike

经过 2 个小时的搜索,我真的找不到为什么我的代码不起作用,所以我想知道你是否可以提供帮助。

当我按下按钮时,我想看到的只是“点击”。我的类 MouseInput 实现了 MouseListener ,在方法 mouseClicked 中我得到的只是 system.out...("clicked");

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;

public class App implements Runnable {


private Display display;
private BufferStrategy bs;
private Graphics g;


private int cubeSide = 150;
private String title;
private int height,width;
private boolean running = false;
private Thread thread;
private MouseInput mouseInput;



public App(String title,int width,int height){
this.height=height;
this.width=width;
this.title=title;
display = new Display(title, width, height);
}
void setBufferStrategy(){
if(display.getCanvas().getBufferStrategy()==null){
display.getCanvas().createBufferStrategy(3);
}
bs = display.getCanvas().getBufferStrategy();
}
void init(){
setBufferStrategy();
mouseInput = new MouseInput();
display.getFrame().addMouseListener(mouseInput);
}
public synchronized void start(){
if(running==true)
return;
running=true;
thread = new Thread(this);
thread.start();

}
void render(){
}
@Override
public void run() {
init();
while(running){
render();
}

}
public synchronized void stop(){

}
}

鼠标输入代码:

`import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;


public class MouseInput implements MouseListener {

@Override
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
System.out.println("Click");
}

@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub

}

@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub

}

@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub

}

@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub

}

}`

对于显示类:

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;

import javax.swing.JFrame;


public class Display {
private JFrame frame;
private Canvas canvas;
private String title;
private int width,height;


public Display(String title,int width,int height){
this.width = width;
this.height=height;
this.title=title;
CreateDisplay();
}
public void CreateDisplay(){
frame = new JFrame(title);
canvas = new Canvas();
frame.setSize(width,height);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setFocusable(false);
frame.setResizable(false);
frame.setLocationRelativeTo(null);

canvas.setPreferredSize(new Dimension(width,height));
canvas.setMaximumSize(new Dimension(width,height));
canvas.setMinimumSize(new Dimension(width,height));
canvas.setBackground(Color.WHITE);
frame.add(canvas);
frame.pack();

}
public Canvas getCanvas(){
return canvas;
}
public JFrame getFrame(){
return frame;
}
}

最佳答案

我认为问题可能出在 display.getFrame().addMouseListener(mouseInput) 中:我想 display.getFrame() 返回一个扩展类的实例java.awt.Component;根据API reference , addMouseListener

Adds the specified mouse listener to receive mouse events from this component.

我认为按钮上的事件不是来自您注册监听器的组件的事件:您可以尝试在 Canvas 实例而不是框架实例上注册监听器吗?可能事件源自 Canvas ,而不是框架......

关于java - 鼠标监听器 - MouseClicked,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54633590/

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