开发者

What is the proper way to get the current look-and-feel's component background color in Swing?

开发者 https://www.devze.com 2022-12-28 07:41 出处:网络
I am writing a custom Swing component with my own painting.I\'d like to query whether or not the current look and feel\'s components are opaque, and if so, what their background color is so my compone

I am writing a custom Swing component with my own painting. I'd like to query whether or not the current look and feel's components are opaque, and if so, what their background color is so my component 开发者_Python百科can use it too. I'm having a hard time finding that on Google. Anyone know? Thanks!


That's pretty simple:

public class MyComponent extends JComponent {

    public void paintComponent(Graphics g) {

        if (this.isOpaque()) {
            // Paint background
            g.setColor(this.getBackground());
            g.fillRect(0,0,this.getWidth(), this.getHeight());
        }

        g.setColor(this.getForeground());
        // Continue painting
    }
}


Don't really understand the question. Each component can have a different background color so what background color do you want your custom component to use?

I would guess generally speaking the background color of a LAF would be determined by a JPanel, so I guess your custom component could just extend JPanel and you don't have to worry about this.

If you want to query the default backgroud colors of every component then you can use the UIManager to look it up. See the UIManager Defaults example.

0

精彩评论

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

关注公众号