本文共 911 字,大约阅读时间需要 3 分钟。
代码如下
package com.liuyanzhao; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.WindowConstants; public class Demo3 extends JFrame { public void GUI() { setTitle("图像测试"); JPanel panel = new JPanel(); JLabel label = new JLabel(); ImageIcon img = new ImageIcon("images/logo.jpg");// 创建图片对象 label.setIcon(img); panel.add(label); add(panel); setExtendedState(JFrame.MAXIMIZED_BOTH);// JFrame最大化 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 让JFrame的关闭按钮起作用 setVisible(true);// 显示JFrame } public static void main(String args[]) { Demo3 d = new Demo3(); d.GUI(); } }
其中 images/logo.jpg 是图片的路径,记得images是项目(Project)目录下的文件夹,跟src同级的。具体路径问题可以参考这篇文章
本文链接:
转载地址:http://nsuu.baihongyu.com/