【问题标题】:Gradient Background for JToolBarJToolBar 的渐变背景
【发布时间】:2011-06-21 11:38:45
【问题描述】:

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

谢谢, 萨蒂什

【问题讨论】:

  • 你也应该问一个问题。

标签: java swing gradient jtoolbar


【解决方案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)

【讨论】:

  • @Sathish,没问题!我只是对您说您可以为JPanel 执行此操作,但无法为JToolBar 执行此操作的事实感到困惑...:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-31
  • 2011-03-15
  • 2012-12-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多