Adding image to JFrame(将图像添加到 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将图像添加到 JFrame
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01