有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

在java中删除所选图像

我正在制作图像

我创建了两个按钮addButtonremoveButton

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

和removeButton以删除图像

我使用addButton添加了4个图像

现在我想点击一个图像,当我按下移除按钮时,它不会被移除

我刚开始荡秋千,请帮帮我

多谢各位

编辑:我的代码

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) 个答案

  1. # 1 楼答案

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