关于 Java 声明式 GUI 编程的建议
我想知道对于 Java 中的声明式 GUI 编程是否有任何建议。 (我讨厌基于视觉的 GUI 创建器/编辑器软件,但对手动实例化 JPanels 和 Boxes 以及 JLabels 和 JLists 等感到有点厌倦。)
这是我的总体问题,但对于我正在考虑采取的方法,我有两个具体问题:
JavaFX:JavaFX 中是否有一个真实的 GUI 显示(例如,不是圆形和矩形,而是列表框、按钮和标签等)的示例,它可以与访问和更新各种元素的 Java 源文件交互?
Plain Old Swing,带有一些解析 XUL-ish XML 的东西:是否有人为 XML 发明了一种声明性语法(如 XUL)以便与 Java Swing 一起使用? 我认为创建一些基于 STaX 的代码并不难,该代码读取 XML 文件,实例化 Swing 元素的层次结构,并使该层次结构可通过某种对象模型进行访问。 但我宁愿使用一些众所周知的、有文档记录和经过测试的东西,也不愿自己尝试发明这样的东西。
JGoodies Forms -- 不完全是声明性的,但有点接近& 我在 JGoodies Binding 上运气很好。 但他们的表单布局语法似乎有点神秘。
编辑:这里有很多很棒的答案! (我在上面添加了#3)我将特别感激听到你们在实际应用程序中使用这些框架之一的任何经验。
PS我确实尝试了一些谷歌搜索(“java gui declarative”),只是不太知道要寻找什么。
I wonder if there are any suggestions for declarative GUI programming in Java. (I abhor visual-based GUI creator/editor software, but am getting a little tired of manually instantiating JPanels and Boxes and JLabels and JLists etc.)
That's my overall question, but I have two specific questions for approaches I'm thinking of taking:
JavaFX: is there an example somewhere of a realistic GUI display (e.g. not circles and rectangles, but listboxes and buttons and labels and the like) in JavaFX, which can interface with a Java sourcefile that accesses and updates various elements?
Plain Old Swing with something to parse XUL-ish XML: has anyone invented a declarative syntax (like XUL) for XML for use with Java Swing? I suppose it wouldn't be hard to do, to create some code based on STaX which reads an XML file, instantiates a hierarchy of Swing elements, and makes the hierarchy accessible through some kind of object model. But I'd rather use something that's well-known and documented and tested than to try to invent such a thing myself.
JGoodies Forms -- not exactly declarative, but kinda close & I've had good luck with JGoodies Binding. But their syntax for Form Layout seems kinda cryptic.
edit: lots of great answers here! (& I added #3 above) I'd be especially grateful for hearing any experiences any of you have had with using one of these frameworks for real-world applications.
p.s. I did try a few google searches ("java gui declarative"), just didn't quite know what to look for.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
您可以查看 javabuilders; 它使用 YAML 构建 Swing UI。
来自 的简单示例手册 [PDF]:
或者:
或者甚至:
You might have a look at javabuilders; it uses YAML to build Swing UIs.
A simple example from the manual [PDF]:
Alternatively:
Or even:
作为 CookSwing(一个可以满足您需要的工具)的作者,我在实际实现之前对这个主题进行了长时间的仔细研究。 我以编写 Java Swing GUI 应用程序为生。
IMO,如果您打算使用任何类型的命令式编程语言来描述 Java Swing 组件,那么您最好只使用 Java。 Groovy 等只会增加复杂性,而没有太多简化。
声明性语言要好得多,因为即使是非程序员也能理解它,特别是当您需要将特定布局的微调任务委托给艺术家时。 XML 非常适合声明性语言(相对于其他选择),因为它简单、可读,并且有大量可用的编辑器/转换工具等。
以下是声明式 GUI 编程中面临的问题,排名不分先后。 这些问题已在 CookSwing 中得到解决。
As the author of CookSwing, a tool that does what you need, I've given this subject a long hard look before doing the actual implementation. I made a living writing Java Swing GUI applications.
IMO, if you are going to use any kind of imperative programming languages to describe Java Swing component, you might as well just use Java. Groovy etc only adds complications without much simplification.
Declarative languages are much better, because even non-programmers can make sense out of it, especially when you need to delegate the task of fine tuning of specific layouts to artists. XML is perfect for declarative languages (over other choices) because of simplicity, readability, and plenty of editors/transformation tools etc available.
Here are the problems faced in declarative GUI programming, not in any particular order. These issues have been addressed in CookSwing.
如果简洁性很重要,您可能需要考虑双括号习惯用法:
这样您就不会失去任何众所周知的通用编程语言的功能(您知道您将需要它,而 JellyTags 很糟糕)。 您所需要的只是一点额外的习语。
它的使用率并不高,因为实际上人们对 XML 的研究并没有解决真正的痛点。
一般来说,您可以使用构建器层来抽象重复的代码。 GUI 代码不一定写得不好,只是几乎所有代码都写得不好(包括教科书上的)。
If conciseness is important you might want to consider the double brace idiom:
You then don't lose any of the power of a well known general purpose programming language (you know you are going to need it, and JellyTags suck). All you need is the one little extra idiom.
It's not used very much, because actually people pissing around with XML weren't solving real pain points.
In general you can use builder layers to abstract repeated code. GUI code doesn't have to be badly written, it's just that almost all of it is (including in text books).
我强烈推荐 MiG Layout - 需要几天时间来习惯语法,但一旦你掌握了它,它有奇效。 我使用 JGoodies Forms 有一段时间了,Karsten 的构建器概念运行良好,但它有点神秘...... MiG 更容易上手,并产生非常简洁的代码。
I strongly recommend MiG Layout - it takes a few days to get used to the syntax, but once you've got it, it works wonders. I used JGoodies Forms for quite awhile, and Karsten's builder concept works well, but it is a bit cryptic... MiG is easier to pick up, and results in wonderfully concise code.
如果您愿意稍微脱离普通 Java,Groovy 的“构建器”概念与 GUI 配合得很好。 当然,您可以相当轻松地在 Groovy 和 Java 之间进行互操作。 有关详细信息,请参阅 Swing Builder 页面。
If you're willing to step slightly outside plain Java, Groovy's "builder" concept works pretty well with GUIs. Of course you can interop between Groovy and Java fairly easily. See the Swing Builder page for more information.
尝试一下 Swiby:http://swiby.codehaus.org/
“Swiby 是 Swing 和Ruby 用于真正丰富的分布式应用程序。”
换句话说,Swiby 是一种混合了 swing 和 ruby 的特定领域语言。
give Swiby a try: http://swiby.codehaus.org/
"Swiby is a blend of Swing and Ruby for truly rich distributed applications."
In other words Swiby is a domain specific language mixing swing and ruby.
SDL/Swing 正是您所需要的。 它是一个很小(283k)、不引人注目、易于学习的声明性 Swing 框架。
SDL/Swing 是开源的,但享有商业支持。 我们 (Ikayzo.com) 经过多年的开发,并将其部署到从生命科学公司到银行等众多客户的生产系统中。
SDL/Swing does exactly what you need. Its a tiny (283k), unobtrusive, easy to learn declarative Swing framework.
SDL/Swing is open source but enjoys commercial support. We (Ikayzo.com) developed it over a period of years and have deployed it in production systems for many customers ranging from life science companies to banks.
我可以找到您所要求的以下示例:
I can find the following examples of what you're asking for:
我最近遇到了 SDL / Swing。
I recently come across SDL / Swing.
一些新的东西...XWT,将包含在 eclipse e4 中
something new...XWT, will be included in eclipse e4
我尝试过很多解决方案,例如 SWIXML、Javabuilders、MigLayout、Cookswing。 我最终发现 javaFX 和 javaFX-Scenebuilder 是最好的、最快的解决方案、基于 XML 的 GUI 工具。 您想要场景生成器创建 GUI 的方式(使用拖放项目!)。 另外,它使用 CSS(层叠样式表)作为 GUI 主题。 我相信 Oracle,它是 Java 应用程序的最佳 GUI 工具。
在这里查看如何使用 scenebuilder 创建 javaFX 应用程序:
http://docs.oracle.com/javafx /scenebuilder/1/get_started/prepare-for-tutorial.htm#CEGJBHHA
I've tried many solutions, such as SWIXML, Javabuilders, MigLayout, Cookswing. I finally found the javaFX and javaFX-Scenebuilder the best an fastest solution, XML-based GUI tool. you'd like the way scenebuilder creates GUI (with drag & drop items!). plus, it uses CSS (Cascading Style Sheets) for the GUI theme. Jsut trust the Oracle, it's the best GUI tool for java applications.
take a tour for creating javaFX apps with scenebuilder, here:
http://docs.oracle.com/javafx/scenebuilder/1/get_started/prepare-for-tutorial.htm#CEGJBHHA
虽然它不是声明性的并且仅限于布局,但您可能想看看 DesignGridLayout它允许以非常简洁的方式以编程方式定义 Swing 布局(它是开源的)。
主要优点:
形式中的组件)也
启用易于维护的
声明式 UI 不能具有)
(基线对齐、之间的间隙
组件...)没有任何硬编码
长度值
Although it is not declarative and is limited exclusively to layouts, you might want to take a look at DesignGridLayout which allows to programmatically define Swing layouts in a very concise manner (it's open source).
Main advantages:
of components in a form) that also
enable easy maintenance
declarative UI can't have)
(baseline alignment, gaps between
components...) without any hard-coded
length value
通常,当您寻找某些东西时,执行搜索总是一个好主意。 这是 google 中寻找“java xml 图形用户界面"
As often, it's always a good idea to perform a search when you're looking for something. This is the first link in google while looking for "java xml gui"
WindowBuilder,这是一个非常不错的插件,其中包括GWT、XWT、SWT、Swing ETC
WindowBuilder, it a very nice plugin, which included GWT,XWT,SWT,Swing etc