0

我创建了一个JFrame. 并设置如下代码所示的布局

    this.setLayout(new GridLayout());
    JPanel panel=new JPanel();
    panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
    panel.add(new PicturePanel1());
    JTabbedPane jtp=new JTabbedPane();
    jtp.addTab("show Images", panel);
    jtp.addTab("Compare Image", new JButton());
    this.add(jtp);

我创建了另一个从特定位置绘制图像的类。

protected void paintComponent(Graphics g) {
   super.paintComponents(g);
   Graphics2D g2 = (Graphics2D) g;
   int k = 20,y=10;
   for (int j = 0; j <= listOfFiles.length - 1; j++) {
       g2.drawImage(b[j], k, y, 100, 100, null);
      // add(new javax.swing.JCheckBox());
       k = k + 120;
       if(k>=960)
       {
           k=20;
           y=y+120;
       }
       }

它工作正常。当用户单击特定图像时,我想删除图像。

这是我的输出窗口。我想从列表中删除图像。在此处输入图像描述

4

2 回答 2

1

这是我做的一个例子(厌倦了学习:))。向您展示示例绝不是最好的快速操作,单击要删除的图像,然后按删除按钮或DEL从面板中删除图像:

当应用程序启动时(橙色只显示悬停的焦点标签):

在此处输入图像描述

通过单击标签选择要删除的图像(边框变为绿色突出显示并且将保持这种方式直到单击另一个标签):

在此处输入图像描述

按下删除按钮或键后:

在此处输入图像描述

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.net.URL;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import javax.swing.AbstractAction;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.border.LineBorder;

public class JavaApplication5 {

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

    public static void createAndShowJFrame() {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {

                JFrame frame = createJFrame();
                frame.setVisible(true);

            }
        });
    }

    private static JFrame createJFrame() {
        JFrame frame = new JFrame();
        //frame.setResizable(false);//make it un-resizeable
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("Test");

        ArrayList<BufferedImage> images = null;

        try {
            images = getImagesArrayList();
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        final ImageViewPanel imageViewPanel = new ImageViewPanel(images);
        JScrollPane jsp = new JScrollPane(imageViewPanel);
        jsp.setPreferredSize(new Dimension(400, 400));
        frame.add(jsp);

        JPanel controlPanel = new JPanel();
        JButton addLabelButton = new JButton("Delete Selected Image");
        addLabelButton.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                imageViewPanel.removeFocusedImageLabel();
            }
        });
        controlPanel.add(addLabelButton);
        frame.add(controlPanel, BorderLayout.SOUTH);
        frame.pack();

        return frame;
    }

    private static ArrayList<BufferedImage> getImagesArrayList() throws Exception {
        ArrayList<BufferedImage> images = new ArrayList<>();
        images.add(resize(ImageIO.read(new URL("http://icons.iconarchive.com/icons/deleket/sleek-xp-software/256/Yahoo-Messenger-icon.png")), 100, 100));
        images.add(resize(ImageIO.read(new URL("https://upload.wikimedia.org/wikipedia/commons/6/64/Gnu_meditate_levitate.png")), 100, 100));
        return images;
    }

    public static BufferedImage resize(BufferedImage image, int width, int height) {
        BufferedImage bi = new BufferedImage(width, height, BufferedImage.TRANSLUCENT);
        Graphics2D g2d = (Graphics2D) bi.createGraphics();
        g2d.addRenderingHints(new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY));
        g2d.drawImage(image, 0, 0, width, height, null);
        g2d.dispose();
        return bi;
    }
}

class ImageViewPanel extends JPanel {

    JLabel NO_IMAGES = new JLabel("No Images");
    ArrayList<BufferedImage> images;
    ArrayList<MyLabel> imageLabels;

    public ImageViewPanel(ArrayList<BufferedImage> images) {

        this.images = images;
        imageLabels = new ArrayList<>();

        for (BufferedImage bi : images) {
            imageLabels.add(new MyLabel(new ImageIcon(bi), this));
        }
        layoutLabels();

        getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0, true), "Delete pressed");
        getActionMap().put("Delete pressed", new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                removeFocusedImageLabel();
            }
        });
    }

    void removeFocusedImageLabel() {
        if (focusedLabel == null) {
            return;
        }
        imageLabels.remove(focusedLabel);
        remove(focusedLabel);
        layoutLabels();
    }

    private void layoutLabels() {
        if (imageLabels.isEmpty()) {
            add(NO_IMAGES);
        } else {
            remove(NO_IMAGES);
            for (JLabel il : imageLabels) {
                add(il);
            }
        }
        revalidate();
        repaint();
    }
    private MyLabel focusedLabel;

    void setFocusedLabel(MyLabel labelToFocus) {
        if (focusedLabel != null) {
            focusedLabel.setBorder(null);
            focusedLabel.setClicked(false);
        }
        focusedLabel = labelToFocus;

        focusedLabel.setBorder(new LineBorder(Color.GREEN));
    }
}

class MyLabel extends JLabel {

    private final ImageViewPanel imageViewPanel;
    private boolean clicked = false;

    public MyLabel(Icon image, ImageViewPanel imageViewPanel) {
        super(image);
        this.imageViewPanel = imageViewPanel;
        initLabel();
    }

    public MyLabel(String text, ImageViewPanel imageViewPanel) {
        super(text);
        this.imageViewPanel = imageViewPanel;
        initLabel();
    }

    private void initLabel() {
        setFocusable(true);
        addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                clicked = true;
                imageViewPanel.setFocusedLabel(MyLabel.this);
            }

            @Override
            public void mouseEntered(MouseEvent me) {
                super.mouseEntered(me);
                if (clicked) {
                    return;
                }
                setBorder(new LineBorder(Color.ORANGE));
                //call for focus mouse is over this component
                requestFocusInWindow();
            }
        });

        addFocusListener(new FocusAdapter() {
            @Override
            public void focusLost(FocusEvent e) {
                super.focusLost(e);

                if (clicked) {
                    return;
                }

                setBorder(null);
            }
        });
    }

    public void setClicked(boolean clicked) {
        this.clicked = clicked;
    }

    public boolean isClicked() {
        return clicked;
    }
}
于 2013-06-26T13:59:48.047 回答
0

只需找到图像的位置(x 和 y)和大小(宽度和高度),然后使用 Graphics 对象(g2)在绘制的图像上填充一个矩形,如下所示

g2.setColor(this.getBackground());//set the color you want to clear the bound this point to JFrame/JPanel
g2.fillRect(x, y, width, height);

其中x和y是图片所在的位置,宽高是图片的大小

于 2013-06-26T11:46:39.040 回答