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

后端 未结 5 1051
情话喂你
情话喂你 2021-01-13 04:08

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 setB

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-13 04:56

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

提交回复
热议问题