【问题标题】:How to add Image to a JPanel in case of mouseReleased Event如何在 mouseReleased 事件的情况下将图像添加到 JPanel
【发布时间】:2013-10-05 05:11:26
【问题描述】:

我试图在 java 中构建一个基本程序,它创建一个带有 JPanel 的窗口,当用户单击 JPanel 时会显示一个图像,但是当运行应用程序并单击 JPanel 时,什么都没有显示...

这里是代码...

//driver.java

import javax.swing.JFrame;

public class driver {

    public static void main(String[] args) {        
        Gui obj = new Gui();
        obj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        obj.setSize(400, 400);
        obj.setVisible(true);   
    }
}

//GUI.java
import javax.swing.*;    
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class Gui extends JFrame{    

    public JPanel panel;
    public ImageIcon img;       

    public Gui(){       
        panel = new JPanel();       
        panel.setBackground(Color.DARK_GRAY);       
        img = new ImageIcon("cross.png");
        panel.addMouseListener(new MouseAdapter(){
                public void mouseReleased(MouseEvent e){
                    panel.add(new JLabel(img));
                    System.out.println("Mouse Click detected");
                }}
                );      
        add(panel);
    }       
}

//更新 Gui.java

import javax.swing.*;    
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

公共类 Gui 扩展 JFrame{

public JPanel panel;
public ImageIcon img;
public final JLabel label;

public Gui(){       
    panel = new JPanel();
    label = new JLabel();
    panel.add(label);   

    img = new ImageIcon(getClass().getResource("res/cross.png"));
    panel.addMouseListener(new MouseAdapter(){
            public void mouseReleased(MouseEvent e){
                label.setIcon(img);
                System.out.println("Mouse Click detected");
            }}
            );
    add(panel);
}   

}

注意:这是我的项目的样子 organised

【问题讨论】:

  • System.out.println("Mouse Click detected");this 打印了吗?
  • 您是否尝试在添加标签后在面板上调用 revalidate...?
  • @porfiriopartida 是的,字符串已打印,但面板中未添加(或显示)img。
  • @MadProgrammer 我尝试在 JPanel 上使用 revalidate 但图像未显示在面板上..
  • @AdityaBhatnagar :图片不应该在.class 文件旁边,而不是在.java 文件旁边。请查看此answer,了解目录结构。此外,下面答案中建议的建议值得注意,因为最好只更改图标,而不是放置新的JLabel。有关详细信息,请参阅此线程,关于如何add Images to the Project

标签: java image swing jlabel mouselistener


【解决方案1】:

改变..

    // ..
    panel.addMouseListener(new MouseAdapter(){
            public void mouseReleased(MouseEvent e){
                panel.add(new JLabel(img));
                System.out.println("Mouse Click detected");
            }}
            );      

收件人(类似于 - 未经测试):

    // ..
    final JLabel label = new JLabel();
    panel.add(label);
    panel.addMouseListener(new MouseAdapter(){
            public void mouseReleased(MouseEvent e){
                label.setIcon(img);
                System.out.println("Mouse Click detected");
            }}
            );      

这是 ImageViewer 中使用的基本技术,尽管它会在 Swing Timer 上更改图像,而不是鼠标单击。


当然,使用JButton 图标比使用JLabel/MouseListener 更容易。 JButton 不需要任何监听器来更改图标,并且适用于鼠标和键盘活动。例如。如this answer 所示。


img = new ImageIcon("cross.png");

到部署时,这些资源可能会变成

在这种情况下,资源必须由URL 而不是File 访问。查看标签的info page,了解形成URL的方法。

【讨论】:

  • 感谢您用这么好的示例进行解释,但即使在标签对象上调用 setIcon 方法也不会在 JPanel 上显示图标...
  • 将新代码显示为edit to the question。请发布SSCCE。要制作图像示例“SC”(自包含)的 SSCCE,有必要执行一些操作,例如在运行时生成图像(如我链接到的第一个 SSCCE 中的合成图像)或从运行时的 URL(与第二个链接的 SSCCE 一样)。
  • 我已经发布了更新的代码并且这次使用了 URL 而不是文件,现在我在 img = new ImageIcon(getClass().getResource("res/cross.png "));
  • new ImageIcon(getClass().getResource("res/cross.png")) 1) 最好是new ImageIcon(getClass().getResource("/res/cross.png"))(前导/)。 2)它适用于合成或热链接图像吗?如果是这样,这真的是一个“资源在哪里?”问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-09-07
  • 2010-09-22
  • 2012-07-12
  • 2018-09-02
相关资源
最近更新 更多