1

我正在处理图像。

我创建了两个按钮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();
        }
    }
4

1 回答 1

2

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();
    }
}
于 2013-07-19T13:49:31.253 回答