简体   繁体   English

如何在 Java Spring GUI (JFrame) 中将图像设置为背景

[英]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这是我的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 https://stackoverflow.com/a/1466278/12796448

Look at this example too也看看这个例子

https://www.tutorialspoint.com/how-to-add-background-image-to-jframe-in-java 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.看看我分享的链接,它们可能会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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