How to set the button color of a JButton (not background color)

此生再无相见时 提交于 2019-12-19 19:13:15

问题


I have a JButton that I would like to change the background color of to white. When using the Metal Look And Feel, I achieve the desired effect with setBackground:

Unfortunately, the concept of "background color" is different when using the Windows LAF; the background color is the color drawn around the button:

I would like to use the Windows LAF, but allow the button color of this JButton to be changed to white. How do I do this?


回答1:


You'll have to decide if it's worth the effort, but you can always create youe own ButtonUI, as shown in this example due to @mKorbel.




回答2:


I use JDK 6 on XP. It looks like the Window UI doesn't follow the normal painting rules in more ways than 1. As you noticed setBackground() doesn't work. You should be able to do custom painting by telling the component not to fill in the content area:

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

public class ButtonBackground extends JFrame
{
    public ButtonBackground()
    {
        setLayout( new FlowLayout() );

        JButton normal = new JButton("Normal");
        add(normal);

        JButton test1 = new JButton("Test 1")
        {
            @Override
            public void paintComponent(Graphics g)
            {
                g.setColor( Color.GREEN );
                g.fillRect(0, 0, getSize().width, getSize().height);
                super.paintComponent(g);
            }
        };
        test1.setContentAreaFilled(false);
        add(test1);
    }

    public static void main(String[] args)
    {
        try
        {
//          UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        }
        catch (Exception e2) {}

        ButtonBackground frame = new ButtonBackground();
        frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible(true);
    }
}

When you run the code as is it seems to work properly. That is when you click on the button you see the Border change. However is you run with the Windows XP LAF, the Border never changes to you don't see the button click effect.

Therefore, I guess the issue is with the WindowUI and you would need to customize the UI which is probably too complex to do so I don't have a solution.




回答3:


but I still think that (modified but by Darryl) is correct UIManager.get("Button.gradient"), because would be crossplatform

EDIT: correct answer would be - Nimbus or some Custom L&F, why reinvent the wheel (by Rob)




回答4:


Your best option is using SwingX:

JXButton allows you to set a Painter for the background with .setBackgroundPainter(Painter) using a MattePainter achieves exactly what you want. Having that JXButton extends from JButton, the changes are minimal in your code:

Color bg = new Color(new Random().nextInt(16777215)); // Random color

JButton button = new JButton();
button.setBackground(bg);

would become

JXButton button = new JXButton();
button.setBackgroundPainter(new MattePainter(bg));



回答5:


instead of messing with the button's background color, could you do whatever indication you're trying to show a different way?

displaying an Icon, making the button bold instead of plain text, etc.

Indicating something only through a different background color isn't always obvious, and depending on the user's system colors, may be jarring, or invisible.

for example, what if the user is running windows in "high contrast" mode?



来源:https://stackoverflow.com/questions/6256483/how-to-set-the-button-color-of-a-jbutton-not-background-color

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!