java摇摆中的图像定位

时间:2013-11-23 07:35:27

标签: java image swing jlabel

我有这段代码来添加图片

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

我得到的结果是enter image description here

我尝试将bounds更改为其他值,但图像位置没有变化。图像保持在同一位置。

我做错了什么?

2 个答案:

答案 0 :(得分:1)

您尚未将布局设置为框架。

note.setLayout(new FlowLayout());

这将有助于您在center中拖动图片(或您要添加的任何组件)。

答案 1 :(得分:1)

如果要通过setBounds()定位组件,则需要将容器的布局设置为null。见http://docs.oracle.com/javase/tutorial/uiswing/layout/none.html

但我建议使用带有约束的适当布局管理器。