所以我浏览了很多关于这个的话题,但似乎没有一个能解决我的问题!当我尝试在 JLabel 或 JButton 上使用 setBounds() 或 setLocation() 时,它不起作用!似乎它们都是随机放置的。这是我的代码:
public static void main(String[] args) {
final JFrame frame = new JFrame(TITLE);
final JLabel fpsLabel = new JLabel("FPS: ERROR");
final JLabel fpsDone = new JLabel("FPS done: ERROR");
final JPanel contentPanel = new JPanel();
frame.setSize(WIDTH, HEIGHT);
frame.setContentPane(contentPanel);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fpsLabel.setLocation(WIDTH / 2, HEIGHT / 6);
fpsDone.setLocation(200, HEIGHT / 6);
frame.setJMenuBar(menubar);
frame.setResizable(false);
frame.add(fpsLabel);
frame.add(fpsDone);
frame.setVisible(true);
}
如果需要我可以添加图片。
SSCCE:
public static void main(String[] args) {
final JFrame frame = new JFrame("Example SSCCE");
final JLabel fpsLabel = new JLabel("FPS: ERROR");
final JLabel fpsDone = new JLabel("FPS done: ERROR");
final JPanel contentPanel = new JPanel();
final int HEIGHT = 400 / 16 * 9;
frame.setSize(400, HEIGHT);
frame.setContentPane(contentPanel);
fpsDone.setLocation(200, HEIGHT / 2);
fpsLabel.setLocation(200, HEIGHT / 2 + 50);
contentPanel.add(fpsDone);
contentPanel.add(fpsLabel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
我希望它看起来像什么(ASCII艺术?):
__________________________________
| |
| |
| |
| |
| |
| FPS: ERROR |
| FPS done: ERROR |
| |
| |
| |
| |
| |
__________________________________