在Java中显示图像 [英] display image in Java
问题描述
我用java工作的一个桌面应用程序的Windows版本。在我的应用程序有一个下一个和previous按钮来显示路径图像的要求。
有关,我写了一个类返回图像的所有路径:我使用ArrayList
进口的java.io.File;
进口的java.util.ArrayList;
公共类RE {
私人的ArrayList<串GT; C =新的ArrayList<串GT;();
公共RE(字符串REP)
{
文件SRC =新的文件(REP);
如果(SRC =空&放大器;!&放大器; src.exists()及&放大器; src.isDirectory())
{
的String []选项卡= src.list();
如果(标签!= NULL)
{
对于(一个String:选项卡)
{
文件SRCC =新的文件(REP +即File.separatorChar + S);
如果(srcc.isFile())
{
如果(srcc.getName()。匹配(*+PNG $)|| srcc.getName()。匹配(*+JPG $)|| srcc.getName()。匹配( 。*+的gif $))
c.add(srcc.getPath());
} }
}
}
}公众的ArrayList<串GT;得到所有()
{
返回℃;
}
}
和一个类来显示图像,但我在的actionPerformed一些问题
进口java.awt.BorderLayout中;
进口java.awt.FlowLayout中;
进口java.awt.Image中;
进口java.awt.event.ActionEvent中;
进口java.awt.event.ActionListener;
进口java.util.ListIterator中;进口的javax.swing *。
公共类摆幅的JFrame实现的ActionListener {
私人RE C =新的RE(H:\\\\ \\\\的照片G);
JTextField的舍曼=新的JTextField(30);
JLabel的实验室; ImageIcon的ImageIcon的;
接下来的JButton =的新的JButton(NEXT);
JButton的preV =的新的JButton(preV);
JPanel的PAN1 =新JPanel();
JPanel的pan2 =新JPanel();
JPanel的pan3 =新JPanel();
摇摆()
{
ImageIcon的=新的ImageIcon(c.getAll()获取(2));
实验室=新的JLabel(ImageIcon的);
this.setLayout(新的BorderLayout());
this.setVisible(真);
pan1.setLayout(新的FlowLayout());
pan1.add(新的JLabel(ENTREZ LE CHEMIN DE REPERTOIRE:));
pan1.add(舍曼); pan2.setLayout(新的FlowLayout());
pan2.add(实验室);
pan3.setLayout(新的FlowLayout());
next.addActionListener(本);
prev.addActionListener(本);
pan3.add(preV); pan3.add(下);
this.add(PAN1,BorderLayout.NORTH);
this.add(pan2,BorderLayout.CENTER);
this.add(pan3,BorderLayout.SOUTH);
this.pack();
}
公共静态无效的主要(字串[] args){
新摆幅();
}
@覆盖
公共无效的actionPerformed(ActionEvent的五){
如果(e.getSource()== NEXT)
{
串CUR = imageIcon.toString();
&的ListIterator LT;弦乐> L = c.getAll()的ListIterator(c.getAll()的indexOf(现)。)。
lab.setIcon(。新的ImageIcon(L previous()的toString()));
}
其他
{ } }}
但我不能完成该:
公共无效的actionPerformed(ActionEvent的五){
如果(e.getSource()== NEXT)
{
串CUR = imageIcon.toString();
&的ListIterator LT;弦乐> L = c.getAll()的ListIterator(c.getAll()的indexOf(现)。)。
lab.setIcon(。新的ImageIcon(L previous()的toString()));
}
其他
{ } }
使用您的列表<弦乐>
来构建相应的列表< ImageIcon的>
,并根据需要更换标签的图标。在此例如,一个的JComboBox
保持当前的选择,并且按钮更改相应的选择。注意,索引环绕,形成一个循环队列。
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<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;
}
}
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<String> 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<String> l=c.getAll().listIterator(c.getAll().indexOf(cur));
lab.setIcon(new ImageIcon(l.previous().toString()));
}
else
{
}
}
Use your List<String>
to construct a corresponding List<ImageIcon>
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.
这篇关于在Java中显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!