当使用“setlayout(null)”时,小部件表现得很奇怪。 [英] Widgets behaving strangely when using "setlayout(null)"

查看:175
本文介绍了当使用“setlayout(null)”时,小部件表现得很奇怪。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的代码中进行以下调用:

I'm doing the following call in my code:

...
setLayout(null);
...

我正在尝试通过指定按钮来放置按钮和文本字段x和y坐标。

I'm trying to place a button and a textfield by specifying their x and y coordinates.

我运行程序时(使用Eclipse或BlueJ)的问题是我需要在面板上运行到按钮的位置和文本字段分别用于查看按钮和文本字段。

The problem when I run the program (either with Eclipse or BlueJ) is that I need to run on the panel up to the position of the button and the textfield in order to see respectively the button and the textfield.

当我找到文本字段时,它很小。只有当我开始写作时才假定我指定的大小。

When I find the textfield, it is small. Only when I start writing it assumes the size I specified.

有谁知道如何解决它?

推荐答案


  1. 避免使用setLayout(null),除非你有充分的理由。您可以在此处了解布局管理器: http://docs.oracle.com /javase/tutorial/uiswing/layout/using.html

如果您仍想使用空布局,则必须设置宽度和组件的高度,而不仅仅是它的x和y位置(参见setSize方法)。

If you still want to use a null layout, you have to set the width and height of the component, not just its x and y position (see the setSize method).

来自链接上面提到过:


虽然我们强烈建议你使用布局管理器,但你可以在没有它们的情况下执行布局
。通过将容器的布局属性
设置为null,可以使容器不使用布局管理器。使用这种名为绝对定位的
策略,您必须指定该容器中每个组件的大小和
位置。
绝对定位的一个缺点是,当
顶级容器调整大小时,它不能很好地调整。它也不能很好地适应用户和系统之间的
差异,例如不同的字体大小
和区域设置。

Although we strongly recommend that you use layout managers, you can perform layout without them. By setting a container's layout property to null, you make the container use no layout manager. With this strategy, called absolute positioning, you must specify the size and position of every component within that container. One drawback of absolute positioning is that it does not adjust well when the top-level container is resized. It also does not adjust well to differences between users and systems, such as different font sizes and locales.

这篇关于当使用“setlayout(null)”时,小部件表现得很奇怪。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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