简体   繁体   English

JFrame 背景图像

[英]JFrame background image

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

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 here for more info单击此处了解更多信息

The best way to load an image is through theImageIO API加载图像的最佳方式是通过ImageIO API

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 .您可以使用JLabel This is the simplest method if you don't want to modify the image in anyway...如果您不想以任何方式修改图像,这是最简单的方法......

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

Instead, create a custom component, extending from something like JPanel .相反,创建一个自定义组件,从JPanel东西扩展。 Override it's paintComponent method and render your output to it, for example...覆盖它的paintComponent方法并将您的输出呈现给它,例如......

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

Take a look at Performing Custom Painting and 2D Graphics for more details有关更多详细信息,请查看执行自定义绘画2D 图形

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。

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 类构造函数的第一行,这对我来说很好用

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

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