gpt4 book ai didi

java - JFrame - 组件忽略 Y 和 X,除了一个

转载 作者:行者123 更新时间:2023-12-01 22:51:12 25 4
gpt4 key购买 nike

所以,我有这个 JFrame 代码,其中一切似乎都很顺利,并且没有错误,但所有组件(除了一个组件)都忽略了 Y 和 X 轴。为什么会发生这种情况?我什至尝试插入一些极其荒谬的 Y 轴,例如 456789 甚至 -80,它保持不变。为什么?

我的代码:

    frame.setSize(750,200);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel panel = new JPanel();
frame.setContentPane(panel);

textField.setBounds(0,0,40,200);
textField.setVisible(true);
panel.add(textField);

button.setBounds(200, 0, 40, 200);
button.setVisible(true);
panel.add(button);

bInfo.setBounds(0,80,40,400);
bInfo.setVisible(false);
panel.add(bInfo);

label.setBounds(0,40,40,160);
label.setVisible(false);
panel.add(label);

registerButton();
registerMoreInfoButton();

frame.setVisible(true);

提前致谢! :)

最佳答案

JPanel 默认使用 FlowLayout。调用 setBounds() 和使用 LayoutManager 是互斥的。您要么使用 LayoutManager(覆盖您所做的任何 setBounds()),要么不使用 LayoutManager (setLayout(null)) 和 setBounds()。

首选方法是使用 LayoutManager。在这里了解如何操作http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

关于java - JFrame - 组件忽略 Y 和 X,除了一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24702121/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com