[英]Image/text not showing in JFrame
I want to create a Window with an image and a text so far i've got this: 到目前为止,我想创建一个包含图像和文本的窗口:
public void ShowPng1() {
ImageIcon theImage = new ImageIcon("Icon_Entry_21.gif");
panel.setSize(270, 270);
JLabel label = new JLabel("Hello, World!");
JLabel imageLabel = new JLabel(theImage);
imageLabel.setOpaque(true);
panel.add(imageLabel);
panel.add(label);
panel.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel.setVisible(true);
}
My panel: 我的面板:
private JFrame panel = new JFrame();
For some reason it won't load nor image nor text, it just pops up as a white window. 由于某种原因,它既不会加载,也不会图像或文本,只会弹出为白色窗口。 What can be the problem? 可能是什么问题? I've also tried changing the format to .png
, didn't work. 我也尝试将格式更改为.png
,但没有用。
UPDATE 更新
import java.awt.BorderLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Img {
private JFrame panel = new JFrame();
public Img(){
ShowPng1();
}
public void ShowPng1() {
ImageIcon theImage = new ImageIcon("Icon_Entry_21.gif");
panel.setSize(300, 300);
panel.setResizable(false);
JLabel label = new JLabel("Hello, World!");
JLabel imageLabel = new JLabel(theImage);
imageLabel.setOpaque(true);
panel.add(imageLabel);
panel.add(label, BorderLayout.PAGE_END);
panel.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel.setVisible(true);
}
public static void main(String[] args) {
new Img();
}
}
I've managed to get this working, which is ridiculous because I can't figure out how to make it work with my program. 我设法做到了这一点,这很荒谬,因为我不知道如何使其与我的程序一起使用。 Reimeus gave me an idea on creating this script separately, the fix and that worked. Reimeus为我提供了一个单独创建此脚本的想法,此修复程序有效。 I will have to look through my entire program to see if I'm missing anything. 我将不得不浏览整个程序,以查看是否缺少任何内容。 Creating it in a separate class should work as well. 在单独的类中创建它也应该起作用。
it just pops up as a white window 它只是弹出一个白色的窗口
Sounds like you're blocking the EDT on startup. 听起来好像您在启动时阻止了EDT。 You may need to use one of Swing's concurrency mechanisms to solve it. 您可能需要使用Swing的并发机制之一来解决它。 Post a Minimal, Complete, Tested and Readable example so we can determine this for sure. 发布一个最小,完整,经过测试和可读的示例,以便我们可以确定。
In the meantime... 同时...
You're displacing the component containing the theImage
component in the BorderLayout.CENTER
location 您要在BorderLayout.CENTER
位置中theImage
包含theImage
组件的组件。
panel.add(label);
You could organize your labels so that they can appear simultaneously (placing the components at 2 different BorderLayout
locations will do) 您可以组织标签,以便它们可以同时显示(将组件放置在2个不同的BorderLayout
位置即可)
panel.add(imageLabel);
panel.add(label, BorderLayout.PAGE_END);
You should make a JPanel and add it to the frame, and then add the labels to the panel 您应该制作一个JPanel并将其添加到框架中,然后将标签添加到面板中
Something like 就像是
private JPanel panel = new JPanel;
and then add it to the frame in your method calling 然后将其添加到方法调用的框架中
frame.add(panel);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.