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


基础教程推荐
- Struts2 URL 无法访问 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- 如何对 Java Hashmap 中的值求和 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01