在JLabel上的过渡是由网格布局中的图像组成的? [英] Rollover on JLabel which consists of image within grid layout?

查看:42
本文介绍了在JLabel上的过渡是由网格布局中的图像组成的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有网格布局和两个JLabel图像的代码.每次将鼠标悬停在每张图片上时,我都不会出现一些文字.我对当图像不是JLabel时如何执行此操作很熟悉,但是在整个Web上进行搜索以找到在未命名的JLabel时如何执行此操作.我不希望看到的两张带有单独翻转消息的图像分别是:

I have a code with grid layout and two JLabel images. I wan't some text to appear everytime I roll over each image. I am familiar on how to do this when the image is not a JLabel, but have searched all over the web to find how to do this while it is an unnamed JLabel. The two images I wan't to have, with separate roll over messages are:

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

这是我的代码:

    public class giraffe implements ActionListener{


        public void actionPerformed(ActionEvent event) {


            JOptionPane.showMessageDialog(null,
                    "Press ok, and see the amazing giraffe outside a window!");

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

            /*
             * I want to have a rollover on EACH IMAGE so that when they rollover the image you see different text.
             */

        }

非常感谢您花时间阅读本文,非常感谢您为帮助程序员而付出的努力!

Thank you so much for the time you are taking for reading this, I really appreciate the effort you are putting into helping a fellow programmer!

推荐答案

首先查看如何编写鼠标侦听器.

基本上,您希望将MouseListener附加到每个标签并监视mouseEnteredmouseExited事件,并根据需要更新标签状态

Basically, you want to attach a MouseListener to each label and monitor the mouseEntered and mouseExited events, updating the label state as per your requirements

import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            try {
                JLabel left = new JLabel(new ImageIcon(ImageIO.read(...))));
                left.setVerticalTextPosition(JLabel.BOTTOM);
                left.setHorizontalTextPosition(JLabel.CENTER);
                left.setHorizontalAlignment(JLabel.CENTER);
                left.setVerticalAlignment(JLabel.CENTER);
                left.setText(" ");
                JLabel right = new JLabel(new ImageIcon(ImageIO.read(...))));
                right.setVerticalTextPosition(JLabel.BOTTOM);
                right.setHorizontalTextPosition(JLabel.CENTER);
                right.setHorizontalAlignment(JLabel.CENTER);
                right.setVerticalAlignment(JLabel.CENTER);
                right.setText(" ");

                setLayout(new GridLayout(1, 2));

                add(left);
                add(right);

                left.addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseEntered(MouseEvent e) {
                        left.setText("I'm on the left");
                    }

                    @Override
                    public void mouseExited(MouseEvent e) {
                        left.setText(" ");
                    }
                });
                right.addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseEntered(MouseEvent e) {
                        right.setText("I'm on the right");
                    }

                    @Override
                    public void mouseExited(MouseEvent e) {
                        right.setText(" ");
                    }
                });
            } catch (IOException exp) {
                exp.printStackTrace();
            }
        }

    }

}

您可能还想看看阅读/加载图片

这篇关于在JLabel上的过渡是由网格布局中的图像组成的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆