在Eclipse 里面执行
package ZYM_GUI; import java.awt.Container; import java.net.URL; import javax.swing.*; public class ImageIconDemo extends JFrame { public ImageIconDemo() { JLabel jLabel = new JLabel("Image"); //ALT+回车 URL url=ImageIconDemo.class.getResource("logo.png"); ImageIcon imageIcon = new ImageIcon(url); jLabel.setIcon(imageIcon); jLabel.setHorizontalAlignment(SwingConstants.CENTER); Container contentPane = getContentPane(); contentPane.add(jLabel); setTitle("lesson16-ImageIconDemo"); setBounds(1, 1, 400, 150); setVisible(true); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new ImageIconDemo(); System.out.println(ImageIconDemo.class.getResource("logo.png")); } }
标签:lesson16,import,ImageIconDemo,new,jLabel,public From: https://www.cnblogs.com/zym97816/p/17787420.html