如何通过右键单击鼠标删除 JLabel 中的图像

How do I delete an image inside a JLabel by right clicking my mouse

我正在做一个动物项目,我想用 MouseListener 函数改进这个项目,但我找不到如何做这个特定的部分,所以我到处找了找。这是我的代码,因此您可以很好地了解我在做什么。

主要Class

public class Animals {

public static void main(String[] args) {
    JFrame application = new JFrame("Animal Project");

    GUI graphicalInterface = new GUI();
    application.add(graphicalInterface);

    application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    application.setLocation(200, 200);
    application.pack();
    application.setVisible(true);
    application.setResizable(false);
}

子Class

public class GUI extends JPanel implements ActionListener {

private JButton animalOption = new JButton();
private JButton save = new JButton();
private JButton load = new JButton();
private JButton clear = new JButton();
private JPanel buttonPanel;
private JPanel imagePanel;
private ImageIcon bear;
private ImageIcon tiger;
private ImageIcon lion;
private JLabel imageBlock1;
private JLabel imageBlock2;
private JLabel imageBlock3;
private int choice;
private int count = 1;
private JLabel currImageBlock = null;

GUI() {
    Border blackline = BorderFactory.createLineBorder(Color.black);

    //create button panel
    buttonPanel = new JPanel();
    buttonPanel.setPreferredSize(new Dimension(100, 230));
    buttonPanel.setOpaque(true);
    buttonPanel.setBackground(Color.white);
    buttonPanel.setBorder(blackline);
    imagePanel = new JPanel(new GridLayout(3, 3));
    imagePanel.setPreferredSize(new Dimension(500, 500));
    imagePanel.setOpaque(true);
    imagePanel.setBackground(Color.white);
    imagePanel.setBorder(blackline);
    imageBlock1 = new JLabel();
    imageBlock1.setPreferredSize(new Dimension(100, 100));
    imageBlock1.setOpaque(true);
    imageBlock1.setBackground(Color.white);
    imageBlock1.setBorder(blackline);
    imageBlock2 = new JLabel();
    imageBlock2.setPreferredSize(new Dimension(100, 100));
    imageBlock2.setOpaque(true);
    imageBlock2.setBackground(Color.white);
    imageBlock2.setBorder(blackline);
    imageBlock3 = new JLabel();
    imageBlock3.setPreferredSize(new Dimension(100, 100));
    imageBlock3.setOpaque(true);
    imageBlock3.setBackground(Color.white);
    imageBlock3.setBorder(blackline);

    bear = new ImageIcon("Bear.png");
    tiger = new ImageIcon("Tiger.png");
    lion = new ImageIcon("Lion.png");

    animalOption = new JButton();
    //add action listener to each button
    animalOption.addActionListener(this);
    //set button size
    animalOption.setPreferredSize(new Dimension(100, 50));
    //set text for each button
    animalOption.setText("Animal");
    animalOption.setToolTipText("press to select your animal");
    //add buttons to gui
    buttonPanel.add(animalOption);

    save = new JButton();
    //add action listener to each button
    save.addActionListener(this);
    //set button size
    save.setPreferredSize(new Dimension(100, 50));
    //set text for each button
    save.setText("Save");
    save.setToolTipText("press to save your selection");
    //add buttons to gui
    buttonPanel.add(save);

    load = new JButton();
    //add action listener to each button
    load.addActionListener(this);
    //set button size
    load.setPreferredSize(new Dimension(100, 50));
    //set text for each button
    load.setText("Load");
    load.setToolTipText("press to load your selection");
    //add buttons to gui
    buttonPanel.add(load);

    clear = new JButton();
    //add action listener to each button
    clear.addActionListener(this);
    //set button size
    clear.setPreferredSize(new Dimension(100, 50));
    //set text for each button
    clear.setText("Clear");
    clear.setToolTipText("press to clear your selection");
    //add buttons to gui
    buttonPanel.add(clear);

    this.add(buttonPanel);
    this.add(imagePanel);
    imagePanel.add(imageBlock1);
    imagePanel.add(imageBlock2);
    imagePanel.add(imageBlock3);

}

public void actionPerformed(ActionEvent e) {

    if (count == 1) {
        currImageBlock = imageBlock1;
    } else if (count == 2) {
        currImageBlock = imageBlock2;
    } else if (count == 3) {
        currImageBlock = imageBlock3;
    } else if (count > 3 && e.getSource().equals(animalOption)) {
        JOptionPane.showMessageDialog(imagePanel, "Your choices have exceeded, please press clear.");
    }

    if (e.getSource().equals(animalOption) && count < 4) {
        choice = selectAnimal();
        if (choice == 1) {
            currImageBlock.setIcon(bear);
            currImageBlock.setToolTipText("This is a bear");
        } else if (choice == 2) {
            currImageBlock.setIcon(tiger);
            currImageBlock.setToolTipText("This is a tiger");
        } else if (choice == 3) {
            currImageBlock.setIcon(lion);
            currImageBlock.setToolTipText("This is a lion");
        }
        chooseNumber();
        count++;
    }

    if (e.getSource().equals(clear)) {
        imageBlock1.setIcon(null);
        imageBlock2.setIcon(null);
        imageBlock3.setIcon(null);
        imageBlock1.revalidate();
        imageBlock2.revalidate();
        imageBlock3.revalidate();
        count = 1;
    }
}

static int selectAnimal() {

    int animal = 0;
    String theAnimal = JOptionPane.showInputDialog("Please enter animal, type 1 for bear, type 2 for tiger, type 3 for lion");
    animal = Integer.parseInt(theAnimal);

    return animal;

}

这就是我 运行 代码和选择我想要的动物后的样子

我有一个清除所有按钮,如果我单击它,它会清除 imageBlock Jlabel 中的所有图像,但是我想添加一个功能,如果我右键单击特定的 JLabel,图像及其所有内容将在该特定 JLabel 中删除。任何帮助将不胜感激。

您可以将鼠标侦听器设置为在单击鼠标右键时将其移出框架。或者您可以设置一个布尔值,如果单击鼠标设置为真,并且仅在布尔值为真时才显示该对象,因此您从文件中设置图像的位置仅 运行 如果未单击鼠标右键,则该代码

此示例代码显示了如何使用鼠标侦听器对 JLabel 中设置为图标的图像执行操作(在本例中为删除)。请注意,代码中使用了 MouseAdapter,但可以实现类似结果的 MouseListener 接口。

执行此功能的其他方式:

  • Select 图像标签并单击按钮将其删除。
  • 使用删除图像菜单选项打开上下文或弹出菜单 - 当右键单击图像标签时。

示例代码:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ImageLabelAction {
    private JLabel imageBlock;
    private ImageIcon koala = new ImageIcon("koala.jpg");
    public static void main(String [] args) {
        new ImageLabelAction().gui();
    }
    private void gui() {
        JFrame frame = new JFrame();
        frame.setTitle("Frame with Image Label");
        imageBlock = new JLabel();
        imageBlock.setPreferredSize(new Dimension(100, 100));
        imageBlock.setOpaque(true);
        imageBlock.setBackground(Color.white);
        imageBlock.setBorder(BorderFactory.createLineBorder(Color.black));
        imageBlock.setIcon(koala);
        imageBlock.addMouseListener(new PictureRemoveListener());
        frame.add(imageBlock);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setSize(250, 250);        
        frame.setVisible(true);
    }
    private class PictureRemoveListener extends MouseAdapter {
        @Override public void mousePressed(MouseEvent e) {
            if (e.getButton() == MouseEvent.BUTTON3) {
                imageBlock.setIcon(null);
            }
        }
    }
}

类似于下面的伪代码:

imageBlock1.addMouseListener(new MouseAdapter() {
    public void mouseClicked (MouseEvent e) {
        // use flags to figure out if it is right mouse click
        imageBlock1.setIcon(null);
    }
});

对 imageBlock2、3、4 等执行此操作

已经有一段时间了,但按照这些思路可以满足您的要求。