我可以将玻璃窗格分成两部分,并为每个窗格添加一个单独的鼠标侦听器吗?

Can I split a glass pane up into two, and add a seperate mouselistener to each?

我有一些代码在玻璃窗格下有两个图像。我想使每个图像都在其自己的玻璃窗格下,并且每个玻璃窗格都发出其自己的鼠标监听器的信号。目前,我已将它们都放在一个玻璃窗格下,整个玻璃窗格都有一个鼠标监听器。两个图像并排采用网格布局,因此将玻璃板分成两半应该不会太难。这是一个玻璃板的代码,但请注意,我正在尝试制作两个玻璃板,以及两个 seperate 鼠标侦听器 类 for each 图片。这只是带有 one* 鼠标侦听器的代码,用于 **both 图像:

package Buttons;


import java.awt.GridLayout;
import java.awt.event.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Giraffewindow extends JDialog {
public Giraffewindow() {
    JDialog giraffewindow = new JDialog();

    Icon giraffe = new ImageIcon(getClass().getResource("giraffe.png"));
    Icon windows = new ImageIcon(getClass().getResource("windows.png"));

    giraffewindow.setLayout(new GridLayout(1, 2, 0, 0));
    giraffewindow.add(new JLabel(windows));
    giraffewindow.add(new JLabel(giraffe));


    giraffewindow.pack();
    giraffewindow.setTitle("GIRAFFE!");
    giraffewindow.setVisible(true);
    giraffewindow.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    JPanel glass = ((JPanel) giraffewindow.getGlassPane());
    glass.setVisible(true);
    status = new JLabel("I can change");

    glass.add(status);
    glass.setLayout(null);
    giraffemousehandler giraffemouse = new giraffemousehandler();
    glass.addMouseListener(giraffemouse);
    glass.addMouseMotionListener(giraffemouse); //Can I add mouse motion listener to a picture
    // setLayout(null);
}


JLabel status = null;

class giraffemousehandler extends MouseAdapter implements MouseListener, MouseMotionListener { //MouseAdapter makes it so that you don't have to have all 7 implemented mouse listener methods

    @Override
    public void mouseMoved(MouseEvent e) {
        // TODO Auto-generated method stub
        status.setBounds(e.getX(), e.getY(), 50, 60); //Makes JLabel follow mouse

    }

    @Override
    public void mouseEntered(MouseEvent e) {

        status.setText("Enter);

    }

    @Override
    public void mouseExited(MouseEvent e) {

        status.setText("Exit");
        // status.setBounds(e.getX(), e.getY(), 5, 6);

    }

}
}

这是 camickr 要求的代码,请注意有两个独立的鼠标侦听器,否则我很想知道该怎么做。当 JLabel 跟随鼠标时,1) 它离鼠标非常远,2) 它没有显示完整的 JLabel 和 3) 它在一个 exit/enter 之后没有改变。非常感谢您的帮助,这里是基于 camickrs 建议的代码:

import java.awt.GridLayout;
import java.awt.event.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;

class SSCCE extends JDialog {

public SSCCE() {
    JDialog giraffewindow = new JDialog();

    Icon giraffe = new ImageIcon(getClass().getResource("giraffe.png"));
    Icon windows = new ImageIcon(getClass().getResource("windows.png"));

    giraffewindow.setLayout(new GridLayout(1, 2, 0, 0));
    JLabel giraffelabel = new JLabel();
    JLabel windowlabel = new JLabel();

    windowlabel.setIcon(windows);
    giraffelabel.setIcon(giraffe);

    giraffewindow.add(windowlabel);
    giraffewindow.add(giraffelabel);

    giraffewindow.setTitle("Title!");
    giraffewindow.setSize(1100, 600);
    giraffewindow.setVisible(true);
    giraffewindow.setDefaultCloseOperation(DISPOSE_ON_CLOSE);

    JPanel glass = ((JPanel) giraffewindow.getGlassPane()); //Glasspane
    glass.setVisible(true);

    status = new JLabel("I can change"); //This is the JLabel which should follow my mouse

    glass.add(status);
    glass.setLayout(null);

    giraffemousehandler giraffemouse = new giraffemousehandler();
    windowmousehandler windowmouse = new windowmousehandler();

    windowlabel.addMouseListener(windowmouse);
    giraffelabel.addMouseMotionListener(giraffemouse); //Can I add mouse motion listener to a picture

    // setLayout(null);
}

JLabel status = null;

class giraffemousehandler extends MouseAdapter implements MouseListener, MouseMotionListener { //MouseAdapter makes it so that you don't have to have all 7 implemented mouse listener methods

    @Override
    public void mouseMoved(MouseEvent e) {
        // TODO Auto-generated method stub
        status.setBounds(e.getX(), e.getY(), 50, 60); //Makes JLabel follow mouse

    }

    @Override
    public void mouseEntered(MouseEvent e) {

        status.setText("Mouse is on giraffe");

    }

    @Override
    public void mouseExited(MouseEvent e) {

        status.setText("Mouse has left giraffe");
        // status.setBounds(e.getX(), e.getY(), 5, 6);

    }

}

class windowmousehandler extends MouseAdapter implements MouseListener, MouseMotionListener {
    public void mouseMoved(MouseEvent event) {
        // TODO Auto-generated method stub
        status.setBounds(event.getX(), event.getY(), 50, 60); //Makes JLabel follow mouse

    }

    public void mouseEntered(MouseEvent event) {

        status.setText("Mouse is on window");

    }

    @Override
    public void mouseExited(MouseEvent event) {

        status.setText("Mouse has left window");
        // status.setBounds(e.getX(), e.getY(), 5, 6);

    }
}
}

public class Icallsscce {
    public static void main(String [] args) {
        SSCCE object = new SSCCE();
    }
}

Almost every problem can be demonstrated in 10-50 lines of code if you truly understand what you are trying to achieve.

没有。一个组件就是一个组件,不能把一个组件一分为二。

您有两个选择:

  1. 如果将 MouseListener 添加到 GlassPane,则需要使用事件中的鼠标点并使用 Container.getComponentAt(...) 确定鼠标当前位于哪个组件上。请注意,GlassPane 覆盖了整个框架,因此鼠标点是相对于包含边框的框架的,因此您首先需要使用 SwingUtilities.convertPoint(...) 方法将鼠标点转换为相对于您添加的面板的点标签到.

  2. 如果为每个标签添加单独的 MouseListeners,则鼠标点将相对于标签,因此您需要将标签的 "x" 值添加到玻璃上的点移动弹出标签时的窗格。我认为这种方法更容易。请注意,使用此方法您可以共享相同的侦听器,您只需要使用 MouseEvent 的 getSource() 方法来获取标签。

编辑:

how I can add the getSource() method to my mouseevent?

10 天前已回答:

当你甚至不看代码时它会很烦人,因为它不是一个完整的程序。我们不是来为您编写代码的。当有人花时间回答问题时,您至少可以花时间理解建议。

1) it is extremely far from the mouse,

这个问题我已经回答过了。我说 you will need to add the "x" value of the label..。您需要这样做,因为鼠标事件是相对于添加侦听器的组件生成的。

因此对于长颈鹿标签,x 的值将从 0 开始,并随着鼠标向右移动而增加。但是,长颈鹿从 Glass Pane 的中心开始,因此您不能使用 0 作为弹出标签的位置。您还需要包括长颈鹿的 x 位置。

所以基本代码应该是:

popupLabel.setLocation(e.getX() + giraffe.getLocation().x, e.getY());

当然,您应该使用 MouseEvent 的 "source" 对象(而不是长颈鹿标签),如上面的答案所示。当您使用源对象时,代码将适用于两个标签。

2) it does not show the full JLabel

因为您在 setBounds() 方法中使用了 "magic numbers"。为什么标签的宽度使用“60”这样的数字?不要硬编码数字。相反,当您更改标签的文本时,您可以这样做:

label.setText(....);
label.setSize(label.getPreferredSize());

现在标签将具有正确的 width/height。然后,当您在玻璃板上移动标签时,您只需使用 setLocation(...) 方法来定位标签。

It does not change after one exit/enter

您正在实现 MouseListener 和 MouseMotionListener,因此您需要将这两个侦听器添加到每个组件。所以你需要:

giraffe.addMouseListener(...); giraffe.addMouseMotionListener(...); window.addMouseListener(...); window.addMouseMotionListener(...);

再次提醒,您只需要 class 即可实现 10 天前演示的 MouseListener 和 MouseMotionListener。可以将相同的侦听器实例添加到这两个组件,这将有助于清理您的代码。

我真的认为您在 glassPane 上使用 MouseListener 会有问题,因为这会消耗鼠标事件,这意味着任何其他组件实际上都不会已通知。

相反,我会使用玻璃并监视鼠标在其上的移动以找到它经过的组件,然后相应地更改 "hover label"。

import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRootPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import static javax.swing.WindowConstants.DISPOSE_ON_CLOSE;

public class Giraffewindow {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }
                new Giraffewindow();
            }
        });
    }

    public Giraffewindow() {
        JDialog giraffewindow = new JDialog();

        try {
            Icon giraffe = new ImageIcon(ImageIO.read(new File("...")));
            Icon windows = new ImageIcon(ImageIO.read(new File("...")));

            JLabel left = new JLabel(windows);
            JLabel right = new JLabel(giraffe);

            giraffewindow.setLayout(new GridBagLayout());
            giraffewindow.add(left);
            giraffewindow.add(right);

            giraffewindow.pack();
            giraffewindow.setLocationRelativeTo(null);
            giraffewindow.setTitle("GIRAFFE!");
            giraffewindow.setVisible(true);
            giraffewindow.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
            JPanel glass = ((JPanel) giraffewindow.getGlassPane());
            glass.setVisible(true);
            status = new JLabel("I can change");
            status.setForeground(Color.WHITE);
            status.setBackground(Color.RED);
            status.setOpaque(true);

            glass.add(status);
            glass.setLayout(null);
            giraffemousehandler giraffemouse = new giraffemousehandler();

            glass.addMouseMotionListener(giraffemouse);

            // setLayout(null);
        } catch (IOException exp) {
            exp.printStackTrace();
        }
    }

    JLabel status = null;

    class giraffemousehandler extends MouseAdapter implements MouseListener, MouseMotionListener { //MouseAdapter makes it so that you don't have to have all 7 implemented mouse listener methods

        private Component last = null;

        @Override
        public void mouseMoved(MouseEvent e) {
            // TODO Auto-generated method stub

            Point p = e.getPoint();

            JRootPane rootPane = SwingUtilities.getRootPane(e.getComponent());
            Container contentPane = rootPane.getContentPane();
            Component comp = contentPane.getComponentAt(p);
            if (comp != last) {
                if (last != null) {
                    mouseExited(last);
                }
                if (comp != null) {

                    if (comp != contentPane) {
                        last = comp;
                        mouseEntered(comp);
                    } else {
                        last = null;
                    }

                }
            }

            status.setSize(status.getPreferredSize());
            status.setLocation(p.x, p.y);

        }

        protected void mouseExited(Component comp) {
            System.out.println("Exited");
            status.setText("Exited");
        }

        protected void mouseEntered(Component comp) {
            status.setText("Entered");
        }

    }
}

我想知道工具提示是否会更简单...