Java 在 JFrame 中设置背景图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1064977/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 23:01:00  来源:igfitidea点击:

Setting background images in JFrame

javaimageswingbackgroundjframe

提问by

Are any methods available to set an image as background in a JFrame?

是否有任何方法可以将图像设置为背景JFrame

回答by Michael Myers

There is no built-in method, but there are several ways to do it. The most straightforward way that I can think of at the moment is:

没有内置的方法,但有几种方法可以做到。目前我能想到的最直接的方法是:

  1. Create a subclass of JComponent.
  2. Override the paintComponent(Graphics g)method to paint the image that you want to display.
  3. Set the content paneof the JFrameto be this subclass.
  1. 创建 的子类JComponent
  2. 重写paintComponent(Graphics g)方法以绘制要显示的图像。
  3. 设置内容窗格JFrame是这个子类。

Some sample code:

一些示例代码:

class ImagePanel extends JComponent {
    private Image image;
    public ImagePanel(Image image) {
        this.image = image;
    }
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(image, 0, 0, this);
    }
}

// elsewhere
BufferedImage myImage = ImageIO.read(...);
JFrame myJFrame = new JFrame("Image pane");
myJFrame.setContentPane(new ImagePanel(myImage));

Note that this code does not handle resizing the image to fit the JFrame, if that's what you wanted.

请注意,此代码不处理调整图像大小以适合JFrame,如果这是您想要的。

回答by Savvas Dalkitsis

Try this :

尝试这个 :

import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;


public class Test {

    public static void main(String[] args) {
        JFrame f = new JFrame();
        try {
            f.setContentPane(new JLabel(new ImageIcon(ImageIO.read(new File("test.jpg")))));
        } catch (IOException e) {
            e.printStackTrace();
        }
        f.pack();
        f.setVisible(true);
    }

}

By the way, this will result in the content pane not being a container. If you want to add things to it you have to subclass a JPanel and override the paintComponent method.

顺便说一下,这将导致内容窗格不是容器。如果你想向它添加东西,你必须子类化一个 JPanel 并覆盖 paintComponent 方法。

回答by camickr

You can use the Background Panelclass. It does the custom painting as explained above but gives you options to display the image scaled, tiled or normal size. It also explains how you can use a JLabel with an image as the content pane for the frame.

您可以使用背景面板类。它执行如上所述的自定义绘画,但为您提供了显示缩放、平铺或正常大小的图像的选项。它还解释了如何使用带有图像的 JLabel 作为框架的内容窗格。

回答by yugaanks

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

class BackgroundImageJFrame extends JFrame
{

    JButton b1;
    JLabel l1;

    public BackgroundImageJFrame() {

        setSize(400,400);
        setVisible(true);

        setLayout(new BorderLayout());

        JLabel background=new JLabel(new ImageIcon("C:\Users\Computer\Downloads\colorful_design.png"));

        add(background);

        background.setLayout(new FlowLayout());

        l1=new JLabel("Here is a button");
        b1=new JButton("I am a button");

        background.add(l1);
        background.add(b1);
    }

    public static void main(String args[]) 
    {
        new BackgroundImageJFrame();
    }
}

check out the below link

查看以下链接

http://java-demos.blogspot.in/2012/09/setting-background-image-in-jframe.html

http://java-demos.blogspot.in/2012/09/setting-background-image-in-jframe.html