将图片添加到JFrame中

将图片添加到JFrame中

本文介绍了将图片添加到JFrame中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我使用Eclipse与Windows建设者。我只是想知道是否有反正我可以导入将在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,这将重新present图像的位置。使用$ C $的几行自己的自定义图像组件,然后添加到在JPanel c您将永远不会有改变。他们应该是这样的:

  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是覆盖的paintComponent()方法来绘制图像的自我创建的类扩展的JComponent

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

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

08-23 13:06