开发者

Gradient Background for JToolBar

开发者 https://www.devze.com 2023-03-14 14:34 出处:网络
I Want to set gradient background color for JToolBar in Java. Am able to set this 开发者_JAVA技巧gradient effect for JPanel.

I Want to set gradient background color for JToolBar in Java. Am able to set this 开发者_JAVA技巧gradient effect for JPanel.

Thanks, Sathish


Like any other Swing component, you must override its paintComponent(...) method. For instance,

@Override
protected void paintComponent(Graphics g){
    // Create the 2D copy
    Graphics2D g2 = (Graphics2D)g.create();

    // Apply vertical gradient
    g2.setPaint(new GradientPaint(0, 0, Color.WHITE, 0, getHeight(), Color.BLUE));
    g2.fillRect(0, 0, getWidth(), getHeight());

    // Dipose of copy
    g2.dispose();
}

If you want this gradient to show through the components on the JToolBar, you must invoke setOpaque(false) on each of those components.

0

精彩评论

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

关注公众号