如何在 Java 中制作 Windows 7 工具提示 [英] How do I make a Windows 7 Tooltip in Java

查看:39
本文介绍了如何在 Java 中制作 Windows 7 工具提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在网上到处找,但我没有找到这个小问题的答案.在 Windows 7 中(我认为在 Vista 中)你有一个漂亮的圆形银色工具提示,它看起来比旧的黄色盒装蹩脚的工具提示要好得多.下面的如何在 Java 中制作 Windows 7 工具提示 - 堆栈溢出"提示是它的显示方式.

所以在 Java 中,我想要做到这一点,制作漂亮的新 Windows 工具提示.问题是我不知道该怎么做.

在下面的简单代码之外是一个 main 方法,它简单地创建了一个工具提示"类的对象.

import java.awt.FlowLayout;导入 javax.swing.JButton;导入 javax.swing.JFrame;导入 javax.swing.UIManager;导入 javax.swing.UnsupportedLookAndFeelException;公共类工具提示扩展 JFrame {公共工具提示(){尝试 {UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) {e.printStackTrace();}setLayout(new FlowLayout());JButton button = new JButton("工具提示按钮");button.setToolTipText("工具提示");添加(按钮);设置可见(真);setDefaultCloseOperation(EXIT_ON_CLOSE);setSize(250, 160);}}

正如您在 try/multi catch 中看到的,我将外观设置为程序当前运行的操作系统.如果我在 Windows 上运行这个程序,我会得到糟糕的黄色工具提示.

那么,如果程序在 Windows 操作系统上运行,我必须对代码进行哪些更改才能获得漂亮的银色工具提示?

解决方案

为此,您可能需要使用基于 JWindow 的组件,如我对 .

对于轻微的渐变(斜视),使用 GradientPaint.

工具提示提供的功能未包含在链接的简单示例中.但这是一个起点.确保您不会意外地将 Windows 7 风格的工具提示丢给 Windows 7 之前的用户或其他操作系统的用户.与您感受到方形/黄色 BG"工具提示冲突的方式相同,他们不会发现冲突样式漂亮".这导致..

<小时>

实现效果的最佳方式是通过基于 合成器的外观和感觉.

<块引用>

创建自定义外观或修改现有外观可能是一项艰巨的任务.javax.swing.plaf.synth 包可用于轻松创建自定义外观.您可以通过编程或使用外部 XML 文件来创建 Synth 外观.

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?

解决方案

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.

这篇关于如何在 Java 中制作 Windows 7 工具提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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