Java-如何为JFrame设置setToolTipText

时间:2013-07-05 22:44:31

标签: java swing jframe

我想将鼠标移动到使用g2D.drawRenderedImag在JFrame上绘制的图像上,并在鼠标光标旁边的工具提示文本处显示该像素的x,y。即:

Graphics2D g2D = (Graphics2D)g;
g2D.drawRenderedImage...

我知道如何读取x,y但不知道如何为JFrame设置setToolTipText。你能帮帮我吗?我的意思是我不能这样做       JFrame.setToolTipText !!!!

3 个答案:

答案 0 :(得分:2)

你可以做componentName.setToolTipText(“context”);有关更多信息,您可以查看http://docs.oracle.com/javase/tutorial/uiswing/components/tooltip.html许多有关Swing API的有用信息

我现在看到你想为JFrame而不是组件做这个,这适用于JFrame的标题,也许它有帮助

 import darrylbu.util.SwingUtils;
import javax.swing.*;

public class FrameTitleToolTip {

   public static void main(String[] args) {
      JFrame.setDefaultLookAndFeelDecorated(true);
      SwingUtilities.invokeLater(new Runnable() {

         @Override
         public void run() {
            new FrameTitleToolTip().makeUI();
         }
      });
   }

   public void makeUI() {
      JFrame frame = new JFrame();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setSize(400, 400);
      for (JComponent component : SwingUtils.getDescendantsOfType(JComponent.class,
            frame)) {
         if (component.getClass().getName().contains("MetalTitlePane")) {
            component.setToolTipText("Tooltip for frame title bar");
            break;
         }
      }
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }
}

答案 1 :(得分:2)

您需要利用getToolTip

提供的JComponent方法

您还需要注册ToolTipManager,方法是使用非setToolTipText值在您的组件上调用null,或使用ToolTipManager#registerComponent

更新了示例

你似乎在说你直接渲染到JFrame,大概是通过覆盖paint方法。这是非常不推荐的,除了任何东西,顶级容器不是双缓冲的,所以当你更新屏幕内容时,你最终会有闪烁的可能性。

使用像JPanel这样的东西要好得多,它是双缓冲的并且有为你工作的好处(当涉及显示工具提示时)。它还使您的组件更具可移植性和可重用性

enter image description here

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.ToolTipManager;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestToolTipImage {

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

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

                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new ImagePane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }

        });
    }

    public class ImagePane extends JPanel {

        private BufferedImage img;

        public ImagePane() {
            try {
                img = ImageIO.read(getClass().getResource("/TestToolTipImage.jpg"));
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            ToolTipManager.sharedInstance().registerComponent(this);
        }

        @Override
        public Dimension getPreferredSize() {
            return img == null ? super.getPreferredSize() : new Dimension(img.getWidth(), img.getHeight());
        }

        @Override
        public String getToolTipText(MouseEvent event) {

            String text = super.getToolTipText(event);

            if (img != null) {

                int xOffset = (getWidth() - img.getWidth()) / 2;
                int yOffset = (getHeight() - img.getHeight()) / 2;
                Rectangle bounds = new Rectangle(xOffset, yOffset, img.getWidth(), img.getHeight());

                if (bounds.contains(event.getPoint())) {

                    int x = event.getPoint().x - xOffset;
                    int y = event.getPoint().y - yOffset;
                    int rgb = img.getRGB(x, y);                    
                    String hex = Integer.toHexString(rgb & 0xffffff);

                    StringBuilder sb = new StringBuilder(128);
                    sb.append("<html><table><tr><td>Pixel at ").
                                    append(x).append("x").append(y).
                                    append("</td>");
                    sb.append("<td bgcolor='#").append(hex).append("'>&nbsp;</td>");
                    sb.append("</tr></table><html>");

                    System.out.println(sb.toString());
                    text = sb.toString();

                }

            }

            return text;

        }

        @Override
        public Point getToolTipLocation(MouseEvent event) {
            return event.getPoint();
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            if (img != null) {
                int x = (getWidth() - img.getWidth()) / 2;
                int y = (getHeight() - img.getHeight()) / 2;
                g.drawImage(img, x, y, this);
            }
        }
    }
}

答案 2 :(得分:2)

由于JFrame未从JComponent继承,因此它没有setToolTipText方法。尝试从JLayeredPan获取JFrame e并调用它的工具提示方法:

getLayeredPane().setToolTipText("text");