向面板添加JLabel-如何正确布局组件? [英] Adding a JLabel to a Panel - how to layout the components properly?

查看:554
本文介绍了向面板添加JLabel-如何正确布局组件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在框架中添加一些单词,所以我使用了panel1.add(new JLabel("Hello"));

I want to put some words in the frame so I used panel1.add(new JLabel("Hello"));

但是有些按钮的大小和位置是自定义的.听说我们必须setLayout(null)进行按钮自定义,这也使标签不显示. (不确定是否是因为这个原因)

But there are some buttons which their sizes and positions are customized. I heard that we have to setLayout(null) for the button customizing and this is also making the label not showing up. (Not sure if it is because of this)

对此有什么解决方案?

推荐答案

我将带您到

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

这篇关于向面板添加JLabel-如何正确布局组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆