如何在Java中保持GUI不重叠

Gal*_*are 1 java eclipse user-interface swing jbutton

我正在尝试使用Eclipse创建Java 7地址簿,当我测试运行JFrame时,JLabel不可见并且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);
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

Rof*_*ion 9

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

查看布局管理器可视指南.它通过可视化和代码示例解释了最常见的布局类型.