为什么在 Swing 中使用空布局会令人不悦?
最近,我开始为我工作的公司创建一个程序。正如背景信息一样,我仍然是一名学生和初学者程序员,所以我的解决方案可能不被推荐,而且我不知道如何做到这一点,但它有效,我不会因此而受到评判,因为这是一项与编程完全无关的学生工作。
该程序的问题是,它将在具有不同屏幕尺寸和分辨率(800x600 及以上)的多台不同计算机上运行。为了确保它占据尽可能多的屏幕而不丢失程序的任何部分,我将布局设置为 null 并使用相对值对所有内容进行硬编码。
该程序是信息亭风格的,我首先获取屏幕尺寸值并从那里开始(例如,从我的头顶开始,左侧菜单占据屏幕的八分之一,顶部栏 2% 等) 。我还使用字体规格来确保组件的大小正确并且所有内容都能很好地显示。
我的问题是:为什么让布局为空而不是使用布局管理器如此令人皱眉? (我在一些论坛上被告知这是一种可怕的做事方式)我知道布局管理器是如何工作的并且知道如何使用不同的布局,但是对于这个程序的要求(多个不同的分辨率,自定义按钮形状和位置) ,当您更改语言时组件上的文本发生更改等),我无法想象自己使用布局管理器来完成这一切。
在这种情况下,更有经验的程序员如何使用布局管理器?当您希望按钮位于特定位置而其他组件位于其他特定位置且与任何预定义布局并不真正匹配时,您该怎么办?
Recently, I started creating a program for the company I work for. Just as background info, I'm still a student and a beginner programmer, so my solution is probably not recommended and I didn't know how to do it otherwise, but it works and I'm not going to be judged for it because it's a student job totally unrelated to programming.
The thing about the program is, it's going to be run on multiple different computer with different screen sizes and resolutions (800x600 and up). To make sure it takes as much of the screen as possible without losing any part of the program, I set the layout to null and hard-coded everything using relative values.
The program is kiosk-style and I first get the screen size values and go from there (for example, off the top of my head, the left-side menu takes an eighth of the screen, the top bar 2%, etc.). I also use font metrics to make sure the components are sized correctly and that everything gets displayed nicely.
My question is: why is it so frowned upon to make the layout null instead of using the layout managers? (I was told on some forums that this is a horrible way of doing things) I know how the layout manager works and know how to use the different layouts, but for the requirements of this program (multiple different resolutions, custom button shapes and placements, text changing on the components when you change language, etc.), I couldn't see myself using the layout managers to do it all.
How do you more experienced programmers use the layout managers in a situation like this? And what do you do when you want a button to be somewhere specific and other components somewhere else specific that don't really match any of the predefined layouts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您正确地对布局管理器进行分层,屏幕将为您重新排列为不同的尺寸,其想法是在所有屏幕尺寸上使用一组布局管理器。
如果您使用 null,您将必须自己调整每个屏幕尺寸。不仅如此,如果应用程序可以窗口化,您还必须支持它们可能滚动到的所有可能的尺寸。
这有点难做到,但布局管理器就是为了做到这一点而设计的。
有一些常见的技巧。 BorderLayout 是一个很棒的布局。有时您可能会在多个级别上使用它——通常只有 2 或 3 个组件。这是因为它非常擅长为除一个区域之外的所有区域提供所需的最小面积,并将其他所有区域都交给中心。
FlowLayout 可能很有用,但如果您的组件大小不同,那就很棘手。
我不会尝试 GridBagLayout 除非您计划编写代码来为布局管理器提供数据(这是一个很好的解决方案!)。
我也不会使用 GUI 构建器,他们不知道您想要重排布局的总体方式。
If you layer the layout managers correctly the screen will re-flow to different sizes for you, the idea is to use a single set of layout managers on ALL screen sizes.
If you use null you will have to do each screen size yourself. Not only that but if the app can be windowed you have to support every possible size they might scroll to.
That's kind of difficult to do, but the layout mangers are designed to do just that.
There are some common tricks. BorderLayout is a great layout to start with. Sometimes you might use it at multiple levels--often with just 2 or 3 components in it. That's because it's really good at giving all but one area the minimum required area and giving everything else to the CENTER.
FlowLayout can be useful but it's tricky if your components are different sizes.
I wouldn't try GridBagLayout unless you are planning to write code to feed your layout manager (an excellent solution at that!).
I also wouldn't use GUI builders, they don't know the overall way you want to reflow your layout.
简而言之:因为您上面解释的所有工作都是由布局管理器完成的(或者至少:应该完成的)。
通常,当使用空布局时,它还意味着所有位置和大小都被硬编码为单个值,因此根本没有灵活性。这意味着窗口大小、语言、字体大小、显示密度或任何其他相关参数的更改对布局没有影响,并且您会得到通常的丑陋效果:窗口的空白部分;微小的、不可调整的列表;标签被切断的按钮; ...
听起来您所做的工作确实应该由布局管理器完成。 要么找到一个可以做到这一点的(我个人的建议是MiGLayout,它可以很多并且易于使用)或编写您自己的。
In a nutshell: because all the work that you explain above is done (or at least: should be done) by the layout manager.
More often than not, when a null layout is used, it also implies that all positions and sizes are hardcoded to a single value, so no flexibility at all is given. This means that changes in window size, language, font size, display density or any other related parameter have no effect on the layout and you get the usual ugly effects: empty parts of the window; tiny, unresizable lists; buttons with their labels cut off; ...
It sounds like the work you do should really be done by the Layout Manager. Either find one that does that (my personal suggestion would be MiGLayout, which does a lot and is easy to use) or write your own.
您实际上使用的是您自己的布局,以及所有复杂的位置计算。
您可以将这些逻辑移至自定义布局管理器类以安抚批评者。
You are practically using a layout - your own, with all your sophisticated calculations of positions.
You can move these logic to a custom layout manager class to pacify the critics.
嗯技巧应该是通过混合 LayoutMangers 并使用数量嵌套 JPanel,每个面板可以有不同的布局或不,实际上取决于 JComponents 的数量,它允许您创建看起来像使用 AbsoluteLayout 但具有相同布局的 GUI针对每个屏幕分辨率和比例(4:3、16:9、16:10)查看/输出到 GUI
hmmm trick should be by mixing LayoutMangers and by usage of numbers of nested JPanels that each could have diferrent Layout or not, really depends of number of JComponents, that allows you to create GUI that looks like as layed by using AbsoluteLayout but with same look/output to the GUI for every screen resolutions and ratio (4:3, 16:9, 16:10)