给你一个我们当时的作业:
//源文件名:album.java
//在下载源程序中的文件夹:0709相册
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class album extends Applet implements ActionListener
{
Image img[];
int j=0;
String f;
Label a1=new Label("文件名:");
Label a2=new Label(" ");
Button btn1,btn2;
public void init()
{
img = new Image[22];
setLayout(null);
setBackground(Color.cyan);
add(a1);
add(a2);
btn1= new Button("下一页");
btn2= new Button("上一页");
add(btn1);
add(btn2);
a1.setBounds(650,60,60,30);
a2.setBounds(650,80,110,60);
a2.setBounds(650,80,110,60);
a1.setBackground(Color.cyan);
a2.setBackground(Color.cyan);
a2.setForeground(Color.red);
Font ft = new Font("Times New Romon",1,20);
a2.setFont(ft);
btn1.setBounds(650,180,60,30);
btn2.setBounds(650,240,60,30);
btn1.addActionListener(this);
btn2.addActionListener(this);
for (int i=0;i<22;i++)
{
f="pic"+Integer.toString(i)+".jpg";
img[i]=getImage(getCodeBase(),f);
}
}
public void paint (Graphics g)
{
f="pic"+Integer.toString(j)+".jpg";
a2.setText(f);
int w=img[j].getWidth(this);
int h=img[j].getHeight(this);
g.drawImage(img[j],0,0,w,h,this);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==btn1)
{
j++;
if (j>21) j=0;
}
if(e.getSource()==btn2)
{
j--;
if(j<0) j=21;
}
repaint();
}
}