我有这段代码来添加图片
JFrame note=new new JFrame();
JLabel label5=new JLabel();
label5.setIcon(new ImageIcon(searchresult.class.getResource("/images/expired.png")));
label5.setBounds(200,500,450,100);
note.add(label5);
我得到的结果是
我尝试将bounds
更改为其他值,但图像位置没有变化。图像保持在同一位置。
答案 0 :(得分:1)
您尚未将布局设置为框架。
note.setLayout(new FlowLayout());
这将有助于您在center
中拖动图片(或您要添加的任何组件)。
答案 1 :(得分:1)
如果要通过setBounds()
定位组件,则需要将容器的布局设置为null
。见http://docs.oracle.com/javase/tutorial/uiswing/layout/none.html
但我建议使用带有约束的适当布局管理器。