[英]JLabel with Image?
Honestly, I am not that familiar with GUI and I'm still having a study about it. 老实说,我对GUI并不那么熟悉,但我仍在对其进行研究。 My question is, how can I combine an image and a label?
我的问题是,如何合并图像和标签? I want to have an output that there's a text above the picture.
我想要输出图片上方有文字的信息。 Here's the code I got from a reference
这是我从参考中获得的代码
import java.awt.Container;
import java.awt.EventQueue;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
class JavaApplication2 extends JFrame {
public JavaApplication2(String title, String imageFileName) {
setTitle("title");
ImageIcon icon = new ImageIcon(imageFileName);
Container pane = getContentPane();
JLabel label = new JLabel(icon);
pane.add(label);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
JFrame f = new JavaApplication2("Orca","C:/Users/Balmaceda/Desktop/Naomi
/Capture.png"); //change pic here
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setVisible(true);
}
});
}
}
and this thing here: 这东西在这里:
JLabel label = new JLabel(icon);
everytine I change the (icon) into ("icon"), only the word icon is displayed on the window. 我将(icon)更改为(“ icon”),只有单词图标显示在窗口上。 The picture is no longer there.
图片不再存在。
What I want to happen is the word icon is above the picture. 我想发生的是图片上方有单词图标。
Start by taking a look at How to Use Labels and The JavaDocs for `javax.swing.JLabel 首先看一下如何 为`javax.swing.JLabel 使用标签和JavaDocs。
Basically, you have a number of options. 基本上,您有很多选择。
You can use 您可以使用
JLabel label = new JLabel("This is an Icon", icon, JLabel.CENTER);
You can use 您可以使用
JLabel label = new JLabel();
label.setText("This is an icon");
label.setIcon(icon);
You can use 您可以使用
JLabel label = new JLabel("This is an icon");
label.setIcon(icon);
You can use 您可以使用
JLabel label = new JLabel(icon);
label.setText("This is an icon");
To centre the text over the image you could use something like... 要将文字放在图像上居中,可以使用...
label.setVerticalTextPosition(JLabel.TOP);
label.setHorizontalTextPosition(JLabel.CENTER);
You will want to check the JLabel API for there you'll find the correct constructor, one with an Icon, a String and an int for horizontal alignment: 您将需要检查JLabel API ,在那里您将找到正确的构造函数,一个带有Icon,String和一个用于水平对齐的int的构造函数:
public JLabel(String text,
Icon icon,
int horizontalAlignment)
horizontalAlignment - One of the following constants defined in SwingConstants: LEFT, CENTER, RIGHT, LEADING or TRAILING.
horizontalAlignment-在SwingConstants中定义的以下常量之一:LEFT,CENTER,RIGHT,LEADING或TRAILING。
Another way to show images in JLabel, is to use HTML: 在JLabel中显示图像的另一种方法是使用HTML:
String imageInHtml = "<html> Image 1 </br> <img src = \"/absolute/path/to/file.jpg\" height = \"120\" width =\"50\"/> </html>";
JLabel l = new JLabel(imageInHtml);
this way you an also show easily multiple images with one label. 这样,您还可以轻松显示带有一个标签的多张图像 。
PS: PS:
This also works with images which are in a jar on the classpath. 这也适用于类路径中的jar中的图像。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.