gpt4 book ai didi

java - 在java中删除选定的图像

转载 作者:行者123 更新时间:2023-11-30 04:18:28 25 4
gpt4 key购买 nike

我正在处理图像。

我创建了两个按钮addButtonremoveButton

addButton 使用附加到 JLabel 的 JFileChooser 添加图像

和removeButton删除图像

我使用 addButton 添加了 4 个图像

现在我想单击图像,但当我按“删除”按钮时,它没有被删除

我是 Swing 新手,请帮助我

谢谢

编辑:我的代码

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.filechooser.FileNameExtensionFilter;

public class ImageCreation extends JFrame
{
JButton browseBtn=new JButton("Browse");
JButton removeBtn=new JButton("Remove");
String filename;
BufferedImage img;
JLabel imgLbl;
private volatile JLabel lastFocused;

public ImageCreation()
{
setSize(500,500);
setVisible(true);
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(browseBtn);
add(removeBtn);

browseBtn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
JFileChooser chooser = new JFileChooser();
chooser.setMultiSelectionEnabled(true);
chooser.addChoosableFileFilter(new
FileNameExtensionFilter("Images", "jpg", "png", "gif", "bmp"));
chooser.setAcceptAllFileFilterUsed(false);
chooser.setCurrentDirectory(new
File(System.getProperty("user.home")));
int option = chooser.showOpenDialog(ImageCreation.this);
if(option == JFileChooser.APPROVE_OPTION) {
filename=chooser.getSelectedFile().toString();

try {
img = ImageIO.read(new File(filename));
imgLbl = new JLabel();
imgLbl.setIcon(new ImageIcon(img));
imgLbl.setBounds(150,50,img.getWidth(),img.getHeight());
add(imgLbl);
imgLbl.addFocusListener(new FocusAdapter()
{
public void focusGained(FocusEvent e)
{
if (e.getComponent() instanceof JLabel)
lastFocused = (JLabel) e.getComponent();
}
});

imgLbl.repaint();

} catch (IOException e) { }

}
}
});

removeBtn.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
if(lastFocused==null)
JOptionPane.showMessageDialog(null,"You Must Select an Label to remove it");
if (lastFocused != null)
{
Container scollPane = lastFocused;
System.out.println(scollPane);
Container parent = scollPane.getParent();
System.out.println(parent);
parent.remove(scollPane);
}
}
});

}

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

最佳答案

JLabel 没有键盘焦点,只有鼠标。

我没有更改您的代码,只是使其正常工作:

imgLbl.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (e.getComponent() instanceof JLabel)
lastFocused = (JLabel) e.getComponent();
}
});

删除后必须重新绘制,因此只需在此代码后添加 repaint() 即可:

    if (lastFocused != null) {
Container scollPane = lastFocused;
System.out.println(scollPane);
Container parent = scollPane.getParent();
System.out.println(parent);
parent.remove(scollPane);
}
repaint(); // repainting after removal

请复制并粘贴以下代码。应该可以工作

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.filechooser.FileNameExtensionFilter;

public class ImageCreation extends JFrame
{
JButton browseBtn=new JButton("Browse");
JButton removeBtn=new JButton("Remove");
String filename;
BufferedImage img;
JLabel imgLbl;
private volatile JLabel lastFocused;

public ImageCreation()
{
setSize(500,500);
setVisible(true);
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(browseBtn);
add(removeBtn);

browseBtn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
JFileChooser chooser = new JFileChooser();
chooser.setMultiSelectionEnabled(true);
chooser.addChoosableFileFilter(new
FileNameExtensionFilter("Images", "jpg", "png", "gif", "bmp"));
chooser.setAcceptAllFileFilterUsed(false);
chooser.setCurrentDirectory(new
File(System.getProperty("user.home")));
int option = chooser.showOpenDialog(ImageCreation.this);
if(option == JFileChooser.APPROVE_OPTION) {
filename=chooser.getSelectedFile().toString();

try {
img = ImageIO.read(new File(filename));
imgLbl = new JLabel();
imgLbl.setIcon(new ImageIcon(img));
imgLbl.setBounds(150,50,img.getWidth(),img.getHeight());
add(imgLbl);
imgLbl.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (e.getComponent() instanceof JLabel)
lastFocused = (JLabel) e.getComponent();
}
});

imgLbl.repaint();

} catch (IOException e) { }

}
}
});

removeBtn.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
if(lastFocused==null)
JOptionPane.showMessageDialog(null,"You Must Select an Label to remove it");
if (lastFocused != null)
{
Container scollPane = lastFocused;
System.out.println(scollPane);
Container parent = scollPane.getParent();
System.out.println(parent);
parent.remove(scollPane);
}
repaint();
}
});

}

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

关于java - 在java中删除选定的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17745981/

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