Java UI布局选择

huangapple go评论83阅读模式
英文:

Java UI Layouts Choice

问题

问候各位,我在UI设计和这个社区都相对新,所以请对我宽容一些。

摘要: 我目前正在尝试使用Java Swing为本机桌面应用程序设计GUI。像大多数初学者一样,我目前正在使用Netbeans作为UI构建工具,以使流程更加简单化。我开始的过程是通过观看大量关于如何创建好看的UI的Youtube教程来启动的,我看到其中许多人使用了"AbsoluteLayout",所以我也这样做了。

努力: 我已经使用"AbsoluteLayout"构建了一个相当令人满意的UI,其中包括所有的JPanels和JFrames。问题是,我的“编程老手”朋友在检查UI时说,使用"AbsoluteLayout"是绝对不能接受的,而且在这个论坛和Oracle Java指南中也有类似的看法。我个人尝试过使用BorderLayout和FlowLayout混合来呈现我的UI,尽管这是一件我宁愿不去做的麻烦事情。

问题: 我可以使用GroupLayout(也称为Free Design吗?我注意到对于初学者来说,借助Netbeans的帮助,它相当容易使用(只需拖放,没有太多限制),或者它也有显著的缺点吗?如果使用GroupLayout来设计UI而不是标准的Border、Flow、Grid布局,是否会受到轻视(或被视为廉价)?

注意: 值得一提的是,Jframe是未装饰的,我不打算让用户有能力最大化或调整窗口大小(我知道这既残忍又懒惰)。

提前感谢!

英文:

Greetings all, I am relatively new to both UI designing and this community, so take it easy on me.

Summary: I am currently attempting to design a GUI for a native desktop application using Java swing. Like most beginners, I am currently using Netbeans as a UI builder to make the process easier, and I started my process by watching a whole load of Youtube tutorials on how to make a good-looking UI, and I saw many of them use "AbsoluteLayout" and so that's what I did as well.

Efforts: I have built quite a satisfactory UI with all JPanels and JFrames using AbsoluteLayout. The problem is that my "coding veteran" friend, upon inspection of the UI, said that it is absolutely unacceptable to use AbsoluteLayout, and a quick look on this forum and the Oracle Java guides reveals similar sentiments. I have personally tried to present my UI using a mix of BorderLayout and FlowLayout, although it is quite a hassle that I would prefer not to undertake.

Question: Could I use GroupLayout(aka Free Design I think)? I have noticed that it is quite easy to use for a beginner with the help of Netbeans (just drag n drop with not too many restrictions), or does it have a significant disadvantage as well? Is is looked down upon in some way (or considered cheap) if it is used to design a UI instead of the standard Border, Flow, Grid layouts?

Note: It is worth mentioning that the Jframe is undecorated and I do not plan on giving the user the ability to maximize or resize the window (both cruel and lazy of me I know).

Thanks in advance!

My current Gui with AbsoluteLayout:

Java UI布局选择

My GUI attempt with a mix of Flow and Borderlayout before considering Grouplayout

Java UI布局选择

答案1

得分: 1

使用布局管理器的重点在于让您在组合所有组件以及这些组件在调整窗口大小时应如何响应时,能够逻辑思考。即使此应用程序可能不会调整大小,但您需要了解这些信息以备将来使用。

然后,通常会从JFrame的默认边界布局开始进行布局,然后创建具有不同布局管理器的子面板。您还可以嵌套面板以实现所需的布局。

因此,首先阅读关于布局管理器的Swing教程,了解每个布局管理器的基础知识。

所以我看到:

  1. 从一个JPanel开始,您可以将其添加到BorderLayout.LINE_START。此面板可以使用垂直的BoxLayout。然后,您可以在每组组件之间添加“粘合剂”以提供间距,以及在每个组件之间添加“垂直支撑”。

  2. 然后创建另一个名为“center”的面板,再次使用BorderLayout,然后将此面板添加到BorderLayout.CENTER。此面板将有3个子面板:

  3. 向“center”面板添加一个面板,放置在BorderLayout.PAGE_START。此面板可能使用水平的BoxLayout或FlowLayout。

  4. 向“center”面板添加一个面板,放置在BorderLayout.CENTER。此面板可以使用GridBagLayout。

  5. 向“center”面板添加一个面板,放置在BorderLayout.PAGE_END。此面板将使用GridLayout。

是的,首次设计GUI可能需要更多时间,但从长远来看,这是花费的时间都是值得的,因为您在学习Java/Swing,而不是在学习如何使用IDE。您的代码将是可维护的,无论使用哪个IDE,而不会依赖于特定的IDE。可以使用IDE来帮助调试等,但不要让它生成任何依赖于IDE的代码。

英文:

The point of using layout managers is the is makes you think logically about how to group all your components and how those components should react when the frame is resized. Even though this application may not resize, you need to know this information for the future.

Then the layout is typically done by starting with the default BorderLayout of the JFrame and then creating child panel with different layout managers. You can also nest panels for your desired layout.

So start by reading the Swing tutorial on Layout Manager for the basics of each layout manager.

So I see:

  1. You start with a JPanel that you can add to the BorderLayout.LINE_START. This panel could use a vertical BoxLayout. You can then add "glue" between each group of components to give spacing as well as a "vertical strut" between each component in a group.

  2. Then you create another panel "center" that again uses a BorderLayout and add this panel to the BorderLayout.CENTER. This panel will have 3 more child panels:

  3. To the "center" panel add a panel to the BorderLayout.PAGE_START. This panel might use a horizontal BoxLayout or FlowLayout.

  4. To the "center" panel add a panel to the BorderLayout.CENTER. This panel could use a GridBagLayout.

  5. To the "center" panel add a panel to the BorderLayout.PAGE_END. This panel will use a GridLayout.

Yes, it will take a little more time the first few times you design your GUI, but in the long run it is time well spent since you are spending the time learning Java/Swing and not time learning how to use the IDE. You code will be maintainable no matter what IDE you use, instead of being dependent on a given IDE. Use the IDE to help debug etc. but don't let it generate any IDE dependent code.

huangapple
  • 本文由 发表于 2020年8月3日 21:03:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/63229970.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定