我在程序中遇到了鼠標事件問題。我試圖用畫布編寫一個繪圖程序。如何在Java中按下鼠標按鈕時收聽鼠標移動事件
如果用戶左鍵單擊並移動鼠標,應繪製圖形。所以我在其中定義了Drawer
類,其中boolean allow_draw
,並且我添加了一個方法draw
。
draw
被稱爲在畫布mousemoved
事件和allow_draw
設置真假與mousepressed
和released
。
然而,mousemoved
不點火,而我按下鼠標按鈕...
我的問題是:我怎麼能聽鼠標動作,同時按下鼠標鍵。
希望你知道我在找什麼:)
我不清楚爲什麼您需要'allow_draw'標誌,但請注意,請確保您正確同步allow_draw。否則,您將遇到該變量貨幣的問題。 – RWVan3
謝謝,現在它工作! :) 我寫在我的主要和設計器類的構造函數 ' public CanvasTest(){ initComponents(); this.drw =新的抽屜(this); this.canvas.addMouseMotionListener(new MouseMotionListener(this)); this.canvas.addMouseListener(new MouseClickListener(this)); //this.canvas.addMouseMotionListener(new MouseMotion(this)); } ' – user1795687
抱歉,codehighlighting不會對我工作...但我做你:) – user1795687