Re: [問題] 關於actionperformed裡寫程式繪圖

看板java作者 (愚者)時間19年前 (2006/10/23 09:24), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串12/12 (看更多)
※ 引述《hubin (阿賓￾  NN)》之銘言: : ※ 引述《neigence (心夜)》之銘言: : : 為什麼不把paint拉出去外面寫呢? 程式呼叫repaint()時 自已就會去找paint這個 : : method了 : : public class ???? extends JFrame{ : : public itemListener implements Actionlistener{ : : public void actionPerformed(ActionEvent event){ : : repaint(); : : } : : } : : public void paint(Graphics g){ : : // do something : : } : : } : 我照了你的方式去寫: : public class itemHandler implements ActionListener{ : public void actionPerformed(ActionEvent event){ : repaint(); //這行出現錯誤訊息 : } : public void paint(Graphics g){ : g.drawLine(Painter.x1,Painter.y1,Painter.x2,Painter.y2); : } : } : 訊息為:The method repaint() is undefined for the type itemHandler : 感覺它似乎沒有跑去找paint這個method…… repaint()是某個你想要畫的Component的method 但是你這個handler並沒有直接取得此Component的方法。 private Component myDrawTarget; Component void setDrawTaget(Component c) method 然後寫自己handle內的repaint private void repaint(){ myDrawTarget.repaint(); ............ } 當然如果你的target不需要改變,也可以透過建構式傳入方式 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 163.26.34.213
文章代碼(AID): #15F1dZ2e (java)
討論串 (同標題文章)
文章代碼(AID): #15F1dZ2e (java)