简体   繁体   中英

How to set image as background in Java Spring GUI (JFrame)

I was thinking to set an image as background instead of a solid color. Is that possible?

Here are the codes of my MainApp.java

package com.gabriel.guiApp;

import java.awt.Color;
import javax.swing.JFrame;
import org.springframework.beans.factory.BeanFactory;  
import org.springframework.beans.factory.xml.XmlBeanFactory;  
import org.springframework.core.io.*;
import com.gabriel.guiImpl.Car;  

public class MainApp extends JFrame{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    public static void main(String[] args) {
        MainApp mainApp=new MainApp();
        mainApp.setTitle("Draw");
        mainApp.setBounds(10,10, 1200, 400);
        Draw draw= new Draw();
        draw.setBounds(0,0, 80,80);
        draw.setBackground(Color.red);
        draw.setForeground(Color.yellow);
        draw.init();
        mainApp.add(draw);
        
        ClassPathResource r= new ClassPathResource("ApplicationContext.xml");  
        BeanFactory factory=new XmlBeanFactory((org.springframework.core.io.Resource) r);  
        
        Car car=(Car) factory.getBean("car");
        
        draw.setCar(car);
        mainApp.setVisible(true);
    
    }
}

Please insert your solution to my code. Thank you!

I believe there's no helper method that does this for you. This type of question has been asked and answered before, so i would just share a link to the accepted answer. You could look at other examples on that thread too and then choose the one that best suits you.

https://stackoverflow.com/a/1466278/12796448

Look at this example too

https://www.tutorialspoint.com/how-to-add-background-image-to-jframe-in-java

I believe the best way to achieve what you want is to paint the background of your container with the desired image. Look at the links i shared, they might help.

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