0

我在玻璃窗格下有一些带有两个图像的代码。我想让每个图像都在它自己的玻璃窗格下,并且每个玻璃窗格都发出它自己的鼠标监听器的信号。目前,我将它们都制作在一个玻璃窗格下,一个鼠标监听器用于整个玻璃窗格。两个图像并排在网格布局中,因此将玻璃窗格分成两半应该不会太难。这是一个玻璃窗格的代码,但请注意,我正在尝试制作两个玻璃窗格,并为每个图像制作两个单独的鼠标侦听器类。这只是带有一个*两个图像的鼠标侦听器的代码:

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)它在一次退出/进入后不会改变。我非常感谢您的帮助,这是基于 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();
    }
}
4

2 回答 2

2

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

相反,我会使用玻璃并监视鼠标在其上方的运动以找到它经过的组件,然后相应地更改“悬停标签”。

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");
        }

    }
}

我想知道工具提示是否会更容易......

于 2015-08-30T09:32:19.983 回答
2

如果您真正了解您要实现的目标,那么几乎每个问题都可以用 10-50 行代码来演示。

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

你有几个选择:

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

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

编辑:

如何将 getSource() 方法添加到我的 mouseevent?

10 天前已经回答了这个问题:JLabel 上的翻转,它由网格布局中的图像组成?.

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

1)离鼠标极远,

我已经在这个问题中回答了这个问题。我说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 的“源”对象(而不是长颈鹿标签),如上面的答案所示。当您使用源对象时,代码将适用于两个标签。

2) 它没有显示完整的 JLabel

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

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

现在标签将具有适当的宽度/高度。然后,当您在玻璃窗格上移动标签时,您只需使用该setLocation(...)方法来定位标签。

一次退出/进入后不变

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

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

同样,请记住,您只需要一个类来实现 10 天前演示的 MouseListener 和 MouseMotionListener。可以将相同的侦听器实例添加到两个组件中,这将有助于清理您的代码。

于 2015-08-30T01:11:32.970 回答