2011-12-25 62 views
2

我正在使用java的windows版本的桌面應用程序。在我的應用程序中,需要使用下一個和上一個按鈕來顯示Path中的圖像。在Java中的顯示圖像

對於我寫了一個類返回圖像的所有路徑:我使用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 
     { 

     } 

    } 
+5

請確切地提及您在顯示圖像時遇到的問題。 – Lion 2011-12-25 01:20:27

+0

爲了儘快提供更好的幫助,請發佈[SSCCE](http://sscce.org/)。 – 2011-12-25 01:40:03

+0

我不明白你的問題是什麼。但仍然可能[這會幫助你。](http://www.onlinexamples.com/showfullexample.action?idexamples=81&title=Display%20Image%20In%20Jlabel%20Within%20A%20Jpanel) – 2011-12-27 09:28:13

回答

3

使用適當的佈局管理器。在這種情況下,請使用CardLayout。這將使圖像交換更容易。除非圖像數量非常大,否則我強烈建議採用這種方法。

+0

是的,但我可以完成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())); } 其他 { } } – 2011-12-25 01:22:39

+0

利用前述的佈局管理器,垃圾邏輯將不再需要。你真的需要努力封裝你的邏輯。 – mre 2011-12-25 01:23:46

+0

你能幫我嗎? – 2011-12-25 01:32:46

3

使用您的List<String>構建相應的List<ImageIcon>並根據需要替換標籤的圖標。在此example中,JComboBox保存當前選擇,並且按鈕相應地更改選擇。請注意,索引環繞,形成一個循環隊列。