【问题标题】:How to change the background color of a JToolTip using Nimbus LookAndFeel?如何使用 Nimbus LookAndFeel 更改 JToolTip 的背景颜色?
【发布时间】:2011-10-18 12:45:53
【问题描述】:

在使用 Nimbus LookAndFeel 的基于 Swing 的 Java 应用程序中,我尝试设置工具提示的背景颜色。所以我创建了一个 JToolTip 的子类,并通过覆盖 createToolTip() 在我的组件中使用它。到目前为止很好,工具提示正确显示,但背景颜色没有改变。前景色按预期设置。 将 LookAndFeel 更改为例如金属 我可以按预期设置颜色。

这是一个能够在 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 的背景颜色?”
  • “有什么想法吗?” 为了尽快获得更好的帮助,请发帖 SSCCE。对于“加分”(或至少有更多帮助),让 GUI 有一个按钮可以在 Metal 和 Nimbus PLAF 之间切换。
  • @AndrewThomson:谢谢!我更改了示例。
  • 3 cmets。 1) SSCCE 应包括进口。 2) 关于异常//should not happen here a) 异常是针对异常情况。 b) 运行 1.6.0_09 的客户端会触发该异常。 c) ex.printStackTrace() 不仅比注释短,而且更有用。 3)我不知道如何解决这个问题。我让给 Swing 专家,希望他们很快就会出现。
  • 更改示例:添加导入并添加异常处理

标签: java swing


【解决方案1】:

以下内容也适用于 Metal LAF,无需覆盖 createToolTip() 方法:

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

LAF 可以选择是否使用 UIManager 属性。我不知道这是否适用于 Nimbus。

【讨论】:

  • @pmoule:你试过derived color吗?
  • @trashgod:我看不到任何效果,我不确定我是否做对了。我尝试了Color derivedColor = nimbusLaf.getDerivedColor("textForeground",1, 1, 1, 1, true); 并由toolTip.setBackground(derivedColor); 分配了这个颜色
  • 对于绘制工具提示 Nimbus 使用最终类型 'ToolTipPainter' 的实例,并硬编码使用 Nimbus 默认值 'info'。所以我有2个选择。 1. 使用UIManager.put("info", Color.BLACK); 全局更改默认的“信息”值 2. 通过使用UIDefaults defaults = new UIDefaults(); defaults.put("ToolTip[Enabled].backgroundPainter", new MyTooltipPainter()); toolTip.putClientProperty("Nimbus.Overrides", defaults); 设置自己的Painter 来覆盖UIDefaults
【解决方案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

【讨论】:

  • 此代码不会更改工具提示的背景颜色。您可以使用简单的setBackground 添加它,但这在 Nimbus L&F 中不起作用。
猜你喜欢
  • 2012-01-05
  • 1970-01-01
  • 2012-08-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多