Re: [問題] 關於actionperformed裡寫程式繪圖
※ 引述《neigence (心夜)》之銘言:
: ※ 引述《hubin (阿賓  NN)》之銘言:
: : 我是要寫個簡易的繪圖程式
: : 就是按下某一個Button(例如是畫drawRect或fillOval),
: : 有設handler及actionListener
: : 然後跑到另一個程式的actionPerformed
: : 那怎麼再actionPerformed裡去寫繪圖的程式呀?我寫都會出錯
: : 以下是我的部份程式碼:
: : public class itemHandler implements ActionListener{
: : public void actionPerformed(ActionEvent event){
: : public void paint(Graphics g){ //這行會出錯
: : super.paint(g);
: : Graphics2D g2d = (Graphics2D)g;
: : g2d.drawLine(Painter.x1,Painter.y1,Painter.x2,Painter.y2);
: : }
: : repaint();
: : }
: : }
: : 上面的錯誤訊息為:Multiple markers at this line
: : -Syntax error on token "(",;expected
: : -Syntax error on token ")",;expected
: : 另外還有一個主程式,我是用那個主程式按下Button鍵跑來這個itemHandler
: : 主程式Painter沒有錯誤訊息。我真的想不到要怎麼改了~泣
: : 或是各位有什麼方法可以在actionPerformed裡寫繪圖的,使用者自己繪圖
: : 我上面的x1,y1,x2,y2是滑鼠按下、放開的座標值
: : 不知道有沒有表達的清礎~~
: : 希望大家幫幫忙~感恩,或者是~跟本就不能這樣寫……
: 為什麼不把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……
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.115.206.17
討論串 (同標題文章)
java 近期熱門文章
PTT數位生活區 即時熱門文章