Change JButton gradient color, but only for one button, not all

后端 未结 3 1668
你的背包
你的背包 2020-11-30 11:43

I want to change JButton gradient color, i found this, http://java2everyone.blogspot.com/2009/01/set-jbutton-gradient-color.html, but i want to change gradient

3条回答
  •  情深已故
    2020-11-30 12:01

    A little improvement over mre answer:

    enter image description here

    private static final class JGradientButton extends JButton{
        private JGradientButton(String text){
            super(text);
            setContentAreaFilled(false);
        }
    
        @Override
        protected void paintComponent(Graphics g){
            Graphics2D g2 = (Graphics2D)g.create();
            g2.setPaint(new GradientPaint(
                    new Point(0, 0), 
                    getBackground(), 
                    new Point(0, getHeight()/3), 
                    Color.WHITE));
            g2.fillRect(0, 0, getWidth(), getHeight()/3);
            g2.setPaint(new GradientPaint(
                    new Point(0, getHeight()/3), 
                    Color.WHITE, 
                    new Point(0, getHeight()), 
                    getBackground()));
            g2.fillRect(0, getHeight()/3, getWidth(), getHeight());
            g2.dispose();
    
            super.paintComponent(g);
        }
    }
    

提交回复
热议问题