1

我想在 Java 中为 JToolBar 设置渐变背景颜色。我能够为 JPanel 设置这种渐变效果。

谢谢, 萨西什

4

1 回答 1

1

与任何其他 Swing 组件一样,您必须覆盖其paintComponent(...)方法。例如,

@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();
}

如果您希望此渐变通过 上的组件显示JToolBar,则必须setOpaque(false)在每个组件上调用。

于 2011-06-21T12:02:14.690 回答