如何使用Nimbus LookAndFeel更改JToolTip的背景颜色?

3
在使用Nimbus LookAndFeel的基于Swing的Java应用程序中,我尝试设置工具提示的背景颜色。因此,我创建了JToolTip的子类,并通过覆盖createToolTip()在我的组件中使用它。到目前为止都很好,工具提示正确显示,但是背景颜色没有改变。前景颜色按预期设置。 当将LookAndFeel更改为例如Metal时,可以按预期设置颜色。
以下是一个小示例,具有在Metal和Nimbus之间切换的功能。希望您能看到,只有在使用Metal时才设置了按钮工具提示的背景颜色。
import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JToolTip;

public class TooltipTestApp {

private static final String METAL_LOOK_AND_FEEL = "javax.swing.plaf.metal.MetalLookAndFeel";
private static final String NIMBUS_LOOK_AND_FEEL = "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel";
private static JButton button;
private static String usedLookAndFeel = NIMBUS_LOOK_AND_FEEL;

    public static void main(String args[]) {
        button = new JButton() {

            @Override
            public JToolTip createToolTip() {
                JToolTip toolTip = super.createToolTip();
                toolTip.setBackground(Color.BLACK);
                toolTip.setForeground(Color.RED);

                    return toolTip;
            }
        };

        button.addActionListener(new java.awt.event.ActionListener() {

            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TooltipTestApp.toggleLookAndFeel();
            }
        });

        button.setToolTipText("Some text");

        JFrame frame = new JFrame("TooltipTestApp");

        TooltipTestApp.toggleLookAndFeel();
        frame.add(button);
        frame.setSize(450, 100);
        frame.setVisible(true);
    }

    private static void toggleLookAndFeel() {
        try {
            if (usedLookAndFeel.equals(METAL_LOOK_AND_FEEL)) {
                usedLookAndFeel = NIMBUS_LOOK_AND_FEEL;
            } else {
                usedLookAndFeel = METAL_LOOK_AND_FEEL;
            }

            UIManager.setLookAndFeel(usedLookAndFeel);

            String lookAndFeelName = usedLookAndFeel.substring(usedLookAndFeel.lastIndexOf(".") + 1);
            button.setText("This is: " + lookAndFeelName);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

作为提示:使用的LookAndFeel是Nimbus。当更改它时,我可以设置背景颜色。因此问题最好是“在使用Nimbus LookAndFeel时如何更改JToolTip的背景颜色?” - pmoule
"有什么想法吗?" 为了更快地获得帮助,请发布一个SSCCE。为了获得“额外的分数”(或至少更多的帮助),使GUI具有在Metal和Nimbus PLAF之间切换的按钮。 - Andrew Thompson
@AndrewThomson: 谢谢!我改变了这个例子。 - pmoule
3条注释。1)SSCCE应包括导入。2)关于异常“//should not happen here”a)异常是针对非常状况的。b)运行1.6.0_09版本的客户端将触发该异常。c)ex.printStackTrace()不仅比注释更简短,而且更有用。3)我不知道如何解决问题。我让Swing专家来处理,希望他们很快会出现。 - Andrew Thompson
1
更改示例:添加导入并添加异常处理。 - pmoule
2个回答

6
以下内容也适用于Metal LAF,无需重写createToolTip()方法:

(注:LAF指Look and Feel,是Java Swing中的外观和感觉)

UIManager.put("ToolTip.background", Color.RED);

外观管理器可以选择是否使用UIManager属性。我不知道这是否适用于Nimbus。


@pmoule:你尝试过使用派生颜色吗? - trashgod
@trashgod:我没有看到任何效果,也不确定我是否做对了。我尝试了 Color derivedColor = nimbusLaf.getDerivedColor("textForeground",1, 1, 1, 1, true); 并通过 toolTip.setBackground(derivedColor); 分配了这个颜色。 - pmoule
3
为了绘制工具提示,Nimbus使用一个类型为'ToolTipPainter'的最终实例,并硬编码使用Nimbus默认值'info'。因此我有两个选项:1. 通过全局更改默认的'info'值来设置为黑色 UIManager.put("info", Color.BLACK);;2. 通过设置自己的Painter来覆盖UIDefaults UIDefaults defaults = new UIDefaults(); defaults.put("ToolTip[Enabled].backgroundPainter", new MyTooltipPainter()); toolTip.putClientProperty("Nimbus.Overrides", defaults); - pmoule

2
尝试这个:
 public class Main {
      public static void main(String args[]) {
        JFrame frame = new JFrame("JToolTip Sample");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JButton b1 = new JButton("Button 1") {
          public JToolTip createToolTip() {
            JToolTip tip = super.createToolTip();
            tip.setForeground(Color.YELLOW);
            tip.setBackground(Color.RED);
            tip.setFont(new Font("Arial", Font.BOLD,36));
            return tip;
          }
        };
        b1.setToolTipText("HELLO");
        frame.add(b1, BorderLayout.NORTH);
        frame.setSize(300, 150);
        frame.setVisible(true);
      }
    }

来源: http://www.java2s.com/Code/Java/Swing-JFC/ModifythebehaviourofthedefaultJToolTip.htm

本文介绍如何修改默认 JToolTip 的行为。在 Java 中,JToolTip 是用来显示一个组件的描述性文本的小窗口。通过使用 ToolTipManager 类和 JComponent 类,我们可以更改 JToolTip 的行为。具体来说,我们可以设置 JToolTip 延迟出现时间、持续时间和隐藏延迟,以及更改其背景和前景颜色。


这段代码无法改变工具提示的背景颜色。您可以通过简单的 setBackground 添加来实现,但在 Nimbus L&F 中无法使用。 - S.L. Barth

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接