什么原因不能显示有图标的button?

suncs2001 2007-05-17 10:15:48


给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);
}

}
...全文
361 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
chunyou128 2007-05-25
  • 打赏
  • 举报
回复
关注中
tanlingyun 2007-05-22
  • 打赏
  • 举报
回复
setLocation只能设置位置,不能设置大小,还是用setBounds比较好.

public void setLocation(int x,
int y)将组件移到新位置。通过此组件父级坐标空间中的 x 和 y 参数来指定新位置的左上角。

参数:
x - 父级坐标空间中新位置左上角的 x 坐标
y - 父级坐标空间中新位置左上角的 y 坐标

public void setLocation(Point p)将组件移到新位置。通过点 p 来指定新位置的左上角。在父级坐标空间中给出点 p。

参数:
p - 定义新位置左上角的点,在此组件的父级坐标空间中给出
sun_shine_17 2007-05-22
  • 打赏
  • 举报
回复
很感谢你让我知道了icon=new ImageIcon(getClass().getResource("robot.gif"));这个方法~
awingsky 2007-05-19
  • 打赏
  • 举报
回复
同上
将布局管理器设成null的话,在JButton被加入JPanel前先要设置JButton的位置。

button.setBounds(new Rectangle(10,10, 30, 30));
而且,如果JButton太小,也不会显示图象的.
chenweionline 2007-05-17
  • 打赏
  • 举报
回复
将布局管理器设成null的话,在JButton被加入JPanel前先要设置JButton的位置。

button.setBounds(new Rectangle(10,10, 30, 30));

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧