0

ガラス板の下に 2 つの画像を含むコードがあります。各画像が独自のガラス枠の下にあり、各ガラス枠が独自のマウスリスナーの信号を送るようにしたいと考えています。現在、私は両方を 1 つのガラス板の下に作成し、ガラス板全体に対して 1 つのマウスリスナーを使用しています。どちらの画像もグリッド レイアウトで並べて表示されているため、ガラス ペインを半分に分割するのは難しくありません。これは 1 つのガラス ペインのコードですが、2 つのガラス ペインと、画像ごとに 2 つの個別のマウス リスナ クラスを作成しようとしていることに注意してください。これは、**両方の画像に対して 1 つの*マウス リスナーを使用した単なるコードです。

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 のリクエストによるコードは次のとおりです。2 つの別個のマウス リスナーがあることに注意してください。別の方法でそれを行う方法を知りたいです。JLabel がマウスを追跡する場合、1) マウスから非常に離れている、2) 完全な JLabel が表示されない、3) 1 回の終了/開始後に変更されない。本当に助かりました。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

MouseListenerで を使用すると問題が発生すると思います。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 に答える