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

发布于 2024-12-10 16:41:37 字数 2170 浏览 0 评论 0原文

在使用 Nimbus LookAndFeel 的基于 Swing 的 Java 应用程序中,我尝试设置工具提示的背景颜色。因此,我创建了 JToolTip 的子类,并通过重写 createToolTip() 在我的组件中使用它。到目前为止一切正常,工具提示显示正确,但背景颜色没有改变。前景色已按预期设置。 当将 LookAndFeel 更改为例如 Metal 时,我可以按预期设置颜色。

这是一个能够在 Metal 和 Nimbus 之间切换的小例子。正如 yopu 希望看到的那样,按钮工具提示的背景颜色仅在使用 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();
        }
    }
}

In a Swing-based Java application using Nimbus LookAndFeel I try to set the background color of my tooltips. So I created a subclass of JToolTip and used it in my components by overriding createToolTip(). Fine so far and the tooltip is shown correctly, but the background color does not change. The foreground color is set as expected.
When changing the LookAndFeel to e.g. Metal I can set colors as expected.

Here a small example with ability to switch between Metal and Nimbus. As yopu hopefully see, the background color of the button's tooltip is only set when Metal is used.

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();
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情泪▽动烟 2024-12-17 16:41:37

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

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

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

The following also works for the Metal LAF without overriding the createToolTip() method:

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

LAF's can choose whether to use the UIManager properties or not. I have no idea if this will work with Nimbus.

吃颗糖壮壮胆 2024-12-17 16:41:37

试试这个:

 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

Try this:

 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);
      }
    }

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文