什么原因不能显示有图标的button?
给button设置了一个图标,但是运行后不能显示,为什么呢?
如果不用setLayout(null);就可以显示,但是我要实现对button的移动,文档上说要实现就需要setLayout(null),结果现在连图标都看不到了.另外setLocation应该怎么用?
import javax.swing.*;
import java.awt.*;
public class ImageShow extends JFrame{
private JButton button;
private JPanel panel;
ImageIcon icon ;
ImageShow(){
button=new JButton();
panel=new JPanel();
panel.setLayout(null);
this.setSize(500,500);
icon=new ImageIcon(getClass().getResource("robot.gif"));
button.setIcon(icon);
this.getContentPane().add(panel);
panel.add(button);
setVisible(true);
}
public void move(int x,int y){
button.setLocation(200,500);
this.validate();
}
public static void main(String args[]){
ImageShow image=new ImageShow();
image.move(500,500);
}
}