Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在java中删除所选图像_Java_Image_Swing_Jbutton_Jlabel - Fatal编程技术网

在java中删除所选图像

在java中删除所选图像,java,image,swing,jbutton,jlabel,Java,Image,Swing,Jbutton,Jlabel,我正在制作图像 我创建了两个按钮addButton和removeButton addButton使用附加到JLabel的JFileChooser添加图像 和removeButton以删除图像 我使用addButton添加了4个图像 现在我想点击一个图像,当我按下移除按钮时,它不会被移除 我刚开始荡秋千,请帮帮我 多谢各位 编辑:我的代码 import java.awt.Container; import java.awt.FlowLayout; import java.awt.event.Act

我正在制作图像

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

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

你的代码如何?张贴你的代码,是否有任何错误被抛出?考虑制作一个SSCCETE是我编辑和完成的代码可以在系统中运行,并请帮助添加/删除从<代码> jList(和删除所有的<代码> StimeSimult(..)< /代码>废话”。它对我无效。图像没有被删除。它显示了一个错误。“你必须选择一个标签才能删除它”即使我选择了它,我也认为它有效。但我使用了getContentPane()。repaint方法比repaint()方法有一点小的改变。但是你在这方面帮了我很多忙。我只是对它很生气