2011-06-21 76 views
1

我想爲Java中的JToolBar設置漸變背景色。 能夠爲JPanel設置此漸變效果。JToolBar的漸變背景

感謝, Sathish所在

+5

你應該也**問一個問題。** –

回答

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)

+0

你好frnd它的工作很好。 – Sathish

+0

非常感謝。 – Sathish

+0

@Sathish,沒問題!我只是因爲你說你可以爲'JPanel'做到這一點,但不能爲'JToolBar'做這件事...... :) – mre