为什么不赞成在 Swing 中使用空布局? [英] Why is it frowned upon to use a null layout in Swing?

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

问题描述

最近,我开始为我工作的公司创建一个程序.就像背景信息一样,我仍然是一名学生和初学者程序员,所以我的解决方案可能不被推荐,否则我不知道该怎么做,但它有效,我不会因为它而受到评判这是一份与编程完全无关的学生工作.

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.

该程序的特点是,它将在具有不同屏幕尺寸和分辨率(800x600 及更高)的多台不同计算机上运行.为了确保它尽可能多地占用屏幕而不丢失程序的任何部分,我将布局设置为 null 并使用相对值对所有内容进行硬编码.

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.

该程序是信息亭式的,我首先获取屏幕尺寸值并从那里开始(例如,在我的头顶,左侧菜单占据屏幕的八分之一,顶部栏 2%,等等.).我还使用字体度量来确保组件的大小正确,并且所有内容都能很好地显示.

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.

我的问题是:为什么不使用布局管理器而不是将布局设置为 null 如此令人不悦?(我在一些论坛上被告知这是一种可怕的做事方式)我知道布局管理器是如何工作的,并且知道如何使用不同的布局,但是对于这个程序的要求(多种不同的分辨率、自定义按钮形状和位置)、当您更改语言时组件上的文本也会更改等),我无法看到自己使用布局管理器来完成所有工作.

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?

推荐答案

如果您正确地将布局管理器分层,屏幕将为您重新流动到不同的尺寸,想法是在所有屏幕上使用一组布局管理器尺寸.

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.

如果您使用 null,则必须自己处理每个屏幕尺寸.不仅如此,如果应用程序可以窗口化,您必须支持它们可能滚动到的所有可能尺寸.

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.

有一些常见的技巧.BorderLayout 是一个很好的布局.有时您可能会在多个级别使用它——通常只有 2 或 3 个组件.那是因为它非常擅长为除一个区域之外的所有区域提供所需的最小区域,并将其他所有区域都提供给 CENTER.

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 可能很有用,但如果您的组件大小不同,那就很棘手了.

FlowLayout can be useful but it's tricky if your components are different sizes.

除非您打算编写代码来为您的布局管理器提供数据,否则我不会尝试 GridBagLayout(这是一个很好的解决方案!).

I wouldn't try GridBagLayout unless you are planning to write code to feed your layout manager (an excellent solution at that!).

我也不会使用 GUI 构建器,他们不知道您想要重排布局的整体方式.

I also wouldn't use GUI builders, they don't know the overall way you want to reflow your layout.

这篇关于为什么不赞成在 Swing 中使用空布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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