将鼠标悬停在JButtons上并显示一条消息 [英] Hovering over JButtons and displaying a message

查看:202
本文介绍了将鼠标悬停在JButtons上并显示一条消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将鼠标悬停在我的GUI(地图)上的许多JButton上,并显示该位置的名称,例如曼彻斯特和伦敦。我有一个按钮的代码,但它不适用于多个按钮,并打印所有按钮位置的最后一个 out 消息(因为我有10个按钮)。

I want to hover over a number of JButtons on my GUI (map) and display the name of that location e.g. Manchester and London. I have the code working for one button, but it does not work for more than one button and prints the last out message (as i have 10 buttons) for all button locations.

如果 button1 为真,那么它会通过我的<$ c $在指定区域的GUI上绘制文本c> paintComponent()方法。

If button1 is true it then draws the text on the GUI in the specified area via my paintComponent() method.

我如何解决这个问题?

button1.addMouseMotionListener(this);
button2.addMouseMotionListener(this);





public void mouseMoved(MouseEvent arg0)
{
    if(button1.contains(arg0.getPoint()))
    {
        button1  = true;
        out = "test 1";
        repaint();
    }

    if(!button1.contains(arg0.getPoint()))
    {
        b1 = false;
        out = " ";
        repaint();
    }//same for all 10 buttons but change variables
}


推荐答案

为什么不使用已存在的工具提示API?

Why not use the tool tip API that already exists?

button.setTooltip("Manchester");

您甚至可以使用HTML文本生成格式化结果。

You even use HTML text to produce formatted results.

button.setTooltip("<html>Manchester<br>53.4800° N, 2.2400° W</html>");

如果图像是嵌入的,你甚至可以提供图像......

If the images are embedded, you can even supply an image...

button.setTooltip("<html><img src=" + getClass().getResource("/someimage") + "/>Manchester<br>53.4800° N, 2.2400° W</html>");

这篇关于将鼠标悬停在JButtons上并显示一条消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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