关于 Java 声明式 GUI 编程的建议

发布于 2024-07-17 10:19:57 字数 830 浏览 8 评论 0原文

我想知道对于 Java 中的声明式 GUI 编程是否有任何建议。 (我讨厌基于视觉的 GUI 创建器/编辑器软件,但对手动实例化 JPanels 和 Boxes 以及 JLabels 和 JLists 等感到有点厌倦。)

这是我的总体问题,但对于我正在考虑采取的方法,我有两个具体问题:

  1. JavaFX:JavaFX 中是否有一个真实的 GUI 显示(例如,不是圆形和矩形,而是列表框、按钮和标签等)的示例,它可以与访问和更新各种元素的 Java 源文件交互?

  2. Plain Old Swing,带有一些解析 XUL-ish XML 的东西:是否有人为 XML 发明了一种声明性语法(如 XUL)以便与 Java Swing 一起使用? 我认为创建一些基于 STaX 的代码并不难,该代码读取 XML 文件,实例化 Swing 元素的层次结构,并使该层次结构可通过某种对象模型进行访问。 但我宁愿使用一些众所周知的、有文档记录和经过测试的东西,也不愿自己尝试发明这样的东西。

  3. 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:

  1. 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?

  2. 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.

  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(14

临走之时 2024-07-24 10:19:57

您可以查看 javabuilders; 它使用 YAML 构建 Swing UI。

来自 的简单示例手册 [PDF]:

JFrame:
    name: myFrame
    title: My Frame
    content:
        - JLabel:
            name: myLabel2
            text: My First Label
        - JLabel:
            name: myLabel2
            text: My Second Label

或者:

JFrame:
    name: myFrame
    title: My Frame
    content:
        - JLabel: {name: myLabel2, text: My First Label}
        - JLabel: {name: myLabel2, text: My Second Label}

或者甚至:

JFrame(name=myFrame,title=My Frame):
    - JLabel(name=myLabel2, text=My First Label)
    - JLabel(name=myLabel2, text=My Second Label)

You might have a look at javabuilders; it uses YAML to build Swing UIs.

A simple example from the manual [PDF]:

JFrame:
    name: myFrame
    title: My Frame
    content:
        - JLabel:
            name: myLabel2
            text: My First Label
        - JLabel:
            name: myLabel2
            text: My Second Label

Alternatively:

JFrame:
    name: myFrame
    title: My Frame
    content:
        - JLabel: {name: myLabel2, text: My First Label}
        - JLabel: {name: myLabel2, text: My Second Label}

Or even:

JFrame(name=myFrame,title=My Frame):
    - JLabel(name=myLabel2, text=My First Label)
    - JLabel(name=myLabel2, text=My Second Label)
风轻花落早 2024-07-24 10:19:57

作为 CookSwing(一个可以满足您需要的工具)的作者,我在实际实现之前对这个主题进行了长时间的仔细研究。 我以编写 Java Swing GUI 应用程序为生。

IMO,如果您打算使用任何类型的命令式编程语言来描述 Java Swing 组件,那么您最好只使用 Java。 Groovy 等只会增加复杂性,而没有太多简化。

声明性语言要好得多,因为即使是非程序员也能理解它,特别是当您需要将特定布局的微调任务委托给艺术家时。 XML 非常适合声明性语言(相对于其他选择),因为它简单、可读,并且有大量可用的编辑器/转换工具等。

以下是声明式 GUI 编程中面临的问题,排名不分先后。 这些问题已在 CookSwing 中得到解决。

  1. 可读性和简单性。 (JavaFX 并不比 XML 简单。XML 的结束标签对阅读有很大帮助,并且不会增加太多额外的输入,因为 XML 编辑器通常会为您做这件事)
  2. 可扩展性。 非常重要,因为任何重要的项目都会出现自定义 Swing 组件。
  3. GUI 布局。 也非常重要。 能够处理 BorderLayout、GridBagLayout、JGoodies FormsLayout 等实际上是必须的。
  4. 复制/粘贴的简单性。 在设计布局的过程中,需要尝试不同的布局。 因此,人们需要能够复制/粘贴和移动东西。 XML 更好,因为组件和布局的层次结构很容易看到。 由于多行属性和缩进问题,JavaFX 存在一些问题。 拥有一个好的编辑器是必须的,并且有很多好的 XML 编辑器。
  5. 模板(即能够包含另一个布局文件)对于一致的外观非常有用。 例如,人们可能希望对话框、按钮面板等具有一致的外观。
  6. 与 Java 代码的交互。 这一点至关重要。 某些 GUI 组件只能使用 Java 代码创建(无论出于何种原因)。 因此必须能够加载这些对象。 它还必须能够直接连接 XML 代码中的侦听器和其他 Java 对象/组件。 稍后使用 ids 来连接它们不会很好地工作,因为它非常乏味。
  7. 国际化(i18n)。 能够从资源包加载文本/字符串而不是硬编码文本。 此功能对于某些应用程序至关重要。
  8. 本地化(l10n)。 声明式编程(尤其是 XML)的优点是您可以针对特定区域设置切换到不同的 GUI 形式,仅此而已。 如果您使用 Java 或任何其他命令式语言进行编码,那就没那么容易了。
  9. 错误检查/容差。 最初的设计常常会存在一些错误。 有时错误可能是因为还没有设计相应的Java代码。 或者缺少图标资源。 使用命令式编码处理错误是极其乏味的。 因此,希望能够定位错误,同时具有容错能力,以便尽早预览 GUI 布局。
  10. GUI 组件替换。 也就是说,用一些更高级的组件版本替换过去具有 JTextField 的文本字段。 将dialog的含义替换为一些奇特的UI对话框(例如JIDE的)而不是JDialog。 此功能可以节省大量精力。 由于 XSLT 和其他转换工具,XML 本身也很有用。
  11. 超越摇摆。 因为迟早你会发现许多组件配置使用对象类型,例如数组、图标、图像、矢量等。

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.

  1. Readability and simplicity. (JavaFX is not any simpler than XML. Closing tags of XML helps reading quite a bit, and doesn't add extra typing much since XML editors usually do it for you)
  2. Extensibility. Very important, because custom Swing components will come up for any non-trivial projects.
  3. GUI layouts. Also very important. Being able to handle BorderLayout, GridBagLayout, JGoodies FormsLayout, etc are practically a must.
  4. Simplicity of copy/paste. In the course of the designing the layout, it is necessary to try out different ones. So one need to be able to copy / paste and moving things around. XML is better because the hierarchy of components and layouts are easy to see. JavaFX is somewhat problematic due to multi-line attributes and indentation issues. Having a good editor is a must, and there are plenty of good XML editors.
  5. Templates (i.e. being able to include another layout file) is very useful for consistent look. For example, one might want to have a consistent look of dialogs, button panels, etc.
  6. Interactions with Java code. This is crucial. Some GUI components can only be created with Java code (for whatever the reason). It is thus necessary to be able load these objects. It is also necessarily being able to directly hook up listeners and other Java objects/components within the XML code. Using ids to hook them up later WILL not work well, as it is very tedious.
  7. Internationalization (i18n). Being able to load text / string from a resource bundle rather than hard coded text. This feature can be crucial for some applications.
  8. Localization (l10n). The advantage of declarative programming (particularly with XML) is that you can just switch to a different GUI form for a specific locale and that's it. If you code with Java or any other imperative languages, it is not so easy.
  9. Error check / tolerance. Initial designs often will contain errors here and there. Sometimes the error might be because the corresponding Java code hasn't been designed yet. Or an icon resource is missing. Dealing with errors with imperative coding is extremely tedious. Thus it is desirable to be able to locate the errors, yet at the same time being error tolerant, so the preview of the GUI layout can be made as early as possible.
  10. GUI component replacement. That is, replace textfield which used to have JTextField with some fancier version of components. Replace the meaning of dialog with some fancy UI dialogs (such as JIDE's) instead of JDialog. This feature can save significant amount of efforts. XML itself is also useful due to XSLT and other transformation tools.
  11. Beyond Swing. Because sooner or later you will find many component configurations use object types such as arrays, icons, images, vectors, etc.
彡翼 2024-07-24 10:19:57

如果简洁性很重要,您可能需要考虑双括号习惯用法:

new JFrame("My Frame") {{
    setName("myFrame");
    add(new JLabel("My First Label") {{
         setName("myLabel2");
    }};
    add(new JLabel("My Second Label") {{
         setName("myLabel2");
    }};
}}

这样您就不会失去任何众所周知的通用编程语言的功能(您知道您将需要它,而 JellyTags 很糟糕)。 您所需要的只是一点额外的习语。

它的使用率并不高,因为实际上人们对 XML 的研究并没有解决真正的痛点。

一般来说,您可以使用构建器层来抽象重复的代码。 GUI 代码不一定写得不好,只是几乎所有代码都写得不好(包括教科书上的)。

If conciseness is important you might want to consider the double brace idiom:

new JFrame("My Frame") {{
    setName("myFrame");
    add(new JLabel("My First Label") {{
         setName("myLabel2");
    }};
    add(new JLabel("My Second Label") {{
         setName("myLabel2");
    }};
}}

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).

三五鸿雁 2024-07-24 10:19:57

我强烈推荐 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.

浴红衣 2024-07-24 10:19:57

如果您愿意稍微脱离普通 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.

怀中猫帐中妖 2024-07-24 10:19:57

尝试一下 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.

不喜欢何必死缠烂打 2024-07-24 10:19:57

SDL/Swing 正是您所需要的。 它是一个很小(283k)、不引人注目、易于学习的声明性 Swing 框架。

menus {
    "File" {
        "Open" do="open" // calls "open()" in the controller
        "---"
        "Exit" do="exit"
    }
}

SDL/Swing 是开源的,但享有商业支持。 我们 (Ikayzo.com) 经过多年的开发,并将其部署到从生命科学公司到银行等众多客户的生产系统中。

SDL/Swing does exactly what you need. Its a tiny (283k), unobtrusive, easy to learn declarative Swing framework.

menus {
    "File" {
        "Open" do="open" // calls "open()" in the controller
        "---"
        "Exit" do="exit"
    }
}

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.

套路撩心 2024-07-24 10:19:57

我可以找到您所要求的以下示例:

I can find the following examples of what you're asking for:

滿滿的愛 2024-07-24 10:19:57

我最近遇到了 SDL / Swing

I recently come across SDL / Swing.

如梦亦如幻 2024-07-24 10:19:57

一些新的东西...XWT,将包含在 eclipse e4 中

something new...XWT, will be included in eclipse e4

风流物 2024-07-24 10:19:57

我尝试过很多解决方案,例如 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

快乐很简单 2024-07-24 10:19:57

虽然它不是声明性的并且仅限于布局,但您可能想看看 DesignGridLayout它允许以非常简洁的方式以编程方式定义 Swing 布局(它是开源的)。

主要优点:

  • 简单易学、快速。
  • 简洁的代码(每行1行代码
    形式中的组件)也
    启用易于维护的
  • 编译时检查(其中
    声明式 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:

  • easy and quick to learn.
  • concise code (1 line of code per row
    of components in a form) that also
    enable easy maintenance
  • compile-time checking (which
    declarative UI can't have)
  • respect of platform look & feel
    (baseline alignment, gaps between
    components...) without any hard-coded
    length value
迷途知返 2024-07-24 10:19:57

通常,当您寻找某些东西时,执行搜索总是一个好主意。 这是 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"

猫七 2024-07-24 10:19:57

WindowBuilder,这是一个非常不错的插件,其中包括GWT、XWT、SWT、Swing ETC

WindowBuilder, it a very nice plugin, which included GWT,XWT,SWT,Swing etc

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文