作者hubin (阿宾  NN)
看板java
标题Re: [问题] 关於actionperformed里写程式绘图
时间Mon Oct 23 00:39:40 2006
※ 引述《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