【问题标题】:Nothing happen When I use the method setBounds with JButton and JLabel当我将 setBounds 方法与 JButton 和 JLabel 一起使用时,什么也没有发生
【发布时间】:2015-10-29 04:23:45
【问题描述】:

当我在JLabelJButton 上使用setBounds(); 时,什么都没有发生?

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

}

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

【问题讨论】:

  • 你需要表达你的期望和实际发生的事情。
  • 您希望发生什么?为什么你有一个构造函数 Window() 并且你使用常量 Window.MAXIMIZED_BOTH,进一步你使用 setSize?如果您需要帮助,可以制作 SSCCE 吗?
  • 我想设计一个游戏的欢迎画面,但是当我使用 JLabel 作为背景时,我遇到了背景问题,其他 jlabel 和按钮不出现!
  • 你想要一个启动画面吗?
  • 是的,我想要 SplashScreen

标签: java layout background jbutton jlabel


【解决方案1】:

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

【讨论】:

  • 但是背景不会出现؟
  • 很抱歉不是背景,如果我将布局设置为 null,其他按钮和 jlabel 将不会出现!
【解决方案2】:

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

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

留意您的第二个setBounds() 命令。您在l1 上设置了这两个命令。

【讨论】:

    猜你喜欢
    • 2013-06-20
    • 2020-08-26
    • 1970-01-01
    • 2021-08-28
    • 2020-12-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多