简体   繁体   English

如何动态建立GUI

[英]How to dynamicly build up a gui

currently im building an application which is supposed for some sound processing. 目前,我正在构建一个应进行声音处理的应用程序。 I'm doing this in java/eclipse with swt/jface. 我正在使用swt / jface在java / eclipse中进行此操作。 The processing itself need some options/properties for the algorithem inside. 处理本身需要内部算法的一些选项/属性。 At this time, i have a .properties file which holds all options like: 目前,我有一个.properties文件,其中包含所有选项:

trimLeadingSilence=20 trimLeadingSilence = 20
trimtrailingSilence=20 trimtrailingSilence = 20
trimhold=5 修剪点= 5
fftFrameSize=512 ... fftFrameSize = 512 ...

I don't want the user to edit these options in a texteditor like notepad++, but within the gui of my app. 我不希望用户在文本编辑器(如notepad ++)中编辑这些选项,而是在我的应用程序的GUI中。

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. 我有2个“想法”:为每个选项集创建一个类,并手动编码所有这些无聊的gui代码行。 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. 因此,我考虑了如何在启动应用程序时动态创建此类gui代码,或者只是动态创建这些gui窗口。 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 :) 至少我需要一个用于“ gui creator”的配置文件,但是我不想重新发明这样的东西,这就是为什么我问你们:)

I couldn't get clearly what you are asking. 我不清楚你在问什么。

But, since your question was How to dynamicly build up a gui , i have aa suggestion: 但是,由于您的问题是如何动态构建gui ,所以我有一个建议:

You could use Java Template Engine library like Freemarker . 您可以使用Freemarker之类的Java模板引擎库。 This would help you create GUI's that can be built to work on the fly. 这将帮助您创建可即时运行的GUI。 With freemarker you can have one single template file and then replace the values accordingly. 使用freemarker,您可以拥有一个模板文件,然后相应地替换这些值。

I have used it to generate HTML files on the fly. 我用它来动态生成HTML文件。 You could evaluate if you can use it otherwise. 您可以评估是否可以使用它。 The API Documentation is rich. API文档丰富。 So you could go through that once. 因此,您可以经历一次。

Do you mean, you want to make an UI which will have all options you specified? 您的意思是说,您要制作一个包含所有指定选项的UI吗? 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. 但是,您可以肯定地在.properties文件中配置名称和类型。

See, you build a Menu OR form in AWT/Swing OR Servlet, but you can read it from properties file right? 看,您在AWT / Swing OR Servlet中构建了Menu OR表单,但是您可以从属性文件中读取它吗?

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... 您还可以使用Spring bean XML定义配置相同的内容,它可以提供更多支持,例如您可以在某些Map OR List等中指定所有详细信息。

thanks. 谢谢。

I didn't use Swing for a lot of time, so here is just a basic principle. 我已经很长时间没有使用Swing了,所以这只是一个基本原理。
Config should be done in xml, .properties file is bad here, cuz it doesn't describe objects out-of-the-box. 配置应该在xml中完成,.properties文件在这里不好,因为它没有开箱即用地描述对象。
Add button ("Apply config"), attach actionListener, which 1)parses xml config, 2)then creates form or change settings of existing form, textarea, colors etc. 添加按钮(“应用配置”),附加actionListener,其中1)解析xml配置,2)然后创建表单或更改现有表单,文本区域,颜色等设置。

example for xml config: xml配置示例:

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). 找到-检查它的x_coordinate,y_coord(或使用layoutManager,取决于逻辑),动作,然后检查jframe.getLayout()。add(new Button(x_coord,y_coord,动作)。
found - the same. 发现-一样。

than jframe.setVisible(true); 比jframe.setVisible(true);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM