java中如何打开图像并显示,在Java中显示图像

I am working on a desktop application for windows version using java. In my application there is a requirement to display IMAGES from Path with a next and previous button.

For that I wrote a class to return all the paths of the images : I use ArrayList

import java.io.File;

import java.util.ArrayList;

public class RE {

private ArrayList c =new ArrayList();

public RE (String rep)

{

File src=new File(rep);

if(src!=null && src.exists() && src.isDirectory())

{

String[] tab=src.list();

if(tab!=null)

{

for(String s:tab)

{

File srcc=new File(rep+File.separatorChar+s);

if(srcc.isFile())

{

if(srcc.getName().matches(".*"+"png$")|| srcc.getName().matches(".*"+"jpg$") || srcc.getName().matches(".*"+"gif$"))

c.add(srcc.getPath());

}

}

}

}

}

public ArrayList getAll()

{

return c;

}

}

and a class to display images, but I have some problems in ActionPerformed

import java.awt.BorderLayout;

import java.awt.FlowLayout;

import java.awt.Image;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.util.ListIterator;

import javax.swing.*;

public class swing extends JFrame implements ActionListener{

private RE c=new RE("H:\\photos\\g");

JTextField chemin=new JTextField(30);

JLabel lab;ImageIcon imageIcon;

JButton next =new JButton("NEXT");

JButton prev=new JButton("prev");

JPanel pan1=new JPanel();

JPanel pan2=new JPanel();

JPanel pan3=new JPanel();

swing()

{

imageIcon = new ImageIcon(c.getAll().get(2));

lab = new JLabel(imageIcon);

this.setLayout(new BorderLayout());

this.setVisible(true);

pan1.setLayout(new FlowLayout());

pan1.add(new JLabel("ENTREZ LE CHEMIN DE REPERTOIRE :"));

pan1.add(chemin);

pan2.setLayout(new FlowLayout());

pan2.add(lab);

pan3.setLayout(new FlowLayout());

next.addActionListener(this);

prev.addActionListener(this);

pan3.add(prev); pan3.add(next);

this.add(pan1,BorderLayout.NORTH);

this.add(pan2,BorderLayout.CENTER);

this.add(pan3,BorderLayout.SOUTH);

this.pack();

}

public static void main(String[] args){

new swing();

}

@Override

public void actionPerformed(ActionEvent e) {

if(e.getSource()==next)

{

String cur=imageIcon.toString();

ListIterator l=c.getAll().listIterator(c.getAll().indexOf(cur));

lab.setIcon(new ImageIcon(l.previous().toString()));

}

else

{

}

}

}

but I can't complete that :

public void actionPerformed(ActionEvent e) {

if(e.getSource()==next)

{

String cur=imageIcon.toString();

ListIterator l=c.getAll().listIterator(c.getAll().indexOf(cur));

lab.setIcon(new ImageIcon(l.previous().toString()));

}

else

{

}

}

解决方案

Use your List to construct a corresponding List and replace the label's icon as required. In this example, a JComboBox holds the current selection, and the buttons change the selection accordingly. Note that the indexes wrap around, forming a circular queue.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值