经过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;
}
}
答案 0 :(得分:1)
我认为问题可能出在display.getFrame().addMouseListener(mouseInput)
中:我想display.getFrame()
返回扩展了java.awt.Component
的类的实例;根据{{3}},addMouseListener
添加指定的鼠标侦听器以接收来自此组件的鼠标事件。
我认为您按钮上的事件不是您在其上注册侦听器的组件的事件:您可以尝试在画布实例而不是框架实例上注册侦听器吗?可能是事件起源于画布,而不是框架...
答案 1 :(得分:0)
首先,您可以使用嵌入式鼠标适配器而不是扩展鼠标侦听器,并且需要一个单独的文件来存储鼠标代码。
第二,如果要观察按钮的单击,请将侦听器添加到按钮。
yourJButton.addMouseListener( new MouseAdapter()
{
@Override
public void mouseClicked( MouseEvent e )
{
{
//do stuff
}
}
});