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

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

问题描述

我的代码带有空面板:

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

并且我使用了setBounds(x,y,width,heigth),例如:

and I used setBounds(x, y, width, heigth), f.e here:

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?

非常感谢您的答复,谢谢!;)

I'd be very grateful for answer, thanks! ;)

推荐答案

绝对定位存在多个问题:

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.

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

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