开发者

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

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:

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

Unfortunately, the concept of "background color" is different when using the Windows LAF; the background color is the color drawn around the 开发者_开发技巧button:

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

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?


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.


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.


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)


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


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜