How do I make a Windows 7 Tooltip in Java

試著忘記壹切 提交于 2020-01-02 07:18:35

问题


I've been looking everywhere online but I have found no answers to this little problem. In Windows 7 (and in Vista, I think,) you have a nice rounded silver looking tooltip that looks way better than the old yellow boxed crappy looking one. The "How do I make a Windows 7 Tooltip in Java - Stack Overflow" tip below is how it appears.

So in Java, I want to do exactly that, make the nice new Windows tooltip. The problem is I don't know how to.

Outside of the following simple code is a main method that simply creates an object of this class 'Tooltip'.

import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Tooltip extends JFrame {
public Tooltip() {

    try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) {
        e.printStackTrace();
    }

    setLayout(new FlowLayout());

    JButton button = new JButton("Tooltip Button");
    button.setToolTipText("Tooltip");
    add(button);

    setVisible(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setSize(250, 160);
    }
}

As you can see in the try/multi catch, I set the Look and Feel to the operating system that the program is currently running on. If I were to run this program on Windows, I would get the crummy yellow tooltip.

So what changes do I have to make to the code so that I will get a nice silver tooltip if the program is running on a Windows operating system?


回答1:


You might need to use a component based on a JWindow for this, as in my answer to Preview window (like Windows 7 taskbar shows for opened applications).

For the rounded corners, see How to Create Translucent and Shaped Windows.

For the slight gradient (squints), paint the BG using a GradientPaint.

A tool-tip provides a functionality not included in that simple example linked. But it is a starting point. Make sure you don't accidentally drop the Windows 7 style tool-tip to pre Windows 7 users or users of other OS'. In the same way you feel the 'square/yellow BG' tool-tip clashes, they will not find the clashing style to be 'pretty'. Which leads to..


The best way to achieve the effect is through a custom PLAF based on The Synth Look and Feel.

Creating a custom look and feel, or modifying an existing one, can be a daunting task. The javax.swing.plaf.synth package can be used to create a custom look and feel with much less effort. You can create a Synth look and feel either programatically or through the use of an external XML file.



来源:https://stackoverflow.com/questions/11166318/how-do-i-make-a-windows-7-tooltip-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!