简体   繁体   中英

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

Our Swing GUI has a black panel with white controls. However the JCheckBox instance on the panel always shows the focus ring in black. 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? 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...

在此处输入图片说明

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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