我是初学者,我正在尝试使用JLabal将背景图像添加到我的窗口中。据我所知,图像已添加到JLabel和add中以显示在JFrame上。
但是,它不起作用。
非常感谢您为解决问题所提供的帮助。public class frame extends JFrame {
frame(){
final String logo = "./img/icon.png";
final String bg = "./img/background.jpg";
//create window
setSize(1560, 1000);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setTitle("Reaction Speed Test");
setLayout(null);
//setBackground
ImageIcon background_image = new ImageIcon(bg);
JLabel background = new JLabel("", background_image, JLabel.CENTER);
background.setBounds(0, 0, 1560, 1000);
add(background);
//setIcon
try {
setIconImage(Toolkit.getDefaultToolkit().getImage(SpeedTest.class.
getResource(logo)));
} catch (Exception e) {
System.out.println("Can't find icon");
}
setVisible(true);
}
}
public class SpeedTest {
public static void main(String[] args) {
//create window
new frame();
}
}