简体   繁体   English

图像未出现

[英]Image does not appear

Im trying to use JLabel to insert a image into my GUI. 我试图使用JLabel将图像插入到我的GUI中。 However it does not appear 但是它没有出现

Here is a partial of my code 这是我的部分代码

public FirstAid() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 700, 507);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(new BorderLayout(0, 0));

    tabbedPane = new JTabbedPane(JTabbedPane.TOP);
    contentPane.add(tabbedPane, BorderLayout.CENTER);

    panel = new JPanel();


ImageIcon imageIcon = new ImageIcon("src/method_1.png");
    JLabel label = new JLabel(imageIcon);

    panel.add(label);
    tabbedPane.addTab("name", null, panel, null);

You create a JPanel called panel, add a JLabel to it, but do nothing with the panel variable after this. 创建一个名为panel的JPanel,向其中添加一个JLabel,但是此后对panel变量不执行任何操作。 You must add it to your GUI for the JLabel and the image it might hold to be seen. 您必须将其添加到JLabel的GUI中,并保留可能可见的图像。 I'm guessing that you want to add panel to your JTabbedPane, but without more information, I can only guess. 我猜您想将面板添加到您的JTabbedPane中,但是如果没有更多信息,我只能猜测。

Edit: your edited question now shows that you're adding the JPanel to the JTabbedPane. 编辑:您编辑的问题现在表明您正在将JPanel添加到JTabbedPane。 If you're still not seeing the image, then the problem is likely in your reading in of the image. 如果仍然看不到图像,则可能是在读入图像时出现了问题。 Myself, I use ImageIO.read(...) and try to read the image in as an InputStream or as a URL. 我自己,我使用ImageIO.read(...)并尝试以InputStream或URL的形式读取图像。 Key to all of these methods is to make sure that you have the image path correct, and don't make any assumptions about how you might think that it is correct. 所有这些方法的关键是确保图像路径正确,并且不要对您认为它正确的方式做任何假设。 The only way to know for sure is to test it. 唯一可以确定的方法就是对其进行测试。

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

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