将图像添加到 JFrame [英] Adding image to JFrame

查看:47
本文介绍了将图像添加到 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:

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

  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);

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

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

这篇关于将图像添加到 JFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆