Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将鼠标悬停在JButtons上并显示消息_Java_Swing_Tooltip_Jbutton_Mousemotionevent - Fatal编程技术网

Java 将鼠标悬停在JButtons上并显示消息

Java 将鼠标悬停在JButtons上并显示消息,java,swing,tooltip,jbutton,mousemotionevent,Java,Swing,Tooltip,Jbutton,Mousemotionevent,我想将鼠标悬停在GUI(地图)上的多个JButton上,并显示该位置的名称,例如曼彻斯特和伦敦。我的代码适用于一个按钮,但它不适用于多个按钮,并为所有按钮位置打印最后一条信息(因为我有10个按钮) 如果button1为true,则它将通过我的paintComponent()方法在GUI上的指定区域绘制文本 我如何解决这个问题 button1.addMouseMotionListener(this); button2.addMouseMotionListener(this); 不要使用JBut

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

如果
button1
为true,则它将通过我的
paintComponent()
方法在GUI上的指定区域绘制文本

我如何解决这个问题

button1.addMouseMotionListener(this);
button2.addMouseMotionListener(this);
  • 不要使用
    JButton
    中的
    MouseListener
    MosueMotionListener
    ,此方法在
    JButtons API
    中正确实现

  • 没有理由,我找不到对此作业使用
    repaint()
    的理由

  • 另一种方法是将
    ChangeListener
    添加到
    JButton
    并从派生的
    ButtonModel

  • 为了获得更好的帮助,请尽快发布一个简短的、可运行的、可编译的、大约
    JFrame
    和一个
    JButton


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

      button.setTooltip("Manchester");
      
      您甚至可以使用HTML文本生成格式化的结果

      button.setTooltip("<html>Manchester<br>53.4800° N, 2.2400° W</html>");
      
      按钮。设置工具提示(“曼彻斯特
      53.4800°N,2.2400°W”);
      如果图像是嵌入的,你甚至可以提供一个图像

      button.setTooltip("<html><img src=" + getClass().getResource("/someimage") + "/>Manchester<br>53.4800° N, 2.2400° W</html>");
      
      按钮。设置工具提示(“曼彻斯特
      53.4800°N,2.2400°W”);
      这个答案是针对JDK 8用户的coolio,所以请尝试一下:

      对于常规文本

      buttonyoumade.setToolTipText("Text you choose");
      
      供html使用

      anotherbuttonyoumade.setToolTipText("<html> any valid html code </html>");
      
      anotherbuttonyoumade.setToolTipText(“任何有效的html代码”);
      
      “如果图像被嵌入,你甚至可以提供一个图像…”它们不一定要被嵌入,但如果是这样,a)更快&b)可被沙盒应用程序使用。@andrewhompson啊,这可能是任何有效的
      URL
      anotherbuttonyoumade.setToolTipText("<html> any valid html code </html>");