简体   繁体   English

在JLabel中添加图像但不响应

[英]Adding Image in JLabel but not Responding

I am trying to add image in JLabel but it's not working. 我正在尝试在JLabel中添加图像,但是它不起作用。 The second label is working but the first JLabel is not working. 第二个标签有效,但第一个JLabel不起作用。 Here is code. 这是代码。 Thanks in advance. 提前致谢。

import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Label;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class MainLabel {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        JFrame jframe;
        jframe = createFrame();

        ImageIcon ii = new ImageIcon("images.jpeg");

        JLabel label = new JLabel(ii);
        jframe.add(label);

        Label label123 = new Label("Be Nice to World!!");
        jframe.add(label123);

        jframe.setVisible(true);

    }

    static JFrame createFrame() {
        JFrame guiFrame = new JFrame();
        guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        guiFrame.setTitle("BorderLayout Example");
        guiFrame.setSize(700, 300);
        return guiFrame;
    }

}

Your label which contains the image is being replaced with label123 in the BorderLayout.CENTER position, which doesnt have any image attached. 你的label其中包含了图像被替换label123BorderLayout.CENTER位置,这可是没有附加任何图像。 You could use: 您可以使用:

label123.setIcon(ii);

If you want the 2 labels to be shown, you could place the text-based label123 in the SOUTH location: 如果要显示2个标签,可以将基于文本的label123SOUTH位置:

jframe.add(label123, BorderLayout.SOUTH);

Note: Use JLabel instead of Label . 注意:使用JLabel代替Label

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

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