java - 当调用 JPanel 中的一个鼠标监听器时,它们都会被调用吗?

标签 java swing mouselistener

我有一个 Java GUI 代码,它在 JPanel 中使用多个 JLabel。每个单独的 JLabel 都有一个唯一的鼠标监听器,单击 JLabel 时会调用该监听器。由于某种原因,当单击一个 JLabel 时,它们都会被调用。

这是 JLabel:

car1 = new JLabel(card1); //card1 is just an image icon, no problems there
car2 = new JLabel(card2);
car3 = new JLabel(card3);

鼠标监听器:

car1.addMouseListener(new CardGUI("/cards/aceclubsS.gif", car1)); //Sends the path of the new card to be implemented as a JLabel as well as the current JLabel clicked.
car2.addMouseListener(new CardGUI("/cards/aceheartsS.gif", car2));
car3.addMouseListener(new CardGUI("/cards/acespadesS.gif", car3));

CardGUI 类:

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

public class CardGUI extends JFrame implements MouseListener
{
    // instance variables - replace the example below with your own
    private static JLabel called;
    private static JLabel current;
    private static String tmp;
    private static JLabel temp;
    /**
     * Constructor for objects of class CardGUI
     */
    public CardGUI(String path, JLabel cur)
    {
        temp = cur;
        tmp = path;
    }
    public void mouseExited(MouseEvent e)
    {
        current = null;
        called = null;
    }
    public void mouseEntered(MouseEvent e)
    {
        current = null;
        called = null;
    }
    public void mousePressed(MouseEvent e)
    {
        current = null;
        called = null;
    }
    public void mouseReleased(MouseEvent e)
    {
        current = null;
        called = null;
    }
    public void mouseClicked(MouseEvent e)
    {
        ImageIcon ic = GameGUI.createImageIcon(tmp, "");
        called = new JLabel(ic);
        current = temp;
        GameGUI.replace(current, called);
    }
    public static JLabel getCalled()
    {
        return called;
    }
    public static JLabel getCurrent()
    {
        return current;
    }
}

原类中的replace方法:

public static void replace(JLabel jl1, JLabel jl2)
    {
        JLabel calledr = jl2;
        p2.remove(jl1);
        p2.add(calledr);
        p2.revalidate();
        p2.repaint();
    } //p2 is the panel all of the JLabels are in

提前致谢!

最佳答案

您的 CardGUI 类仅使用静态变量。 静态变量对于类的所有实例仅存在一次。 所以

CardGUI one=new CardGUI("Path", label1)
CardGUI two=new CardGUI("otherPath", label2)

一和二共享相同的 tmp、temp、called、current 变量。因此,在第 2 行中,您将覆盖第 1 行中的 label1。 取决于您的显示、刷新、您显示的符号或路径在所有监听器中也相同。

问候 雷内克

编辑:

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

public class CardGUI extends JFrame implements MouseListener
{
    // instance variables - replace the example below with your own
    private JLabel called;
    private JLabel current;
    private String tmp;
    private JLabel temp;
    /**
     * Constructor for objects of class CardGUI
     */
    public CardGUI(String path, JLabel cur)
    {
        temp = cur;
        tmp = path;
    }
    public void mouseExited(MouseEvent e)
    {
        current = null;
        called = null;
    }
    public void mouseEntered(MouseEvent e)
    {
        current = null;
        called = null;
    }
    public void mousePressed(MouseEvent e)
    {
        current = null;
        called = null;
    }
    public void mouseReleased(MouseEvent e)
    {
        current = null;
        called = null;
    }
    public void mouseClicked(MouseEvent e)
    {
        ImageIcon ic = GameGUI.createImageIcon(tmp, "");
        called = new JLabel(ic);
        current = temp;
        GameGUI.replace(current, called);
    }
    public static JLabel getCalled()
    {
        return called;
    }
    public static JLabel getCurrent()
    {
        return current;
    }
}

这应该可以正常工作

关于java - 当调用 JPanel 中的一个鼠标监听器时,它们都会被调用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28881248/

相关文章:

java - 如何在获得焦点时制作 JComboBox 下拉列表?

java - JFrame 和 MouseListener 无法正确执行单击的问题

java - 如何在 JTextArea 上打印

java - Android: View 寻呼机中 fragment 之间的通信

java - 最后调用 super

java - MouseListener - 不适用于 JTextArea

java - 鼠标控制形状绘制

java - 多行字符串到数组

java - 第二次点击按钮时图像不会改变

java - 从不同目录访问文件