简体   繁体   English

当我在JButton和JLabel中使用setBounds方法时,什么也没有

[英]Nothing happen When I use the method setBounds with JButton and JLabel

when i use setBounds(); 当我使用setBounds(); on JLabel or JButton nothing happen ? JLabelJButton什么都没有发生?

public Window() {

    this.setExtendedState(Window.MAXIMIZED_BOTH);
    this.setDefaultCloseOperation(Window.EXIT_ON_CLOSE);

    setUndecorated(true);          
    hideMoue();

    JLabel back=new JLabel(new ImageIcon("D:\\Java\\stone\\background.png"));
    add(back);
    back.setLayout(new FlowLayout());

    l1=new JLabel("Label");
    b1=new JButton("Button");
    l1.setBounds(483, 513, 400, 60);
    l1.setBounds(483, 443, 400, 60);
    back.add(l1);
    back.add(b1);

    setSize(1366,768);  

    setVisible(true);

}

i think the problem in the layout manager but i can not find the solution !! 我认为布局管理器中的问题,但我找不到解决方案!

That is the expected function for the Layout that you are using ie FlowLayout . 这是您正在使用的Layout的预期功能,即FlowLayout If you want setBounds() to take effect than setLayout(null); 如果您希望setBounds()setLayout(null);生效setLayout(null); Having said this it is highly recommended not to use null layout. 话虽如此,强烈建议不要使用空布局。 How the components are placed and arranged should be left with Layout Manager 组件的放置和排列方式应留给布局管理器

Change your back JLabel to null layout. 将您的back JLabel更改为null布局。

...
JLabel back=new JLabel(new ImageIcon("D:\\Java\\stone\\background.png"));
add(back);
back.setLayout(null);    
...

Keep an eye on your second setBounds() command. 注意第二个setBounds()命令。 You set both commands on l1 . 您可以在l1上设置两个命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM