经过 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 {@Overridepublic void mouseClicked(MouseEvent arg0) { // TODO Auto-generated method stub System.out.println("Click");}@Overridepublic void mouseEntered(MouseEvent arg0) { // TODO Auto-generated method stub}@Overridepublic void mouseExited(MouseEvent arg0) { // TODO Auto-generated method stub}@Overridepublic void mousePressed(MouseEvent arg0) { // TODO Auto-generated method stub}
2 回答
萧十郎
TA贡献1815条经验 获得超13个赞
首先,您可以使用内联鼠标适配器,而不是扩展鼠标侦听器并且需要单独的鼠标代码文件。
其次,如果您想观察按钮的点击,请将侦听器添加到您的按钮。
yourJButton.addMouseListener( new MouseAdapter()
{
@Override
public void mouseClicked( MouseEvent e )
{
{
//do stuff
}
}
});
添加回答
举报
0/150
提交
取消