更新时间:2023-12-04 09:25:46
I'll point you to the Java tutorials for laying out components - they give a good introduction to Swing formatting and layouts.
您可能应该调用特定的LayoutManager
,例如setLayout(new FlowLayout());
,而不是调用setLayout(null)
,以便Java知道如何显示组件.在尝试将组件添加到JPanel
之前,应先调用此函数.您赋予setLayout()
方法的LayoutManager
类型将指示Java以特定方式绘制GUI,例如以Grid或仅此一种方式.上面链接的教程将解释所有这一切,并提供不同LayoutManager
s
Rather than calling setLayout(null)
, you should probably call a specific LayoutManager
, such as setLayout(new FlowLayout());
so that Java knows how to display your components. You should call this before you try to add the components to the JPanel
. The type of LayoutManager
you give to the setLayout()
method will instruct Java to draw the GUI in a specific way, such as in a Grid, or just one-after-the-other. The tutorial linked above will explain all this, and has a visual example of the different LayoutManager
s