如何使用 Nimbus LookAndFeel 更改 JToolTip 的背景颜色?
在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以下内容也适用于 Metal LAF,无需重写 createToolTip() 方法:
LAF 可以选择是否使用 UIManager 属性。我不知道这是否适用于 Nimbus。
The following also works for the Metal LAF without overriding the createToolTip() method:
LAF's can choose whether to use the UIManager properties or not. I have no idea if this will work with Nimbus.
试试这个:
来源:http://www.java2s.com/Code/ Java/Swing-JFC/ModifythebehaviourofthedefaultJToolTip.htm
Try this:
Source : http://www.java2s.com/Code/Java/Swing-JFC/ModifythebehaviourofthedefaultJToolTip.htm