登录站点

用户名

密码

兴趣联盟 - JavaSwing

  • 分享

    JAVA SWING JPanel实现两种颜色渐变

    2lyz 2010-07-23 15:39

    import java.awt.BorderLayout;

    import java.awt.Color;

    import java.awt.GradientPaint;

    import java.awt.Graphics;

    import java.awt.Graphics2D;

    import java.awt.LayoutManager;

     

    import javax.swing.JFrame;

    import javax.swing.JPanel;

    import javax.swing.WindowConstants;

     

    public class GradientPanel extends JPanel {

     

        public GradientPanel(LayoutManager lm) {

            super(lm);

        }

        public void paintComponent(Graphics g) {

            super.paintComponent(g);

            if (!isOpaque()) {

                return;

            }

           

            int width = getWidth();

            int height = getHeight();

            Graphics2D g2 = (Graphics2D) g;

            GradientPaint gradientPaint =new GradientPaint(0, 0, Color.blue, width/2, height/2,Color.red,false);

           

           

            g2.setPaint(gradientPaint);

            g2.fillRect(0, 0, width, height);

           

        }

       

       

        public static void main(String args[]){

               JFrame jf=new JFrame("GradientPanelTest");

               jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

               GradientPanel gp=new GradientPanel(new BorderLayout());

               jf.getContentPane().add(gp);

               jf.setSize(500,400);

               jf.setVisible(true);

        }

    }

你还不是该群组正式成员,不能参与讨论。 现在就加入