问题描述
import javax.swing.;
import java.awt.event.;
import java.awt.*;
class login extends JFrame {
public static void main(String args[]) {
JFrame f1;
JLabel l1,l2,l3;
JButton sign,cancle;
f1 =new JFrame();
l1= new JLabel("hi");
f1.add(l1);
l1.setBounds(170,40,75,25);
f1.setSize(500,500);
f1.setVisible(true);
f1.setLayout(null);
}
}
我无法使用 Setbounds 方法设置标签的位置,请给我一个解决方案..!!
I cant set position of a label with using Setbounds method kindly give me a solution for this..!!
推荐答案
f1.setVisible(true);
当你调用 setVisible(true)
时,会调用框架的布局管理器,并根据布局管理器的规则给组件一个大小和位置,默认情况下是一个 BorderLayout.
When you invoke setVisible(true)
, the layout manager of the frame is invoked and the components are given a size and location based on the rules of the layout manager, which by default is a BorderLayout.
setLayout(null) 语句对框架上已经可见的组件没有影响,因为您手动设置的边界已被布局管理器重置.
The setLayout(null) statement has no effect on components already visible on the frame because the bounds you manually set have been reset by the layout manager.
无论如何,不要尝试使用空布局.没有理由这样做.使用布局管理器.
Anyway, don't attempt to use a null layout. There is no reason for doing this. Use layout managers.
这篇关于为什么我不能在 java swing 中使用 setbounds 方法设置标签的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!