为什么空布局和绝对位置在 Swing 中是不好的做法? [英] Why null layouts and absolute positions are bad practice in Swing?

查看:35
本文介绍了为什么空布局和绝对位置在 Swing 中是不好的做法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有带有 null 面板的代码:

I have code with null panel:

JPanel thePanel = new JPanel();
thePanel.setLayout(null);

我使用了setBounds(x, y, width, heigth),例如这里:

label2.setBounds(150, 220, 459, 311);

我读到这不是一个好的做法,你能告诉我为什么吗?

I read that this is not a good practice, can you tell me why?

仅仅是因为当你想在一个组件和另一个组件之间添加一些东西时,你必须一次又一次地设置它们的位置还是别的什么?

Is it only because when you want to add something between one component and another you have to set them positions again and again or is it something else?

推荐答案

绝对定位存在多个问题:

There are multiple problems with absolute positioning:

  • 不同的屏幕尺寸和分辨率,在您的机器上看起来很棒的东西在具有不同分辨率的另一个屏幕上会有所不同.
  • 同样,当用户调整屏幕大小时会发生什么,因为他们想与其他应用程序并排运行您的应用程序(复制粘贴或其他)
  • 不同的语言环境和字体大小,当您使用其他语言环境或其他字体或更改字体大小时,您的标签会发生什么变化.

可能有更多原因,但使用布局管理器可确保在调整窗口大小或容器内容更改时重新分配内容......

There's probably more reasons, but using a layout manager makes sure that content is redistributed when windows are resized, or when content of a container changes, ...

一开始使用绝对定位可能是最简单的方法,但了解不同的布局管理器及其操作方式是值得的.它可以让您免去很多麻烦,例如需求变化.

Using absolute positioning is probably the easiest way in the beginning, but it pays to get to know the different layout managers and how they operate. It will save you from a lot of headaches due to, for example, changing requirements.

这篇关于为什么空布局和绝对位置在 Swing 中是不好的做法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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