- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我们正在使用 Nimbus LaF 开发一个 Swing 应用程序。我们更改了许多 Nimbus 默认设置(控件、文本、NimbusLightBackground 等)以拥有深色主题。
现在我们在渲染 JLists 和 JComboBoxes 时遇到了很大的麻烦,因为渲染器显然对选定的文本前景使用了 NimbusLightBackground 颜色。这会导致深蓝色背景上出现深灰色文本 - 不好。
我已经尝试通过 UIManager.putDefault() 和每个组件覆盖全局覆盖 Nimbus Defaults 中任何看似适用的键(“ComboBox:\”ComboBox.listRenderer\“[Selected].textForeground”等),但根本无法找零。
即使是 SwingX 荧光笔似乎也无法覆盖此行为,至少在组合框下拉列表中是这样。
关于如何为 J(X)List 和 J(X)ComboBox Dropdowns 设置选定文本前景色有什么想法吗?
我对每个组件覆盖的最新尝试:
JXComboBox comboBox = new JXComboBox();
UIDefaults comboBoxTheme = new UIDefaults();
comboBoxTheme.put("nimbusLightBackground", new Color(0xFFFAFA));
comboBoxTheme.put("ComboBox:\"ComboBox.listRenderer\"[Selected].textForeground", new Color(0xFFFAFA));
comboBox.putClientProperty("Nimbus.Overrides.InheritDefaults", true);
comboBox.putClientProperty("Nimbus.Overrides", comboBoxTheme);
SwingUtilities.updateComponentTreeUI(comboBox);
应用程序范围的 nimbus 默认值:
ColorUIResource backgroundUI = new ColorUIResource(0x494949);
ColorUIResource textUI = new ColorUIResource(0xFFFAFA);
ColorUIResource controlBackgroundUI = new ColorUIResource(0x5F5F4D);
ColorUIResource infoBackgroundUI = new ColorUIResource(0x2f5cb4);
ColorUIResource infoUI = new ColorUIResource(0x2f5cb4);
ColorUIResource lightBackgroundUI = new ColorUIResource(0x5D5D5B);
ColorUIResource focusUI = new ColorUIResource(0x39698a);
UIManager.put("control", backgroundUI);
UIManager.put("text", textUI);
UIManager.put("nimbusLightBackground", lightBackgroundUI);
UIManager.put("info", infoUI);
UIManager.put("nimbusInfoBlue", infoBackgroundUI);
UIManager.put("nimbusBase", controlBackgroundUI);
UIManager.put("nimbusBlueGrey", controlBackgroundUI);
UIManager.put("nimbusFocus", focusUI);
全部在 Java 7u55 中实现,但我怀疑这是否重要,因为似乎没有人维护 Swing/Nimbus 已经有一段时间了。
PS:我当然读过this question和其他人,但尚未找到有效的答案。
编辑:这是一个演示问题的 SSCCE。它创建一个 JFrame,顶部有一个仅默认的组合框,中间有一个列表,底部有一个每个组件覆盖的组合框。在列表中或从框下拉列表中选择一个值时,可以看到该问题。
package sscce;
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.UIDefaults;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.plaf.ColorUIResource;
public class ForegroundProblemDemo extends JFrame {
public ForegroundProblemDemo() {
super("Demo");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JComboBox<String> comboBoxWithDefaults = createComboBox();
JComboBox<String> comboBoxWithOverrides = createComboBox();
JList<String> list = createList();
addOverrides(comboBoxWithOverrides);
getContentPane().setLayout(new BorderLayout());
getContentPane().add(new JScrollPane(list), BorderLayout.CENTER);
getContentPane().add(comboBoxWithDefaults, BorderLayout.NORTH);
getContentPane().add(comboBoxWithOverrides, BorderLayout.SOUTH);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
JComboBox<String> createComboBox() {
JComboBox<String> comboBox = new JComboBox<>(new String[] {"A","B","C","D"});
return comboBox;
}
JList<String> createList() {
JList<String> list = new JList<>(new String[] {"A","B","C","D"});
return list;
}
void addOverrides(JComponent component) {
UIDefaults theme = new UIDefaults();
theme.put("nimbusLightBackground", new Color(0xFFFAFA));
theme.put("ComboBox:\"ComboBox.listRenderer\"[Selected].textForeground", new Color(0xFFFAFA));
component.putClientProperty("Nimbus.Overrides.InheritDefaults", true);
component.putClientProperty("Nimbus.Overrides", theme);
SwingUtilities.updateComponentTreeUI(component);
}
public static void main(String... args) throws Throwable {
ColorUIResource backgroundUI = new ColorUIResource(0x494949);
ColorUIResource textUI = new ColorUIResource(0xFFFAFA);
ColorUIResource controlBackgroundUI = new ColorUIResource(0x5F5F4D);
ColorUIResource infoBackgroundUI = new ColorUIResource(0x2f5cb4);
ColorUIResource infoUI = new ColorUIResource(0x2f5cb4);
ColorUIResource lightBackgroundUI = new ColorUIResource(0x5D5D5B);
ColorUIResource focusUI = new ColorUIResource(0x39698a);
UIManager.put("control", backgroundUI);
UIManager.put("text", textUI);
UIManager.put("nimbusLightBackground", lightBackgroundUI);
UIManager.put("info", infoUI);
UIManager.put("nimbusInfoBlue", infoBackgroundUI);
UIManager.put("nimbusBase", controlBackgroundUI);
UIManager.put("nimbusBlueGrey", controlBackgroundUI);
UIManager.put("nimbusFocus", focusUI);
for (LookAndFeelInfo lafInfo : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(lafInfo.getName())) {
UIManager.setLookAndFeel(lafInfo.getClassName());
break;
}
}
new ForegroundProblemDemo();
}
}
编辑 2:抱歉,之前应该提到这一点:对于列表,使用 setSelectionForeground() 方法可以轻松解决问题。对于组合框,我还没有找到缺少自定义渲染器的方法。所以我在这里的主要重点是组合框。
最佳答案
import java.awt.*;
import java.util.Vector;
import javax.swing.*;
import javax.swing.UIManager;
import javax.swing.plaf.ColorUIResource;
import javax.swing.plaf.nimbus.AbstractRegionPainter;
public class MyComboBox {
private Vector<String> listSomeString = new Vector<String>();
private JComboBox someComboBox = new JComboBox(listSomeString);
private JComboBox editableComboBox = new JComboBox(listSomeString);
private JComboBox non_EditableComboBox = new JComboBox(listSomeString);
private JFrame frame;
public MyComboBox() {
listSomeString.add("-");
listSomeString.add("Snowboarding");
listSomeString.add("Rowing");
listSomeString.add("Knitting");
listSomeString.add("Speed reading");
someComboBox.setPrototypeDisplayValue("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
someComboBox.setFont(new Font("Serif", Font.BOLD, 16));
someComboBox.setEditable(true);
someComboBox.getEditor().getEditorComponent().setBackground(Color.YELLOW);
((JTextField) someComboBox.getEditor().getEditorComponent()).setBackground(Color.YELLOW);
editableComboBox.setPrototypeDisplayValue("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
editableComboBox.setFont(new Font("Serif", Font.BOLD, 16));
editableComboBox.setEditable(true);
JTextField text = ((JTextField) editableComboBox.getEditor().getEditorComponent());
text.setBackground(Color.YELLOW);
/*JComboBox coloredArrowsCombo = editableComboBox;
Component[] comp = coloredArrowsCombo.getComponents();
for (int i = 0; i < comp.length; i++) {
if (comp[i] instanceof MetalComboBoxButton) {
MetalComboBoxButton coloredArrowsButton = (MetalComboBoxButton) comp[i];
coloredArrowsButton.setBackground(null);
break;
}
}*/
non_EditableComboBox.setPrototypeDisplayValue("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
non_EditableComboBox.setFont(new Font("Serif", Font.BOLD, 16));
frame = new JFrame();
frame.setLayout(new GridLayout(0, 1, 10, 10));
frame.add(someComboBox);
frame.add(editableComboBox);
frame.add(non_EditableComboBox);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocation(100, 100);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
try {
for (UIManager.LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(laf.getName())) {
UIManager.setLookAndFeel(laf.getClassName());
UIManager.getLookAndFeelDefaults().put("ComboBox[Enabled].backgroundPainter",
new javax.swing.plaf.nimbus.AbstractRegionPainter() {
@Override
protected AbstractRegionPainter.PaintContext getPaintContext() {
return new AbstractRegionPainter.PaintContext(null, null, false);
}
@Override
protected void doPaint(Graphics2D g, JComponent c,
int width, int height, Object[] extendedCacheKeys) {
g.setColor(Color.MAGENTA);
g.fill(new Rectangle(0, 0, width, height));
}
});
UIManager.getLookAndFeelDefaults().put("ComboBox[Focused+Pressed].backgroundPainter",
new javax.swing.plaf.nimbus.AbstractRegionPainter() {
@Override
protected AbstractRegionPainter.PaintContext getPaintContext() {
return new AbstractRegionPainter.PaintContext(null, null, false);
}
@Override
protected void doPaint(Graphics2D g, JComponent c,
int width, int height, Object[] extendedCacheKeys) {
g.setColor(Color.CYAN);
g.fill(new Rectangle(0, 0, width, height));
}
});
UIManager.getLookAndFeelDefaults().put("ComboBox[Focused].backgroundPainter",
new javax.swing.plaf.nimbus.AbstractRegionPainter() {
@Override
protected AbstractRegionPainter.PaintContext getPaintContext() {
return new AbstractRegionPainter.PaintContext(null, null, false);
}
@Override
protected void doPaint(Graphics2D g, JComponent c,
int width, int height, Object[] extendedCacheKeys) {
g.setColor(Color.RED);
g.fill(new Rectangle(0, 0, width, height));
}
});
}
}
} catch (Exception e) {
e.printStackTrace();
}
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
MyComboBox aCTF = new MyComboBox();
}
});
}
}
如果不使用 XxxRenderer 就什么都不会发生,然后可以为可编辑和不可编辑的 JComboBox(具有相同的颜色主题)创建漂亮的主题
我会看看 Seaglas L&F(注意需要在 JDK 1.6._Xxx 中编译)
import java.awt.*;
import java.util.Vector;
import javax.swing.*;
import javax.swing.UIManager;
public class MyComboBox {
private Vector<String> listSomeString = new Vector<String>();
private JComboBox someComboBox = new JComboBox(listSomeString);
private JComboBox editableComboBox = new JComboBox(listSomeString);
private JComboBox non_EditableComboBox = new JComboBox(listSomeString);
private JFrame frame;
public MyComboBox() {
listSomeString.add("-");
listSomeString.add("Snowboarding");
listSomeString.add("Rowing");
listSomeString.add("Knitting");
listSomeString.add("Speed reading");
someComboBox.setPrototypeDisplayValue("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
someComboBox.setFont(new Font("Serif", Font.BOLD, 16));
someComboBox.setEditable(true);
someComboBox.getEditor().getEditorComponent().setBackground(Color.YELLOW);
((JTextField) someComboBox.getEditor().getEditorComponent()).setBackground(Color.YELLOW);
editableComboBox.setPrototypeDisplayValue("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
editableComboBox.setFont(new Font("Serif", Font.BOLD, 16));
editableComboBox.setEditable(true);
JTextField text = ((JTextField) editableComboBox.getEditor().getEditorComponent());
text.setBackground(Color.YELLOW);
non_EditableComboBox.setPrototypeDisplayValue("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
non_EditableComboBox.setFont(new Font("Serif", Font.BOLD, 16));
frame = new JFrame();
frame.setLayout(new GridLayout(0, 1, 10, 10));
frame.add(someComboBox);
frame.add(editableComboBox);
frame.add(non_EditableComboBox);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocation(100, 100);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel("com.seaglasslookandfeel.SeaGlassLookAndFeel");
} catch (Exception e) {
e.printStackTrace();
}
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
MyComboBox aCTF = new MyComboBox();
}
});
}
}
关于java - 覆盖 Nimbus 颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24179643/
我是 Java Nimbus 外观和感觉 的新手。我正在尝试使用 Nimbus 的功能来自定义单个组件实例,使用 putClientProperty("Nimbus.Overrides", overr
我正在使用 java 7 update 17 和 netBeans IDE 7.3 我想将 nimbus 设置为每个 GUI 项目的默认值,但 Oracle 网站指令不起作用。 how to set
我正在制作一个程序,该程序应该尽可能独立于平台。我正在使用 Nimbus LAF。 我想知道,我应该担心字体吗?它的默认字体是包含在 LAF 本身中还是从平台获取? 最佳答案 这是默认的非系统字体。
如何自定义 Nimbus 外观中组件的图像?我想在 Photoshop 中创建图像并放置在一些 Nimbus 外观和感觉组件上,这些是我想要更改的组件: 滚动条按钮旋钮滚动条拇指滚动条滚动条轨道 谢谢
在 Nimbus 的网络照片应用程序示例中,当我返回表格(从 Facebook 或 Dribbble 中选择要下载照片的位置)时,我之前看到的和缓存中的照片都会从其中清除。有办法防止这种情况吗? 例如
我想更改选项卡标题的颜色。 我尝试设置不同的属性,但没有任何变化。 在我的 main 中,我将属性 text 设置为白色,因此所有文本都是白色的。 现在我想更改一些组件文本,例如选项卡 Pane ,但
我已经创建了一个文本字段组件,里面有一个“x”按钮,可以清除该字段的文本。它在使用 Window 的系统外观和感觉(对于 Windows)时效果很好,但是我在使用 Nimbus 时尝试获得正确的外观时
引用这个错误: 4848910 : UIManager only updates colors once 我的 JFrame 上有两个按钮。从第一个按钮开始,我将 LAF 颜色更改为 UIManage
所以,我的程序中有一个使用 Nimbus LAF 的 JTextArea。由于一些功能问题,我需要将其替换为 JTextPane。 但是,JTextArea 默认情况下有一个绘制的边框。 JTextP
我正在使用 NIPagingScrollView在 iPhone 上显示多个页面。 每次我翻到一个页面,下一页也被预先加载,这很好。 当我旋转 iPhone从纵向模式到横向模式,我让layoutSub
我正在使用 Nimbus显示带有擦洗器和可缩放 ImageView 的相册。我使用网络图像,并在加载最终图像之前显示缩略图。 NIPhotoAlbumScrollView提供方法didLoadPhot
我正在尝试覆盖 JLabel 的一些 nimbus 属性。 UIDefaults labelDefault = new UIDefaults(); labelDefault.put("Labe
刚接触java,有一个问题,无论我做什么似乎都无法解决,所以任何我们将非常感谢您的帮助。 我有一个Java小应用程序,并集成了一个弹出日历(WWW)使用 Nimbus 外观和感觉的可用。我的问题是我无
我正在尝试全局更改工具提示的大小。 我正在使用: UIManager.put("ToolTip.font", new Font("SansSerif",Font.PLAIN,25)); 总体来说效果很
我们正在使用 Nimbus LaF 开发一个 Swing 应用程序。我们更改了许多 Nimbus 默认设置(控件、文本、NimbusLightBackground 等)以拥有深色主题。 现在我们在渲染
我想验证表格单元格中的用户输入,并且我使用了 Nimbus 外观。这是验证整数输入的单元格编辑器的代码:WholeNumberField它扩展了 JTextField 并添加了输入验证。 当我为列设置
我想知道为什么 Nimbus会以某种方式与 Virtual keys 发生冲突.查看我在下面制作的示例: public class buttontest implements ActionLi
只是寻找一些文档。 最佳答案 Nimbus 基于 Synth,因此您应该从 Synth 的角度考虑对其进行自定义,这一点已有详细记录。 关于java - 有人有关于 Nimbus 外观和感觉定制的信息
我想更改选项卡选项卡的高度,为此我尝试了多种方法,但没有成功。首先,我尝试使用间隔图标,高度很好,但选项卡的文本不再居中。然后我尝试更改 Nimbus L&F 属性,TabbedPane.tabIns
我有例如 UIDefaults defaults = UIManager.getLookAndFeelDefaults(); defaults.put("text",Color.GREEN);` 文本
我是一名优秀的程序员,十分优秀!