我在stackoverflow中搜索过这个问题,然而,我无法解决我的问题。所以我有一个简单的图像,我分配给JLabel并将该标签添加到JFrame但它没有出现。有帮助吗? PS:如果我设置的图像与屏幕尺寸不一致,我该如何“重新调整”或适合重新缩放?
package electricscreen;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
/**
*
* @author MertKarakas
*/
public class ElectricScreen extends JFrame{
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int width = (int) screenSize.getWidth();
int height = (int) screenSize.getHeight();
private String url = "el.jpg";
private ImageIcon img;
private JLabel lbl;
public ElectricScreen() {
setLayout(new FlowLayout());
setSize(width, height);
setResizable(false);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
img = new ImageIcon(getClass().getResource("/electricscreen/el.jpg"));
lbl = new JLabel(img);
add(lbl);
lbl.setVisible(true);
}
public static void main(String[] args){
ElectricScreen e = new ElectricScreen();
}
}
答案 0 :(得分:3)
您正在向已经可见的while(1) {
good_matches.clear();
// other code ...
}
添加组件。在调用JFrame
之前添加组件(首选 - 您可能还需要调用setVisible
来布置组件)
pack
添加组件后,或在JFrame上调用add(lbl);
pack();
setVisible(true);
revalidate
答案 1 :(得分:0)
始终在构造函数的末尾添加框架的setvisible(true);