Java swing的图片浏览器,怎样实现点击了next按钮后JLabel显示下一张图片??求大大们给个思路,有代码最
展开全部
给你一个我们当时的作业:
//源文件名: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();
}
}
//源文件名: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();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询