gpt4 book ai didi

java - 在 JComboBox 事件中的 JFrame 中显示图像

转载 作者:行者123 更新时间:2023-12-02 06:55:55 24 4
gpt4 key购买 nike

我想实现以下功能:

例如:

  • 当用户从 JComboBox 中选择“个人资料图片”项时,“个人资料图片”文件夹中的相关图像应加载到同一帧上。
  • 再次,当用户选择“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;
}
}

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

最佳答案

我强烈建议您再看一下我发布的代码(您似乎正在使用)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();
}

关于java - 在 JComboBox 事件中的 JFrame 中显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17362306/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com