简体   繁体   English

如何更改JCheckBox中聚焦环的颜色?

[英]How do I change the colour of the focus ring in a JCheckBox?

Our Swing GUI has a black panel with white controls. 我们的Swing GUI有一个带有白色控件的黑色面板。 However the JCheckBox instance on the panel always shows the focus ring in black. 但是,面板上的JCheckBox实例始终将聚焦环显示为黑色。 It seems to ignore the foreground colour when rendering the focus ring. 渲染聚焦环时,似乎忽略了前景色。 Here's an example, where I've set the content pane background to grey so that the focus ring is visible: 这是一个示例,其中我将内容窗格的背景设置为灰色,以便可以看到聚焦环:

在此处输入图片说明

Here's the code I'm using: 这是我正在使用的代码:

import javax.swing.*;
import java.awt.*;

public class ScratchSpace {

    public static void main(String[] args) throws Exception {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JCheckBox checkBox = new JCheckBox("Hello cruel world");
                checkBox.setForeground(Color.WHITE);
                checkBox.setOpaque(false);

                JPanel contentPane = new JPanel();
                contentPane.setOpaque(true);
                contentPane.setBackground(new Color(0.5f, 0.5f, 0.5f));
                contentPane.add(checkBox);

                JFrame frame = new JFrame();
                frame.setContentPane(contentPane);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}

How can I tell a JCheckBox to render the focus ring in a specific colour? 如何告诉JCheckBox将对焦环呈现为特定颜色? Ideally it would use the control's foreground colour. 理想情况下,它将使用控件的前景色。

You could try changing the look and feel property CheckBox.focus , note, doing this will effect ALL JCheckBox s... 您可以尝试更改外观属性CheckBox.focus ,请注意,这样做会影响所有JCheckBox s ...

在此处输入图片说明

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestCheckBox {

    public static void main(String[] args) {
        new TestCheckBox();
    }

    public TestCheckBox() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                UIManager.put("CheckBox.focus", Color.RED);

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new GridBagLayout());
                frame.add(new JCheckBox("Hello world"));
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM