java 在applet中显示图像 这段代码有什么问题??我编译不过
importjava.applet.*;importjava.awt.event.*;importjava.awt.*;publicclassExample9_7exte...
import java.applet.*;
import java.awt.event.*;
import java.awt.*;
public class Example9_7 extends Applet {
int totalImage = 4;
int currentImage = 0;
Image[] images;
Button next_one, previous_one;
public void init() {
setLayout(new FlowLayout()); // 使用布局管理器
images = new Image[totalImage];
for (int i = 0; i < totalImage; i ++) {
images[i] = getImage( getDocumentBase(), "images" + (i+1) + ".gif" );
}
next_one = new Button( "下一张" );
previous_one = new Button( "前一张" );
next_one.addActionListener(this);
previous_one.addActionListener(this);//这两个有问题
add(next_one);
add(previous_one);
}
public void paint( Graphics g ) {
g.drawImage( images[currentImage], 50, 50, this );
}
public void actionPerformed( ActionEvent e ) {
if ( e.getSource() == next_one ) {
currentImage ++;
if ( currentImage > totalImage-1 ) {
currentImage = 0;
}
}
else if ( e.getSource() == previous_one ) {
currentImage --;
if ( currentImage < 0 ) {
currentImage = totalImage - 1;
}
}
repaint();
}
} 展开
import java.awt.event.*;
import java.awt.*;
public class Example9_7 extends Applet {
int totalImage = 4;
int currentImage = 0;
Image[] images;
Button next_one, previous_one;
public void init() {
setLayout(new FlowLayout()); // 使用布局管理器
images = new Image[totalImage];
for (int i = 0; i < totalImage; i ++) {
images[i] = getImage( getDocumentBase(), "images" + (i+1) + ".gif" );
}
next_one = new Button( "下一张" );
previous_one = new Button( "前一张" );
next_one.addActionListener(this);
previous_one.addActionListener(this);//这两个有问题
add(next_one);
add(previous_one);
}
public void paint( Graphics g ) {
g.drawImage( images[currentImage], 50, 50, this );
}
public void actionPerformed( ActionEvent e ) {
if ( e.getSource() == next_one ) {
currentImage ++;
if ( currentImage > totalImage-1 ) {
currentImage = 0;
}
}
else if ( e.getSource() == previous_one ) {
currentImage --;
if ( currentImage < 0 ) {
currentImage = totalImage - 1;
}
}
repaint();
}
} 展开
2个回答
展开全部
next_one.addActionListener(this);意思是给这个按钮注册一个监听者。括号里为监听者对象,这里设置为this,因此此类作为按钮的的监听者,这里注册的为动作事件,所以此Example9_7类应该实现ActionListener接口,并且实现接口里的方法public void actionPerformed(ActionEvent),你的类里面有这个方法,因此只需要实现接口就没有问题了。
开头改为public class Example9_7 extends Applet implements ActionListener
开头改为public class Example9_7 extends Applet implements ActionListener
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询