繁体   English   中英

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

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

当我使用setBounds(); 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);

}

我认为布局管理器中的问题,但我找不到解决方案!

这是您正在使用的Layout的预期功能,即FlowLayout 如果您希望setBounds()setLayout(null);生效setLayout(null); 话虽如此,强烈建议不要使用空布局。 组件的放置和排列方式应留给布局管理器

将您的back JLabel更改为null布局。

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

注意第二个setBounds()命令。 您可以在l1上设置两个命令。

暂无
暂无

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

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