Java 将图像添加到 JFrame

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

Adding image to JFrame

javaimageswingbackgroundjframe

提问by user2612619

So I am using Eclipse with Windows builder. I was just wondering if there was anyway I can import an image that'll show up on the JFrame that I can easily move around and re-size instead of setting the location and size and drawing it.

所以我将 Eclipse 与 Windows 构建器一起使用。我只是想知道是否有任何方法可以导入将显示在 JFrame 上的图像,我可以轻松地四处移动并重新调整大小,而不是设置位置和大小并绘制它。

采纳答案by Martijn Courteaux

There is no specialized image component provided in Swing (which is sad in my opinion). So, there are a few options:

Swing 中没有提供专门的图像组件(我认为这很可悲)。所以,有几个选择:

  1. As @Reimeus said: Use a JLabel with an icon.
  2. Create in the window builder a JPanel, that will represent the location of the image. Then add your own custom image component to the JPanel using a few lines of code you will never have to change. They should look like this:

    JImageComponent ic = new JImageComponent(myImageGoesHere);
    imagePanel.add(ic);
    

    where JImageComponent is a self created class that extends JComponentthat overrides the paintComponent()method to draw the image.

  1. 正如@Reimeus 所说:使用带有图标的 JLabel。
  2. 在窗口构建器中创建一个 JPanel,它将表示图像的位置。然后使用几行代码将您自己的自定义图像组件添加到 JPanel,您将永远不必更改。它们应该如下所示:

    JImageComponent ic = new JImageComponent(myImageGoesHere);
    imagePanel.add(ic);
    

    其中 JImageComponent 是一个自创建的类,它扩展JComponent覆盖了paintComponent()绘制图像的方法。

回答by Rollyng

Here is a simple example of adding an image to a JFrame:

这是将图像添加到 的简单示例JFrame

frame.add(new JLabel(new ImageIcon("Path/To/Your/Image.png")));

回答by rishad2m8

If you are using Netbeans to develop, use jLabel and change it's icon property.

如果您使用 Netbeans 进行开发,请使用 jLabel 并更改其图标属性。

回答by ricardo130

As martijn-courteaux said, create a custom component it's the better option. In C# exists a component called PictureBoxand I tried to create this component for Java, here is the code:

正如 martijn-courteaux 所说,创建自定义组件是更好的选择。在 C# 中存在一个名为PictureBox的组件,我试图为 Java 创建这个组件,这是代码:

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JComponent;

public class JPictureBox extends JComponent {

    private Icon icon = null;
    private final Dimension dimension = new Dimension(100, 100);
    private Image image = null;
    private ImageIcon ii = null;
    private SizeMode sizeMode = SizeMode.STRETCH;
    private int newHeight, newWidth, originalHeight, originalWidth;

    public JPictureBox() {
        JPictureBox.this.setPreferredSize(dimension);
        JPictureBox.this.setOpaque(false);
        JPictureBox.this.setSizeMode(SizeMode.STRETCH);
    }

    @Override
    public void paintComponent(Graphics g) {
        if (ii != null) {
            switch (getSizeMode()) {
                case NORMAL:
                    g.drawImage(image, 0, 0, ii.getIconWidth(), ii.getIconHeight(), null);
                    break;
                case ZOOM:
                    aspectRatio();
                    g.drawImage(image, 0, 0, newWidth, newHeight, null);
                    break;
                case STRETCH:
                    g.drawImage(image, 0, 0, this.getWidth(), this.getHeight(), null);
                    break;
                case CENTER:
                    g.drawImage(image, (int) (this.getWidth() / 2) - (int) (ii.getIconWidth() / 2), (int) (this.getHeight() / 2) - (int) (ii.getIconHeight() / 2), ii.getIconWidth(), ii.getIconHeight(), null);
                    break;
                default:
                    g.drawImage(image, 0, 0, this.getWidth(), this.getHeight(), null);
            }
        }
    }

    public Icon getIcon() {
        return icon;
    }

    public void setIcon(Icon icon) {
        this.icon = icon;
        ii = (ImageIcon) icon;
        image = ii.getImage();
        originalHeight = ii.getIconHeight();
        originalWidth = ii.getIconWidth();
    }

    public SizeMode getSizeMode() {
        return sizeMode;
    }

    public void setSizeMode(SizeMode sizeMode) {
        this.sizeMode = sizeMode;
    }

    public enum SizeMode {
        NORMAL,
        STRETCH,
        CENTER,
        ZOOM
    }

    private void aspectRatio() {
        if (ii != null) {
            newHeight = this.getHeight();
            newWidth = (originalWidth * newHeight) / originalHeight;
        }
    }

}

If you want to add an image, choose the JPictureBox, after that go to Properties and find "icon" property and select an image. If you want to change the sizeMode property then choose the JPictureBox, after that go to Properties and find "sizeMode" property, you can choose some values:

如果要添加图像,请选择 JPictureBox,然后转到“属性”并找到“图标”属性并选择一个图像。如果要更改 sizeMode 属性,则选择 JPictureBox,然后转到 Properties 并找到“sizeMode”属性,您可以选择一些值:

  • NORMAL value, the image is positioned in the upper-left corner of the JPictureBox.
  • STRETCH value causes the image to stretch or shrink to fit the JPictureBox.
  • ZOOM value causes the image to be stretched or shrunk to fit the JPictureBox; however, the aspect ratio in the original is maintained.
  • CENTER value causes the image to be centered in the client area.
  • NORMAL 值,图像位于 JPictureBox 的左上角。
  • STRETCH 值会导致图像拉伸或收缩以适合 JPictureBox。
  • ZOOM 值导致图像被拉伸或收缩以适合 JPictureBox;但是,原始的纵横比保持不变。
  • CENTER 值使图像在客户区居中。

If you want to learn more about this topic, you can check this video.

如果您想了解有关此主题的更多信息,可以查看此视频

Also you can see the code on Gitlabor Github.

你也可以在GitlabGithub上看到代码。