且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

向面板添加JLabel-如何正确布局组件?

更新时间: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 LayoutManagers