package MaybeUsed;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class ImageDemo extends JFrame{
ImageIcon img = null;
public ImageDemo() {
this.setSize(300,300);
URL imageURL = JFrame.class.getResource("/image/1.png");
img = imageURL != null ? new ImageIcon(imageURL)
: null;
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
System.exit(0);
}
});
JLabel label = new JLabel(img);
this.add(label);
this.setVisible(true);
}
public static void main(String[] args) {
new ImageDemo();
}
}
不多说,通过反射得到文件路径,然后New 一个panel,添加进去图片。
GUI添加图片
最新推荐文章于 2023-07-25 09:57:18 发布