简体   繁体   中英

How to set a background image to JSlider?

I have an applet. I set its image background. It works fine.
Now I want to set a background image to a JSlider .
How can I do that?

You will need to create a custom JSlider class and override the paintComponent method. Be sure to call setOpaque(false) on your slider object.

public class CustomSlider extends JSlider
{
    private Image img = null;

    public CustomSlider()
    {
        try
        {
            img = ImageIO.read(new File("background.jpg"));
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }

    @Override
    public void paintComponent(Graphics g)
    {
        // Draw the previously loaded image to Component
        g.drawImage(img, 0, 0, null);
        super.paintComponent(g);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM