2011-12-25 30 views
2

私は、Javaを使用してWindows版のデスクトップアプリケーションで作業しています。私のアプリケーションでは、次と前のボタンでPathからIMAGESを表示する必要があります。そのために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

+0

すぐに役立つように、[SSCCE](http://sscce.org/)を投稿してください。 –

+0

私はあなたの問題が何かを得ることはありません。しかし、それでもなお[これはあなたを助けます。](http://www.onlinexamples.com/showfullexample.action?idexamples=81&title=Display%20Image%20In%20Jlabel%20Within%20A%20Jpanel) –

答えて

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())); }他{ }} –

+0

ごみロジックが必要ではないことを、前述のレイアウトマネージャーを使用して。あなたは本当にロジックをカプセル化することに取り組む必要があります。 – mre

+0

あなたは私を助けることができますか? –

3

List<String>を使用して対応するList<ImageIcon>を作成し、必要に応じてラベルのアイコンを置き換えます。このexampleでは、JComboBoxが現在の選択を保持し、ボタンはそれに応じて選択を変更します。インデックスがラップアラウンドし、循環キューが形成されます。