构建 Java Swing GUI 的最佳方式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2353964/
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 20:44:58  来源:igfitidea点击:

Best way to build Java Swing GUI?

javaswing

提问by Yatendra Goel

Till now, I had been using Netbeans Swing GUI generation feature to build GUI for my java applications.

到目前为止,我一直在使用 Netbeans Swing GUI 生成功能为我的 Java 应用程序构建 GUI。

Now I have started using Eclipse for my projects and have dropped Netbeans. So apart from coding manually all the code for GUI, what are the other ways with which I can build GUI quickly and in a much better way.

现在我已经开始在我的项目中使用 Eclipse 并且放弃了 Netbeans。因此,除了手动编写 GUI 的所有代码之外,还有哪些其他方法可以让我以更好的方式快速构建 GUI。

采纳答案by Hyman

You can keep coding it manually but using GroovyBuilders (link) that are enough faster than using plain java but you will need to embed Groovylib inside your application..

您可以继续手动编码,但使用 GroovyBuilders(链接)比使用普通 Java 快得多,但您需要在应用程序中嵌入Groovy库。

otherwise there exists a layout manager, called MIG Layout, that works quite well (it's much more practical that normal gui building)..

否则,存在一个名为MIG Layout的布局管理器,效果很好(它比普通的 gui 构建更实用)。

I also used a tool called JViderto build some simple applications, but using a visual approach you get always the same nasty thing: keeping interface and backend synchronized, considering also the "bindings" between visual objects and variables name..

我还使用了一个名为JVider的工具来构建一些简单的应用程序,但是使用可视化方法总是会遇到同样令人讨厌的事情:保持界面和后端同步,还要考虑可视对象和变量名称之间的“绑定”。

回答by YROjha

I have been using Visual Editorand Windows Builderplugin for eclipse IDE since last 2 years. I found Windows Builderworks perfect! It provides very clean and highly readable code for you. So i prefer to use Windows Builder, so that you can do everything what ever you found in Microsofts Visual Studio features "drag and drop and all".

自过去 2 年以来,我一直在为 Eclipse IDE使用Visual EditorWindows Builder插件。我发现Windows Builder工作完美!它为您提供了非常干净和高度可读的代码。所以我更喜欢使用 Windows Builder,这样你就可以做你在微软 Visual Studio 中找到的所有功能“拖放和所有”。

回答by Luhar

I ran into a similar situation, but discovered that NetBeans actually has a nifty 'import eclipse project' functionality. So, I ended up doing the visual GUI design work in NetBeans, but used Eclipse for coding, debugging and profiling. Netbeans adds some files to your project, and you may want to tweak the auto-code generation templates in NetBeans, but this is a solution that works for me. Of course, Eclipse also ships with its own visual GUI builder, but I prefer NetBeans for visual GUI design and development.

我遇到了类似的情况,但发现 NetBeans 实际上有一个漂亮的“导入 eclipse 项目”功能。因此,我最终在 NetBeans 中完成了可视化 GUI 设计工作,但使用 Eclipse 进行编码、调试和分析。Netbeans 向您的项目添加了一些文件,您可能想要调整 NetBeans 中的自动代码生成模板,但这是一个对我有用的解决方案。当然,Eclipse 还附带了自己的可视化 GUI 构建器,但我更喜欢 NetBeans 进行可视化 GUI 设计和开发。

Importing Eclipse projects in NetBeans.

在 NetBeans 中导入 Eclipse 项目。

The Eclipse Visual Editor project

Eclipse 可视化编辑器项目

回答by Jason

You may also consider Seesaw, which requires embedding Clojure.

您也可以考虑Seesaw,它需要嵌入Clojure

You're GUI code would then look something like:

您的 GUI 代码将如下所示:

(defn -main [& args]
  (invoke-later
    (-> (frame :title "Hello",
               :content "Hi there",
               :on-close :exit)
        pack!
        show!)))

Where "Hi there" is automatically "converted" to a JLabel, but instead you could put any Swing component for :content.

其中“Hi there”会自动“转换”为 JLabel,但您可以将任何 Swing 组件用于:content.

This more complex example from my seesaw-buttonssample project:

我的seesaw-buttons示例项目中的这个更复杂的示例:

(ns seesaw-buttons.core
  (:use seesaw.core))

(defn -main [& args]
  (invoke-later
    (let [open-action (action
                    :handler (fn [e] (alert "I should open a new something."))
                    :name "Open ..."
                    :key  "menu O"
                    :tip  "Open a new something something.")
      exit-action (action
                    :handler (fn [e] (.dispose (to-frame e)))
                    :name "Exit"
                    :tip  "Close this window")]
      (-> (frame :title "Hello",
                 :content (border-panel
                            :north (toolbar :items [open-action exit-action])
                            :center "More content here..."),
                 :on-close :exit)
          pack!
          show!))))

The advantage of Seesaw and Clojure is the elimination of a lot of boilerplate code and Seesaw has added some functionality and conventions that speed up development.

Seesaw 和 Clojure 的优点是消除了大量样板代码,并且 Seesaw 添加了一些功能和约定以加快开发速度。

回答by josefx

The Eclipse Visual Editor project mentioned by Luhar is quite nice, it can use any bean style swing or awt component and supports editing already existing classes since it relies on reflection instead of separate meta-data.
Also there are no limitiations as how you can manipulate the generated code as VEP will do its best to keep it when modifying the source file.
I would recommend a rather highend system if you want to use it for more complex components as it is quite resource hungry.
Nice things about VEP:

Luhar 提到的Eclipse Visual Editor 项目相当不错,它可以使用任何bean 样式的swing 或awt 组件,并且支持编辑已经存在的类,因为它依赖于反射而不是单独的元数据。
此外,对于如何操作生成的代码没有任何限制,因为 VEP 在修改源文件时会尽力保留它。
如果您想将它用于更复杂的组件,我会推荐一个相当高端的系统,因为它非常需要资源。
VEP 的优点:

  • Supports Swing/AWT/SWT
  • Uses the source-code and reflection no additional data required
  • Can be used with already existing components (see above)
  • Can edit code directly or in preview window
  • Generates clean looking code without breaking existing code
  • 支持 Swing/AWT/SWT
  • 使用源代码和反射不需要额外的数据
  • 可与现有组件一起使用(见上文)
  • 可以直接编辑代码或在预览窗口中编辑代码
  • 在不破坏现有代码的情况下生成干净的代码

Not so nice things:

不是很好的事情:

  • Slow, don't forget to pause the preview function when editing source code directly, it will try to refresh almost constantly.
  • Don't use the Preview window while you have compilation errors in your code, any changes made to the preview may cause a messed up edit in the source. You have to clean up any compilation errors before using it again.
  • 慢,直接编辑源代码时不要忘记暂停预览功能,它会尝试几乎不断刷新。
  • 不要在代码中有编译错误时使用预览窗口,对预览所做的任何更改都可能导致源代码中的编辑混乱。在再次使用它之前,您必须清除所有编译错误。

回答by tichy

Another approach is to use some XML library, like SwixML. To work with it requires some code changes but separates your UI from your code quite well. On the other hand, it is only a wrapper around the swing classes. This is IMHO both an advantage and a disadvantage. It is also a quite nice library for rapid prototyping, because you can (usually) try a dialog out until it is sufficient.

另一种方法是使用一些 XML 库,例如SwixML。使用它需要一些代码更改,但可以很好地将您的 UI 与您的代码分开。另一方面,它只是摆动类的包装器。恕我直言,这既是优点也是缺点。它也是一个非常好的快速原型库,因为你可以(通常)尝试一个对话框,直到它足够为止。

回答by Hermann Hans

The experience I've made is that Netbeans is pretty much as good as it gets for building Swing GUIs if you want to be able to just click your application together. Just like you however, I enjoy coding in eclipse a lot more than in Netbeans (personal preference, Netbeans is still great editor imo). For a while I ended up creating my GUIs in Netbeans and then just importing the projects into eclipse.

我的经验是,如果您希望能够一起单击您的应用程序,那么 Netbeans 与构建 Swing GUI 一样好。然而,就像你一样,我更喜欢在 Eclipse 中编码,而不是在 Netbeans 中(个人喜好,Netbeans 仍然是很棒的编辑器 imo)。有一段时间我最终在 Netbeans 中创建了我的 GUI,然后只是将项目导入到 eclipse 中。

This works alright, but in the end I think you'll come to the same conclusion I did, that it's just best to learn the layout managers and code the GUI by hand. Maybe take a look at the ones provided by JGoodiesas well if the ones provided by Sun just don't do it for you.

这行得通,但最后我想你会得出和我一样的结论,最好学习布局管理器并手工编写 GUI。如果 Sun 提供的那些不适合您,也许也可以看看JGoodies提供的那些。