简体   繁体   English

使用JLabel添加图形

[英]Add graphic using a JLabel

Is it correct to use JLabel always when you need to insert graphic or is other way? 当您需要插入图形或其他方式时,始终使用JLabel是否正确? Im using Swing. 我正在使用Swing。

ImageIcon icon = new ImageIcon("icon.png");
JLabel label = new JLabel(icon);
panel.add(label);

http://docs.oracle.com/javase/tutorial/uiswing/components/icon.html http://docs.oracle.com/javase/tutorial/uiswing/components/icon.html

There's always more than one way to insert a graphic. 插入图形总是有多种方法。 It also depends on how you want to use the graphic. 它还取决于您想要如何使用图形。 If its just for an Icon that is simplest and fastest way. 如果它只是一个最简单和最快的图标。

Normally, JLabel is the simplest method of showing an image, especially when you don't need to add any kind of effects, rotate or scale the image dynamically 通常, JLabel是显示图像的最简单方法,尤其是当您不需要添加任何类型的效果时,动态旋转或缩放图像

You can paint the image yourself using the 2D Graphics API and performing custom painting . 您可以使用2D Graphics API自己绘制图像并执行自定义绘制 This is some what more complex and I would consider what it is your want to achieve first and weigh it up with the complexity involved. 这是更复杂的一些,我会考虑你想要首先实现什么,并将其与所涉及的复杂性进行权衡。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM