简体   繁体   English

将图像添加到 JFrame

[英]Adding image to JFrame

So I am using Eclipse with Windows builder.所以我将 Eclipse 与 Windows 构建器一起使用。 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.我只是想知道是否有任何我可以导入将显示在 JFrame 上的图像,我可以轻松地四处移动并重新调整大小,而不是设置位置和大小并绘制它。

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

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

There is no specialized image component provided in Swing (which is sad in my opinion). Swing 中没有提供专门的图像组件(我认为这很可悲)。 So, there are a few options:所以,有几个选择:

  1. As @Reimeus said: Use a JLabel with an icon.正如@Reimeus 所说:使用带有图标的 JLabel。
  2. Create in the window builder a JPanel, that will represent the location of the image.在窗口构建器中创建一个 JPanel,它将表示图像的位置。 Then add your own custom image component to the JPanel using a few lines of code you will never have to change.然后使用几行代码将您自己的自定义图像组件添加到 JPanel,您将永远不必更改。 They should look like this:它们应该如下所示:

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

    where JImageComponent is a self created class that extends JComponent that overrides the paintComponent() method to draw the image.其中 JImageComponent 是一个自创建的类,它扩展了JComponent ,它覆盖了paintComponent()方法来绘制图像。

如果使用 Netbeans 开发,请使用 JLabel 并更改其图标属性。

As martijn-courteaux said, create a custom component it's the better option.正如 martijn-courteaux 所说,创建自定义组件是更好的选择。 In C# exists a component called PictureBox and I tried to create this component for Java, here is the code:在 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.如果要添加图像,请选择 JPictureBox,然后转到“属性”并找到“图标”属性并选择一个图像。 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:如果要更改 sizeMode 属性,则选择 JPictureBox,然后转到 Properties 并找到“sizeMode”属性,您可以选择一些值:

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

If you want to learn more about this topic, you can check this video .如果您想了解有关此主题的更多信息,可以查看此视频

Also you can see the code on Gitlab or Github .你也可以在GitlabGithub上看到代码。

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

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