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

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

问题描述

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

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中使用null布局不赞成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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