如何在java中为我们的渐变面板添加新颜色?
问问题
744 次
1 回答
3
从 JPanel 扩展您的面板并像这样覆盖它的paintComponent。
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
// Creates a two-stops gradient
GradientPaint p;
p = new GradientPaint(0, 0, new Color(0xFFFFFF),
0, getHeight(), new Color(0xC8D2DE));
// Saves the state
Paint oldPaint = g2.getPaint();
// Paints the background
g2.setPaint(p);
g2.fillRect(0, 0, getWidth(), getHeight());
// Restores the state
g2.setPaint(oldPaint);
// Paints borders, text...
super.paintComponent(g);
}
}
并且您看到可以更改现有颜色的颜色对象...
我建议你阅读
肮脏的富客户
并从某个地方得到这本书。它有更多有用的信息,你可以使用学习。
于 2010-02-22T08:05:07.417 回答