做一个颜色渐变的Panel 站点:爱心种子小博士 关键字:做一个颜色渐变的Panel
|
做一个颜色渐变的Panel 程序运行图:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class GradientPanel extends JPanel {
public GradientPanel(LayoutManager lm) {
super(lm);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (!isOpaque()) {
return;
}
Color control = UIManager.getColor("control");
int width = getWidth();
int height = getHeight();
Graphics2D g2 = (Graphics2D) g;
Paint storedPaint = g2.getPaint();
g2.setPaint(new GradientPaint(0, 0, Color.blue, width, height,control));
g2.fillRect(0, 0, width, height);
g2.setPaint(storedPaint);
}
public static void main(String args[]){
JFrame jf=new JFrame("GradientPanelTest");
jf.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
GradientPanel gp=new GradientPanel(new BorderLayout());
gp.add("Center",new JLabel("GradientPanel"));
jf.getContentPane().add(gp);
jf.setSize(500,400);
jf.setVisible(true);
}
} |
|
|
|