2013-06-28 183 views
0

我想實現以下功能:顯示圖片

如:

  • 當用戶選擇「用戶照片」項目從JComboBox中releted從「用戶照片」文件夾中加載圖像在同一幀。
  • 再次當用戶選擇「產品圖像」項目時,應該加載來自「產品圖像」文件夾的相關圖像,替換先前的圖像。

以下是代碼片段,請建議任何改變

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.Graphics2D; 
import java.awt.RenderingHints; 
import java.awt.event.ActionEvent; 
import java.awt.event.FocusAdapter; 
import java.awt.event.FocusEvent; 
import java.awt.event.KeyEvent; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 
import java.awt.image.BufferedImage; 
import java.net.URL; 
import java.util.ArrayList; 
import javax.imageio.ImageIO; 
import javax.swing.AbstractAction; 
import javax.swing.Icon; 
import javax.swing.ImageIcon; 
import javax.swing.JButton; 
import javax.swing.JComponent; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.swing.KeyStroke; 
import javax.swing.SwingUtilities; 
import javax.swing.border.LineBorder; 

public class NewClass1 { 

    public static void main(String[] args) { 
     createAndShowJFrame(); 
    } 

    public static void createAndShowJFrame() { 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 

       JFrame frame = createJFrame(); 
       frame.setVisible(true); 

      } 
     }); 
    } 

    private static JFrame createJFrame() { 
     JFrame frame = new JFrame(); 
     //frame.setResizable(false);//make it un-resizeable 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setTitle("Test"); 

     ArrayList<BufferedImage> images = null; 

     try { 
      images = getImagesArrayList(); 
     } catch (Exception ex) { 
      ex.printStackTrace(); 
     } 

     final ImageViewPanel imageViewPanel = new ImageViewPanel(images); 
     JScrollPane jsp = new JScrollPane(imageViewPanel); 
     jsp.setPreferredSize(new Dimension(400, 400)); 
     frame.add(jsp); 

     final javax.swing.JComboBox filter = new javax.swing.JComboBox<>(); 
     filter.addItem("All"); 
     filter.addItem("Profile Pic"); 
     filter.addItem("Company Logo"); 
     filter.addItem("Product Img"); 


     JPanel controlPanel = new JPanel(); 
     JButton addLabelButton = new JButton("Delete Selected Image"); 
     addLabelButton.addActionListener(new AbstractAction() { 
      @Override 
      public void actionPerformed(ActionEvent e) { 
       imageViewPanel.removeFocusedImageLabel(); 
      } 
     }); 
     JLabel label =new JLabel("Filter By :"); 
     filter.addActionListener(new AbstractAction() { 
      @Override 
      public void actionPerformed(ActionEvent e) { 

       String cat=(String) filter.getSelectedItem(); 

       createJFrame(cat); 
      } 
     }); 
     controlPanel.add(addLabelButton); 
     controlPanel.add(label); 
     controlPanel.add(filter); 
     frame.add(controlPanel, BorderLayout.NORTH); 
     frame.pack(); 



     return frame; 
    } 

    private static ArrayList<BufferedImage> getImagesArrayList(String cat) throws Exception { 
     System.out.println(cat); 

     ArrayList<BufferedImage> images = new ArrayList<>(); 
     if(cat.equals("Profile Pic")) 
     images.add(resize(ImageIO.read(new URL("http://192.168.1.25:8080/pic/ProfilePic/1.jpg")), 100, 100)); 
     else if(cat.equals("Product Img")) 
     { 
     images.add(resize(ImageIO.read(new URL("http://192.168.1.25:8080/pic/ProductImg/2.jpg")), 100, 100)); 

     } 
     return images; 
    } 

private static ArrayList<BufferedImage> getImagesArrayList() throws Exception { 
     ArrayList<BufferedImage> images = new ArrayList<>(); 
     images.add(resize(ImageIO.read(new URL("http://localhost:8080/pic/All/a.jpg")), 100, 100)); 
     images.add(resize(ImageIO.read(new URL("http://localhost:8080/pic/All/b.jpg")), 100, 100)); 
      return images; 
    } 
    public static BufferedImage resize(BufferedImage image, int width, int height) { 
     BufferedImage bi = new BufferedImage(width, height, BufferedImage.TRANSLUCENT); 
     Graphics2D g2d = (Graphics2D) bi.createGraphics(); 
     g2d.addRenderingHints(new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY)); 
     g2d.drawImage(image, 0, 0, width, height, null); 
     g2d.dispose(); 
     return bi; 
    } 
} 

This should be shown on selection of "Profile Pic" item This Image should be shown on selection of "Product Img" item

+1

有什麼問題? –

+0

@Vignesh Vino我想要輸出如上面的屏幕所示,但我沒有得到它。選擇「Product Img」項後,我的程序沒有響應。 –

+3

在您的[sscce](http://sscce.org/)中,通過_public_'URL'訪問發佈的圖片,如[here](http://stackoverflow.com/a/10862262/230513)所示。如圖所示使用合成圖像(http://stackoverflow.com/a/15982915/230513);或使用'UIManager'圖標,如[此處]所示(http://stackoverflow.com/a/12228640/230513)。另請考慮['JTable'](http://docs.oracle.com/javase/tutorial/uiswing/components/table.html)使用自定義的「比較器」進行排序/過濾。 – trashgod

回答

2

我促請你再看看我貼的代碼(你似乎可以用) Deleting images from JFrame

但是:

在你的代碼中,我看到:

filter.addActionListener(new AbstractAction() { 
      @Override 
      public void actionPerformed(ActionEvent e) { 

       String cat=(String) filter.getSelectedItem(); 

       createJFrame(cat); 
      } 
     }); 

我甚至無法找到方法createJFrame(String cat);?

至於我看你應該這樣做:

filter.addActionListener(new AbstractAction() { 

    @Override 
    public void actionPerformed(ActionEvent e) { 

     String cat=(String) filter.getSelectedItem(); 

     ArrayList<BufferedImage> images=getImagesArrayList(cat);//get the new images for the selected item in combo 

     //refresh the layout by removing old pics and itertating the new array and adding pics to the panel as you iterate 
     layoutLabels(images); 
    } 

}); 

    .... 

private JLabel NO_IMAGES=new JLabel("No Images"); 

private void layoutLabels(ArrayList<BufferedImage> images) { 
     removeAll();//remove all components from our panel (the panel should only have the images on if not use setActionCommand("Image") on your images/JLabels and than use getComponents of JPanel and iterate through them looking for getActionCommand.equals("Image") 

     if (images.isEmpty()) {//if the list is empty 
      add(NO_IMAGES);//add Jlabel to show message of no images 
     } else { 
      remove(NO_IMAGES); 
      for (BufferedImage i : images) {//iterate through ArrayList of images 
       add(new JLabel(new ImageIcon(i)));//add each to the panel using JLabel as container for image 
      } 
     } 

     revalidate(); 
     repaint(); 
    } 
+0

它的工作......! –

+0

@Sam很高興能有所幫助。請閱讀[如何接受答案的工作?](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) –

+0

當我默認運行該程序時顯示對選定的圖像的重點,但是當我從組合中選擇我沒有得到重點?? –