0

我想实现以下功能:

例如:

  • 当用户从 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;
    }
}

这应该在选择时显示 此图像应在选择时显示

4

1 回答 1

2

我会敦促您再看看我发布的代码(您似乎正在使用)从 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();
    }
于 2013-06-28T10:47:37.137 回答