Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在jlabel上设置图像?_Java_Swing_Jlabel_Imageicon - Fatal编程技术网

Java 如何在jlabel上设置图像?

Java 如何在jlabel上设置图像?,java,swing,jlabel,imageicon,Java,Swing,Jlabel,Imageicon,您好,我正在创建一个java桌面应用程序,其中我有一个JLabel,我想在JLabel上放置多个图像,还想在该标签上写一些文字 ImageIcon icon1 = new ImageIcon("Images/YourCompanyLogo.png"); ImageIcon icon2 = new ImageIcon("Images/Your.png"); // In init() method write this code jLabelYourCompanyLogo.setIc

您好,我正在创建一个java桌面应用程序,其中我有一个
JLabel
,我想在
JLabel
上放置多个图像,还想在该标签上写一些文字

  ImageIcon icon1 = new ImageIcon("Images/YourCompanyLogo.png");
  ImageIcon icon2 = new ImageIcon("Images/Your.png");
  // In init() method write this code
  jLabelYourCompanyLogo.setIcon(iconLogo);
我怎样才能做到这一点

我想在hello之后将这两个图标添加到
JLabel

提前谢谢

String path = "(Insert your path here)"
您必须将图像移动到项目中,这意味着项目中的所有代码和内容都保存在其中。 然后你创建另一个名为res的文件夹f.e,它代表ressource,保存你想加载的所有图片。 如果你有这个,它看起来像这样:

 String path = "/res/(Your picture name DONT FORGET .png or .jpg ending)"
图片应该是png或jpg

然后返回编码: 创建一个名为URL的新URL和其他内容

Url url;
ImageIcon ii;
Image image;
然后在类的构造函数中写下:

url = getClass().getResource(path);
ii = new ImageIcon(url);
image = ii.getImage();
然后,您需要调用第二个方法

public void paintComponent(Graphics g){
     g.drawImage(url,imageX,imageY,null);
}
在你班的最顶端,你导入了

import java.awt.Graphics;
import java.net.URL;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
您可以使用,它允许您将两个或多个图标组合成一个图标

或者,只需创建一个JPanel(使用适当的布局管理器)并向面板添加两个JLabel

还想在j标签上写些文字吗

  ImageIcon icon1 = new ImageIcon("Images/YourCompanyLogo.png");
  ImageIcon icon2 = new ImageIcon("Images/Your.png");
  // In init() method write this code
  jLabelYourCompanyLogo.setIcon(iconLogo);

请参阅:了解4种方法。

标签只能显示1个图标。将这两个图像制作成一个图像,或使用两个标签。我可以使用一个图像吗text@JBNizet或者重写其
paintComponent
方法,使其显示多个图像“我可以使用一个带文本的图像吗?”。对<代码>设置图标
setText
。使用
setHorizontalTextPosition
setVerticalTextPosition
设置文本相对于图标的位置。@peeskillet是的,我这样做了,但它不符合我的需要