将图像添加到 JFrame

编程入门 行业动态 更新时间:2024-10-11 17:30:08
本文介绍了将图像添加到 JFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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.

推荐答案

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

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

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

  • As @Reimeus said: Use a JLabel with an icon.
  • 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);

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

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

  • 更多推荐

    将图像添加到 JFrame

    本文发布于:2023-08-06 11:30:27,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1311987.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:图像   JFrame

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!