如何动态构建gui

发布于 2024-09-02 09:26:43 字数 1429 浏览 3 评论 0原文

目前我正在构建一个用于某些声音处理的应用程序。我正在 java/eclipse 中使用 swt/jface 执行此操作。 处理本身需要内部算法的一些选项/属性。此时,我有一个 .properties 文件,其中包含所有选项,例如:

trimLeadingSilence=20
修剪尾随沉默=20
修剪保持=5
fftFrameSize=512 ...

我不希望用户在像记事本++这样的文本编辑器中编辑这些选项,而是在我的应用程序的图形用户界面中编辑。

现在我想如何做到这一点。我有两个“想法”: 为每个选项集创建一个类,并手动编写所有这些无聊的 GUI 代码行。就像这里只有一个选项;-)

Spinner spinnerSilenceBack = new Spinner(shell, SWT.BORDER);
        spinnerSilenceBack.setMinimum(0);
        spinnerSilenceBack.setMaximum(200);
        selection = Integer.valueOf(PropertyManager.getPropertyValue("trimming", "trailingSilence"));
        spinnerSilenceBack.setSelection(selection);
        spinnerSilenceBack.setIncrement(5);
        spinnerSilenceBack.setPageIncrement(20);
        spinnerSilenceBack.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
                int selection = ((Spinner) e.getSource()).getSelection();
                int digits = ((Spinner) e.getSource()).getDigits();
                int result = (int) (selection / Math.pow(10, digits));

                PropertyManager.setPropertyValue("trimming", "trailingSilence", String
                        .valueOf(result));

            }
        });

由于有很多不同的选项,这需要很多时间。所以我想到了如何动态创建这样的 gui 代码,或者只是在启动应用程序时动态创建这些 gui 窗口。至少我需要一个“gui Creator”的配置文件,但我不想重新发明这样的东西——这就是我问你们的原因:)

currently im building an application which is supposed for some sound processing. I'm doing this in java/eclipse with swt/jface.
The processing itself need some options/properties for the algorithem inside. At this time, i have a .properties file which holds all options like:

trimLeadingSilence=20
trimtrailingSilence=20
trimhold=5
fftFrameSize=512
...

I don't want the user to edit these options in a texteditor like notepad++, but within the gui of my app.

Now I think about how to do this. I have 2 "ideas":
Create a class for each option set, and manualy code all these boring gui code lines. Like here for just one option ;-)

Spinner spinnerSilenceBack = new Spinner(shell, SWT.BORDER);
        spinnerSilenceBack.setMinimum(0);
        spinnerSilenceBack.setMaximum(200);
        selection = Integer.valueOf(PropertyManager.getPropertyValue("trimming", "trailingSilence"));
        spinnerSilenceBack.setSelection(selection);
        spinnerSilenceBack.setIncrement(5);
        spinnerSilenceBack.setPageIncrement(20);
        spinnerSilenceBack.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
                int selection = ((Spinner) e.getSource()).getSelection();
                int digits = ((Spinner) e.getSource()).getDigits();
                int result = (int) (selection / Math.pow(10, digits));

                PropertyManager.setPropertyValue("trimming", "trailingSilence", String
                        .valueOf(result));

            }
        });

This takes a lot of time due to the fact that there are a lot of different options. So I thought about how I can dynamicly create such gui code, or just dynamicly create these gui windows when starting up the application. At least I would need a config file for the "gui creator" but I don't want to reinvent such a thing-thats why i ask you guys :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

爱要勇敢去追 2024-09-09 09:26:43

我无法清楚地了解你在问什么。

但是,由于您的问题是如何动态构建 gui,我有一个建议:

您可以使用Java模板引擎
类似 Freemarker 的库。这
将帮助您创建可以
专为即时工作而设计。和
freemarker 你可以拥有一张
模板文件,然后替换
相应的值。

我用它来动态生成 HTML 文件。您可以评估是否可以以其他方式使用它。 API文档很丰富。所以你可以经历一次。

I couldn't get clearly what you are asking.

But, since your question was How to dynamicly build up a gui, i have a a suggestion:

You could use Java Template Engine
library like Freemarker. This
would help you create GUI's that can
be built to work on the fly. With
freemarker you can have one single
template file and then replace the
values accordingly.

I have used it to generate HTML files on the fly. You could evaluate if you can use it otherwise. The API Documentation is rich. So you could go through that once.

半仙 2024-09-09 09:26:43

您的意思是,您想要创建一个包含您指定的所有选项的用户界面吗?无论是表单还是菜单,都取决于您。但是,您肯定可以在 .properties 文件中配置名称和类型。

看,您在 AWT/Swing 或 Servlet 中构建了一个菜单或表单,但您可以从属性文件中读取它,对吧?

您还可以使用 Spring bean XML 定义进行配置,这可以提供更多支持,例如您可以在某些映射或列表等中指定所有详细信息...

谢谢。

Do you mean, you want to make an UI which will have all options you specified? It doesn't matter its a form OR a menu, its up to you. But yeah you can surely configure the names and types in .properties file.

See, you build a Menu OR form in AWT/Swing OR Servlet, but you can read it from properties file right?

You can also configure the same using Spring bean XML definitions, which can provide more support like you can specify all details in some Map OR List etc...

thanks.

聊慰 2024-09-09 09:26:43

我使用 Swing 的时间不多,所以这里只是一个基本原理。
配置应该在 xml 中完成,.properties 文件在这里很糟糕,因为它没有描述开箱即用的对象。
添加按钮(“应用配置”),附加 actionListener,它 1)解析 xml 配置,2)然后创建表单或更改现有表单、文本区域、颜色等的设置。

xml 配置示例:

找到 - 检查它是x_coord,y_coord(或使用layoutManager,取决于逻辑),action,而不是jframe.getLayout()。add(new Button(x_coord,y_coord,action)。
相同。

发现 -与 jframe.setVisible(true)

I didn't use Swing for a lot of time, so here is just a basic principle.
Config should be done in xml, .properties file is bad here, cuz it doesn't describe objects out-of-the-box.
Add button ("Apply config"), attach actionListener, which 1)parses xml config, 2)then creates form or change settings of existing form, textarea, colors etc.

example for xml config:

found - check it's x_coordinate, y_coord (or use layoutManager, depends on logic), action, than jframe.getLayout().add(new Button(x_coord, y_coord, action).
found - the same.

than jframe.setVisible(true);

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