以下代码属于示例——涉及事件监听
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.Jframe;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
public class HelloSwing {
public static void main(String[] args) {
Jframe jf=new Jframe("my joji");
JButton jb=new JButton("hiding joJi ");
jf.setSize(500,500);
jf.setLocation(200,200);
jf.setLayout(null);
final JLabel l=new JLabel();
ImageIcon i=new ImageIcon("C:\Users\Geek\Pictures\Saved Pictures\joji.png");
l.setIcon(i);
l.setBounds(50,50,i.getIconWidth(),i.getIconHeight());
jb.setBounds(150,200,100,30);
jb.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
l.setVisible(false);
}
});
jf.add(l);
jf.add(jb);
jf.setVisible(true);
jf.setDefaultCloseOperation(Jframe.DISPOSE_ON_CLOSE);
}
}
以上是测试类代码
细节部分:
ImageIcon i=new ImageIcon(“C:UsersGeekPicturesSaved Picturesjoji.png”);
路径——C:UsersGeekPicturesSaved Picturesjoji.png
其中joji.png 是要求显示的图片
其效果如图:
有意思的是 当joji.png 重命名为——才华.png 怎么运行都不显示
经过多次的实验 发现图片命名中一旦有中文 就不会显示
所以当使用ImageIcon类选用图片显示时 一定要留意其图片命名是否含有中文



