用 Java 构建 GUI

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1126779/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 15:16:29  来源:igfitidea点击:

Building a GUI in Java

javauser-interface

提问by Tolga E

What mechanism do you prefer for building a GUI: from scratch or using GUI-building software?

您更喜欢哪种机制来构建 GUI:从头开始还是使用 GUI 构建软件?

采纳答案by Tedil

I actually enjoy building GUIs with the NetBeans GUI Builder; the thing is, it is fairly customizable - it allows you to change the code used for auto-generation and the auto-generated code [no pun intended] (which is necessary for custom components), it also allows "easy" event-handling and binding. And NetBeans GUI Builder is not restricted to GroupLayout but rather allows you to choose which LayoutManager to use (a somewhat hidden setting in the form's properties dialog).

我实际上很喜欢使用 NetBeans GUI Builder 构建 GUI;问题是,它是相当可定制的 - 它允许您更改用于自动生成的代码和自动生成的代码 [没有双关语](这是自定义组件所必需的),它还允许“简单”的事件处理和绑定。而且 NetBeans GUI Builder 不限于 GroupLayout,而是允许您选择要使用的 LayoutManager(在表单的属性对话框中有点隐藏的设置)。

But it is not always the best choice; as others already said: MiG Layout is a good choice when you need to have full control of your application, knowing every single JLabel, every JButton etc.

但这并不总是最好的选择;正如其他人所说:当您需要完全控制您的应用程序,了解每个 JLabel、每个 JButton 等时,MiG Layout 是一个不错的选择。

I must confess I prefer Netbeans' GUI builder for the ease of use and WYSIWYG view. But I also think that it does not even matter if you have created the GUI-Code by hand or with a GUI-Designer; adding Controls / custom Components dynamically works with both, doing this and that works with both etc.. The only thing that matters is that it works.

我必须承认我更喜欢 Netbeans 的 GUI 构建器,因为它易于使用且具有所见即所得的视图。但我也认为,你是手工还是用 GUI-Designer 创建了 GUI 代码都没有关系;添加控件/自定义组件动态地适用于两者,这样做和适用于两者等等。唯一重要的是它可以工作。

(From my point of view I do not see a point in coding a GUI manually if it can be done faster with a GUI-Builder, as I do not see why one should bother coding it by hand without some kind of benefit)

(从我的角度来看,如果使用 GUI-Builder 可以更快地完成,我认为手动编写 GUI 没有意义,因为我不明白为什么要在没有某种好处的情况下手动编写代码)

回答by 01es

If the question is about GUI development using Swing then IMO it is best to code everything manually without a WYSIWIG tool.

如果问题是关于使用 Swing 的 GUI 开发,那么 IMO 最好在没有 WYSIWIG 工具的情况下手动编码所有内容。

In order for a manual UI design/coding to be efficient a really good layout manager should be used. MigLayout is one of such layout managers. Since I've started using it I have never looked back at WYSIWIG tools -- a sheet of paper, a pencil, MigLayout and you get a full control over your design and code.

为了使手动 UI 设计/编码高效,应该使用非常好的布局管理器。MigLayout 就是这样的布局管理器之一。自从我开始使用它以来,我从未回头看所见即所得的工具——一张纸、一支铅笔、MigLayout,你就可以完全控制你的设计和代码。

Also tools like Balsamiqsimplify making GUI mock ups for quick prototyping if the paper and pencil approach is not suitable.

如果纸笔方法不合适,Balsamiq 等工具还可以简化 GUI 模型的制作,以进行快速原型制作。

回答by oxbow_lakes

My personal opinion is never use visual editors. Until there is a single standard for GUI-building in Java you will find yourself tied to an IDE. The best IDE for building Java GUI has switched around quite a bit in the last 10 years (I wonder how many projects were hit by a massive maintenance overhead from switching away from JBuilderfor example).

我个人的意见是永远不要使用可视化编辑器。在 Java 中出现 GUI 构建的单一标准之前,您会发现自己与 IDE 捆绑在一起。在过去的 10 年中,用于构建 Java GUI 的最佳 IDE 发生了很大变化(例如,我想知道有多少项目因从JBuilder切换而导致的大量维护开销受到影响)。

For most applications, the actual layout of GUI code is probably something like 5-10% of the total amount of time you are likely to spend writing the app as a whole. Therefore even halving this by using a designer is not really saving you very much in the grand scheme of things.

对于大多数应用程序,GUI 代码的实际布局可能大约占您编写整个应用程序可能花费的总时间的 5-10%。因此,即使通过使用设计师将其减半,也不能在宏伟的计划中真正为您节省很多。

Taking this together with the lack of flexibility and more complicated maintenance, it's almost always better just to write the GUI by hand.

再加上缺乏灵活性和更复杂的维护,手工编写 GUI 几乎总是更好。

回答by Joe Attardi

I use GUI builders to rapidly prototype designs and stuff like that. But when it comes down to building the actual production GUI, I code it from scratch. I'm not morally opposed to GUI builders, but I prefer the greater control over my code I get from writing it by hand.

我使用 GUI 构建器来快速原型设计和类似的东西。但是当涉及到构建实际的生产 GUI 时,我会从头开始编写代码。我在道德上并不反对 GUI 构建器,但我更喜欢通过手动编写代码来更好地控制我的代码。

Like any code generator tool, IMHO, you shouldn't use a GUI builder until you at least understand the concept - in this case, the Swing layout managers are particularly tricky.

与任何代码生成器工具一样,恕我直言,在您至少了解概念之前,您不应该使用 GUI 构建器 - 在这种情况下,Swing 布局管理器特别棘手。

回答by zedoo

If you give me the choice between NetBeans and by-Hand I'd definitely choose by-Hand. I was never really convinced by the NetBeans GUI builder's approach. At least the last time I used it, it relied on some proprietary sidecar files...

如果您让我在 NetBeans 和手动之间进行选择,我肯定会选择手动。我从未真正相信 NetBeans GUI 构建器的方法。至少我上次使用它时,它依赖于一些专有的 sidecar 文件......

回答by Maestro1024

Look at Visual Editorfor for Eclipse

查看Eclipse 的可视化编辑器

You will still want to understand what the editor is doing for you.

您仍然想了解编辑器为您做什么。

回答by Thorbj?rn Ravn Andersen

You need to know how Swing works to be able to make things work properly. Nothing like building by hand to learn that :)

您需要知道 Swing 如何工作才能使事情正常工作。没有什么比手工构建更能学习的了 :)

THEN the Netbeans GUI builder is rather nice.

那么 Netbeans GUI 构建器相当不错。

Edit: I would strongly recommend going through the Java Tutorial Swing Trail - http://java.sun.com/docs/books/tutorial/ui/index.html

编辑:我强烈建议您阅读 Java 教程 Swing Trail - http://java.sun.com/docs/books/tutorial/ui/index.html

回答by Brian Sweeney

Both - use a WYSIWYG editor to get the ball rolling, but at the end of the day you'll need to manipulate your controls and in some cases create them programmatically at run time. Eclipse has a variety of free editors as plugins. Go to eclipse.org and look through the plugins listing.

两者 - 使用 WYSIWYG 编辑器让球滚动,但在一天结束时,您将需要操纵您的控件,并且在某些情况下在运行时以编程方式创建它们。Eclipse 有多种免费的编辑器作为插件。转到 eclipse.org 并查看插件列表。

回答by Robbie

If you want a complex gui, then use something like netbeans to build it and create all of the events. If you're looking for a fairly simplistic gui, then building one by hand is still an option.

如果你想要一个复杂的 gui,那么使用类似 netbeans 的东西来构建它并创建所有事件。如果您正在寻找一个相当简单的 gui,那么手工构建仍然是一种选择。

回答by Stroboskop

My company uses Netbeans for GUIs and eclipse for the regular programming.
We have looked but we haven't found any other (free or at least cheap) tool that does GUIs as good as NetBeans.

我的公司将 Netbeans 用于 GUI,将 Eclipse 用于常规编程。
我们已经查看过,但我们还没有找到任何其他(免费或至少便宜的)工具可以像 NetBeans 一样处理 GUI。

I used to write them from scratch with a plain text editor...

我曾经用纯文本编辑器从头开始编写它们......