【问题标题】:how to keep GUIs from overlapping in Java如何防止 GUI 在 Java 中重叠
【发布时间】:2013-01-17 21:20:45
【问题描述】:

我正在尝试使用 Eclipse 创建 Java 7 通讯簿,当我测试运行 JFrame 时,JLabels 不可见并且 JButton 重叠。 (按钮会占据整个 JFrame) 例如

add(saveButton);
add(cancelButton);

add(headingLabel);

headingLabel.setVisible(true);

cancelButton.setLocation(200,200);
saveButton.setLocation(400,200);

cancelButton.setSize(200,50);
saveButton.setSize(200,50);

我做错了什么?

【问题讨论】:

  • 我们需要看更多的代码,你的布局风格是什么? JFrame是如何定义的?
  • 解决方案:了解和使用布局管理器。不要使用绝对定位。这个问题已经在这个网站上被询问和回答了很多次,而且答案没有变化。

标签: java eclipse swing user-interface jbutton


【解决方案1】:

您应该使用 LayoutManager 而不是绝对坐标。使用布局管理器,您不必太在意 GUI 元素的确切位置。布局已为您完成。

查看Visual Guide to Layout Managers.,它通过视觉和代码示例解释了最常见的布局类型。

【讨论】:

    猜你喜欢
    • 2015-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-09
    • 2013-01-15
    • 2010-12-26
    相关资源
    最近更新 更多