經過 2 小時的搜索后,我真的不知道為什么我的代碼不起作用,所以我想知道您是否可以提供幫助。當我按下按鈕時,我只想看到“點擊”。我的班級MouseInput實現(xiàn)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個贊
首先,您可以使用內聯(lián)鼠標適配器,而不是擴展鼠標偵聽器并且需要單獨的鼠標代碼文件。
其次,如果您想觀察按鈕的點擊,請將偵聽器添加到您的按鈕。
yourJButton.addMouseListener( new MouseAdapter()
{
@Override
public void mouseClicked( MouseEvent e )
{
{
//do stuff
}
}
});
添加回答
舉報
0/150
提交
取消