在java中删除选定的图像

时间:2013-07-19 12:22:04

标签: java image swing jbutton jlabel

我正在研究图像。

我创建了两个按钮 addButton removeButton

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

1 个答案:

答案 0 :(得分: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();
    }
}