Changing the background of JButton

前端 未结 3 2034
夕颜
夕颜 2021-01-14 18:36

I have a Swing JButton and I\'m also using the following code for my project:

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

3条回答
  •  天命终不由人
    2021-01-14 19:11

    You should make the JButton opaque:

    btnNewButton.setOpaque(true);
    

    As specified for JComponent#setBackground method in oracle documentation:

    Sets the background color of this component. The background color is used only if the component is opaque, and only by subclasses of JComponent or ComponentUI implementations. Direct subclasses of JComponent must override paintComponent to honor this property.

    It is up to the look and feel to honor this property, some may choose to ignore it.

    I think that the current look and feel does not support this property..That's why the background color is ignored in this case.

提交回复
热议问题