【问题标题】:display image in Java在 Java 中显示图像
【发布时间】:2012-01-27 12:06:44
【问题描述】:

我正在使用 java 开发 Windows 版本的桌面应用程序。在我的应用程序中,需要使用下一个和上一个按钮显示来自路径的图像。

为此,我编写了一个类来返回图像的所有路径:我使用 ArrayList

import java.io.File;
import java.util.ArrayList;


public class RE {
    private ArrayList<String> c =new ArrayList<String>();
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<String> getAll()
{
    return c;


}
}

还有一个显示图像的类,但是我在 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<String> l=c.getAll().listIterator(c.getAll().indexOf(cur));
        lab.setIcon(new ImageIcon(l.previous().toString()));
        }
        else
        {

        }

    }

}

但我无法完成:

public void actionPerformed(ActionEvent e) {
        if(e.getSource()==next)
        {
        String cur=imageIcon.toString();
        ListIterator<String> l=c.getAll().listIterator(c.getAll().indexOf(cur));
        lab.setIcon(new ImageIcon(l.previous().toString()));
        }
        else
        {

        }

    }

【问题讨论】:

  • 准确说明您在显示图像时遇到的问题。
  • 如需尽快获得更好的帮助,请发帖SSCCE
  • 我不明白你的问题是什么。但仍然可能是this will help you.

标签: java arrays image swing io


【解决方案1】:

使用您的List&lt;String&gt; 构造一个对应的List&lt;ImageIcon&gt; 并根据需要替换标签的图标。在这个example 中,JComboBox 保存当前选择,并且按钮相应地更改选择。请注意,索引会环绕,形成一个循环队列。

【讨论】:

    【解决方案2】:

    使用适当的布局管理器。在这种情况下,请使用CardLayout。这将使图像交换更容易。除非图像数量大得离谱,否则我强烈推荐这种方法。

    【讨论】:

    • 是的,但我可以完成 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())); } 其他 { } }
    • 通过使用上述布局管理器,垃圾逻辑将不再是必需的。你真的需要努力封装你的逻辑。
    • +1 我之前忽略了这将是多么容易,例如让每个CardPanel为自己的形象负责。
    猜你喜欢
    • 2012-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-08
    • 1970-01-01
    • 2017-08-10
    相关资源
    最近更新 更多