TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
程序运行图:

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