Java JFrame 背景图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18777893/
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-12 10:57:37  来源:igfitidea点击:

JFrame background image

javaswingbackgroundjframe

提问by Aaron

I am creating a GUI, albeit a simple one, and I want to have a background image (2048 X 2048) fill up the whole window and a square to the left top corner where the occasional 64 X 64 image can be loaded. Thanks in advance to anyone who helps out :) Edit: I already know how to make the JFrame a set size, its the image loading I need help with.

我正在创建一个 GUI,虽然是一个简单的 GUI,但我想要一个背景图像 (2048 X 2048) 填满整个窗口和左上角的一个正方形,偶尔可以加载 64 X 64 图像。在此先感谢任何提供帮助的人 :) 编辑:我已经知道如何将 JFrame 设置为固定大小,这是我需要帮助的图像加载。

采纳答案by Nambi

This is a simple example for adding the background image in a JFrame:

这是在 JFrame 中添加背景图像的简单示例:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class BackgroundImageJFrame extends JFrame
{
    JButton b1;
    JLabel l1;

    public BackgroundImageJFrame()
    {
        setTitle("Background Color for JFrame");
        setSize(400,400);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);

        /*
        One way
        -----------------
        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);
        */

        // Another way
        setLayout(new BorderLayout());
        setContentPane(new JLabel(new ImageIcon("C:\Users\Computer\Downloads\colorful design.png")));
        setLayout(new FlowLayout());
        l1=new JLabel("Here is a button");
        b1=new JButton("I am a button");
        add(l1);
        add(b1);
        // Just for refresh :) Not optional!
        setSize(399,399);
        setSize(400,400);
    }

    public static void main(String args[])
    {
        new BackgroundImageJFrame();
    }
} 
  • Click herefor more info
  • 单击此处了解更多信息

回答by MadProgrammer

The best way to load an image is through the ImageIOAPI

加载图像的最佳方式是通过ImageIOAPI

BufferedImage img = ImageIO.read(new File("/path/to/some/image"));

There are a number of ways you can render an image to the screen.

有多种方法可以将图像渲染到屏幕上。

You could use a JLabel. This is the simplest method if you don't want to modify the image in anyway...

你可以使用一个JLabel. 如果您不想以任何方式修改图像,这是最简单的方法......

JLabel background = new JLabel(new ImageIcon(img));

Then simply add it to your window as you see fit. If you need to add components to it, then you can simply set the label's layout manager to whatever you need and add your components.

然后只需将其添加到您认为合适的窗口即可。如果您需要向其中添加组件,那么您可以简单地将标签的布局管理器设置为您需要的任何内容并添加您的组件。

If, however, you need something more sophisticated, need to change the image somehow or want to apply additional effects, you may need to use custom painting.

但是,如果您需要更复杂的东西,需要以某种方式更改图像或想要应用其他效果,则可能需要使用自定义绘画。

First cavert: Don't ever paint directly to a top level container (like JFrame). Top level containers aren't double buffered, so you may end up with some flashing between repaints, other objects live on the window, so changing it's paint process is troublesome and can cause other issues and frames have borders which are rendered inside the viewable area of the window...

第一个隐瞒者:永远不要直接在顶级容器(如JFrame)上绘画。顶级容器不是双缓冲的,因此您可能会在重绘之间出现一些闪烁,其他对象位于窗口上,因此更改它的绘制过程很麻烦并且可能导致其他问题,并且框架具有在可视区域内呈现的边框窗户的...

Instead, create a custom component, extending from something like JPanel. Override it's paintComponentmethod and render your output to it, for example...

相反,创建一个自定义组件,从类似JPanel. 覆盖它的paintComponent方法并将您的输出呈现给它,例如......

protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.drawImage(img, 0, 0, this);
}

Take a look at Performing Custom Paintingand 2D Graphicsfor more details

有关更多详细信息,请查看执行自定义绘画2D 图形

回答by bott

You can do:

你可以做:

setContentPane(new JLabel(new ImageIcon("resources/taverna.jpg")));

At first line of the Jframe class constructor, that works fine for me

在 Jframe 类构造函数的第一行,这对我来说很好用

回答by Riccorbypro

I used a very similar method to @bott, but I modified it a little bit to make there be no need to resize the image:

我使用了与@bott 非常相似的方法,但我对其进行了一些修改,以便无需调整图像大小:

BufferedImage img = null;
try {
    img = ImageIO.read(new File("image.jpg"));
} catch (IOException e) {
    e.printStackTrace();
}
Image dimg = img.getScaledInstance(800, 508, Image.SCALE_SMOOTH);
ImageIcon imageIcon = new ImageIcon(dimg);
setContentPane(new JLabel(imageIcon));

Works every time. You can also get the width and height of the jFrame and use that in place of the 800 and 508 respectively.

每次都有效。您还可以获取 jFrame 的宽度和高度,并分别使用它来代替 800 和 508。