当前位置: 首页 > 知识库问答 >
问题:

覆盖光晕颜色

翟柏
2023-03-14

我们正在使用Nimbus LaF开发Swing应用程序。我们已经更改了许多Nimbus默认值(控件、文本、NimbusLightbackground等)以具有黑暗主题。

现在,我们在呈现JLists和JComboxes时遇到了很大的麻烦,因为呈现程序显然使用了NimbusLightBackground颜色作为所选文本的前景。这会导致深蓝色背景上出现深灰色文本-不好。

我曾尝试通过UIManager全局覆盖Nimbus默认值(“ComboBox:\”ComboBox.listRenderer\“[Selected].textForeground”等)中任何适用的外观键。putDefault()和每组件重写,但无法得到任何更改。

即使是SwingX Highlighter似乎也无法覆盖这种行为,至少在combobox下拉列表中是如此。

关于如何为J(X)列表和J(X)组合框下拉列表设置所选文本的前景色,有什么想法吗?

我最新的每组件覆盖尝试:

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

和应用程序范围的灵气默认值:

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

所有这些都是用Java7U55实现的,尽管我怀疑这一点,因为似乎有相当一段时间没有人维护Swing/Nimbus。

PS:我当然读过这个问题和其他问题,但没有找到一个有效的答案。

编辑:这是一个演示问题的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()方法很容易解决问题。对于ComboBox,我还没有找到缺少自定义渲染器的方法。所以我在这里的主要重点是ComboBox。

共有2个答案

邢永安
2023-03-14

我不喜欢这个答案,我很乐意接受任何一个告诉我如何使用光环设置的答案

我最终做的是——如果我能找到更好的解决方案,我会很高兴改变的——是:

我创建了一个ListCellRenderer实现,它包装了DefaultListCellRenderer,并在isSelected参数为true时设置前景色。它很管用,但我一点也不喜欢它,原因如下:

  1. 其他所有自定义渲染器(谢天谢地,不是很多)都必须实现相同的攻击,这违反了DRY原则

不管怎样,这是为那些可能有同样问题的孤独流浪者编写的代码。

import java.awt.Color;
import java.awt.Component;

import javax.swing.DefaultListCellRenderer;
import javax.swing.JList;
import javax.swing.ListCellRenderer;


@SuppressWarnings("rawtypes")
public class ThemeCompliantListCellRenderer implements ListCellRenderer {

private ListCellRenderer wrappedRenderer = new DefaultListCellRenderer();
private Color textColor = new Color(0xFFFAFA); 

@Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
    @SuppressWarnings("unchecked")
    Component c = wrappedRenderer.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
    if (isSelected) {
        c.setForeground(textColor);
    }
    return c;
}

public void setSelectedForeground(Color color) {
    textColor = color;
}
}
古文康
2023-03-14
  • 看看发生了什么,Win8.164b,Java7,JDK 1。7_021,通过使用全矩形画家,
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

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

  •  类似资料:
    • 通过RGB值设置光晕的颜色。 光晕的默认颜色是 0xFFFFFF: // 颜色的参数可以是字符串 "#FF0000" 或者是十六进制数值 0xFF0000 controller.setHaloColor("#FF0000"); //controller.setHaloColor(0xFF0000);

    • 光晕是在地球周围的光圈。光晕的默认颜色是0xffffff. 可以通过 configure() API来设置光晕的颜色,具体设置方法如下所示: controller.configure({         color: {                 halo:0xff0000         } }); 也可以通过 setHaloColor() API来动态修改光晕颜色。

    • Halos are light areas around light sources, used to give the impression of small dust particles in the air. 光晕(Halos)是光源周围的光线区域,常常用来表现在空气中的细小的灰尘颗粒的效果 A Light with a separate Halo Component 光线单独带有光晕组(H

    • 创建光晕 光晕工具创建具有明亮的中心、光晕和射线及光环的光晕对象。使用此工具可创建类似照片中镜头光晕的效果。 “光晕 ”包括中央手柄和末端手柄。使用手柄定位光晕及其光环。中央手柄是光晕的明亮中心 -光晕路径从该点开始。 光晕组件 A. 中央手柄 B. 末端手柄 C. 射线(为清晰起见显示为黑色) D. “光晕 ” E. “光环 ” 另请参阅 第 18 页的 “绘图工具库 ” 创建默认光晕 1选择光

    • 创建一个模拟追踪着灯光的镜头光晕。 代码示例 const light = new THREE.PointLight( 0xffffff, 1.5, 2000 ); const textureLoader = new THREE.TextureLoader(); const textureFlare0 = textureLoader.load( "textures/lensflare/lensfla

    • 我希望在Spring Ansi着色日志中使用不同的颜色来区分INFO、DEBUG和TRACE,因为它们目前都设置为绿色(见下表) 从这里的文档中https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-logging-color-coded-outpu