Why is it frowned upon to use a null layout in Swing?

前端 未结 4 1810
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-21 04:44

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 re

相关标签:
4条回答
  • 2020-11-21 05:22

    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.

    0 讨论(0)
  • 2020-11-21 05:23

    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.

    0 讨论(0)
  • 2020-11-21 05:26

    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)

    0 讨论(0)
  • 2020-11-21 05:28

    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.

    0 讨论(0)
提交回复
热议问题