簡體   English   中英

從JComboBox事件在JFrame中顯示圖像

[英]Display Images in JFrame from JComboBox event

我想實現以下功能:

例如:

  • 當用戶從JComboBox中選擇“Profile Pic”項時,“Profile Pic”文件夾中的相關圖像應該加載到同一幀上。
  • 再次當用戶選擇“Product Img”時,應該加載來自“Product Img”文件夾的相關圖像,替換之前的圖像。

以下是代碼段,請提出任何更改建議

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;
    }
}

這應該在選擇“Profile Pic”項目時顯示此圖像應在選擇“Product Img”項目時顯示

我想請你再看看我發布的代碼(你似乎正在使用) 從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();
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM