[英]Why cant I move JLabel Icon?
Why cant I change the x and y coordinates of the icon? 为什么不能更改图标的x和y坐标? All I really need is to add the image to the screen. 我真正需要的只是将图像添加到屏幕上。 Do I even need to use a JLabel? 我什至需要使用JLabel吗?
package bit;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class BIT extends JFrame
{
JLabel CL;
public BIT()
{
CL = new JLabel(new ImageIcon(this.getClass().getResource("final-image.jpg")));
CL.setBounds(0,0,100,100);
this.getContentPane().add(CL);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setBounds(5,5,1000,500);
this.setResizable(false);
this.setVisible(true);
}
public static void main(String[] args)
{
new BIT();
}
}
在使用setBounds添加控件之前,请取消设置JFrame的布局
this.setLayout(null);
You cannot set the x & y coordinates of the JLabel
as the JFrame
is using its default BorderLayout layout manager which arranges its components according to layout implementation. 您无法设置JLabel
的x和y坐标,因为JFrame
使用其默认的BorderLayout布局管理器,该管理器根据布局实现排列其组件。
setBounds
only works when using absolute positioning (or null
layout) and this option should be avoided(!) . setBounds
仅在使用绝对定位(或null
布局)时有效, 应避免使用此选项(!) 。
It appears you are trying to position the JLabel
in the top left-hand corner of the frame ( 0, 0
). 似乎您正在尝试将JLabel
放置在框架( 0, 0
)的左上角。 To do this you could: 为此,您可以:
PAGE_START
position 将标签添加到PAGE_START
位置 This would be: 这将是:
CL = new JLabel(new ImageIcon(...), JLabel.LEFT);
this.getContentPane().add(CL, BorderLayout.PAGE_START);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.