简体   繁体   English

在Swing中创建Java GUI以进行表单输入

[英]Creating a Java GUI in Swing for form input

Well, I've looked all over the internet and just haven't been able to find an answer to this question, so maybe someone can provide some insight. 好吧,我已经浏览了整个互联网,但却未能找到这个问题的答案,所以也许有人可以提供一些见解。

I'm working on developing a relatively simple Java app that will replace a Word doc currently used for system access requests. 我正在开发一个相对简单的Java应用程序,它将取代目前用于系统访问请求的Word文档。 It's designed to allow form entry of new employee hire information - name, access needed, and so forth. 它旨在允许表单输入新的员工雇用信息 - 名称,所需的访问权限等。

So here's my problem. 所以这是我的问题。 Trying to make a GUI with all of the text fields and everything is surprisingly painful. 尝试使用所有文本字段和所有内容制作GUI令人惊讶地痛苦。 Because each widget is a bit different, getting the input once the form is filled out seems to require a separate reference for each widget so I can call them individually. 因为每个小部件有点不同,所以在填写表单后获取输入似乎需要为每个小部件单独引用,以便我可以单独调用它们。 This means each panel for a section has 6-10 different private fields. 这意味着一个部分的每个面板有6-10个不同的私有字段。 (I attempted adding all the similar widgets to a list and calling them in a loop but that didn't seem to work.) (我尝试将所有类似的小部件添加到列表中并在循环中调用它们,但这似乎不起作用。)

It seems that a web form would be a better fit for this in some ways but I don't have the infrastructure available to do that. 在某些方面,似乎Web表单更适合这种情况,但我没有可用的基础结构。 Has anyone out there found a better solution than this for something similar? 有没有人找到比这更类似的更好的解决方案呢? It just seems like a ton of code. 它似乎只是一大堆代码。 Please see below for an idea (I put in some comments rather than actual code because it is so long). 请参阅下面的一个想法(我提出了一些评论而不是实际的代码,因为它太长了)。 Thanks for looking! 谢谢你的期待!

    private JComboBox my_dates;
    private JTextField my_date1;
    private JTextField my_date2;
    private JTextField my_request_date;
    private JTextField my_new_legal_name;
    private JTextField my_new_pref_name;
    private JTextField my_new_username;
    private JTextField my_prev_legal_name;
    private JTextField my_prev_pref_name;
    private JTextField my_prev_username;
    private JTextField my_emp_id;
    private JTextField my_manager;
    private JTextField my_auth_requestor;
    private JTextField my_auth_phone;

    public NameChangePanel(FormSection the_section)
    {
        super();
        initialize();
        buildPanel(the_section.getFields());
    }

    private void initialize()
    {
        // Create all the widgets individuall
    }

    private void buildPanel(List the_fields)
    {
        // add a field label
        // add a component
        // repeat for all values
    }

    public List getFormValues()
    {
        // Call all of the private fields individually
        return values;
    }
}

Vanilla Swing is verbose - no argument there. Vanilla Swing很冗长 - 没有争论。 Basically for this kind of thing I end up relying on a bunch of home-grown util/factory methods like createTextBox() that is parameterized for common usage and will execute the 3-8 statements necessary. 基本上对于这种事情,我最终依赖于一堆本地生成的util / factory方法,比如createTextBox(),它们被参数化以供常用,并将执行必要的3-8语句。

I will sometimes even parameterize the layout. 我有时甚至会参数化布局。 I tend to reduce all layout to GridBagLayout (others do BorderLayout, etc; it's just a matter of personal preference) and then create methods that'll put a control at a particular point in the grid. 我倾向于将所有布局减少到GridBagLayout(其他人做BorderLayout等;这只是个人偏好的问题),然后创建方法,将控件放在网格中的特定点。

If you're not going Web-based, I would just stick with Swing. 如果你不是基于网络的,我会坚持使用Swing。 It might be verbose but it's not really all that difficult for a simple form. 它可能很冗长,但对于一个简单的形式来说并不是那么困难。

Call me crazy but I dislike gui builders. 叫我疯了,但我不喜欢gui建设者。 I prefer the extra control that hand-coding brings. 我更喜欢手动编码带来的额外控制。 Also, as you wrote the code yourself it makes it far easier to debug. 此外,当您自己编写代码时,它使调试变得更加容易。 What is important is to layout your code in a logical order and to use methodical variable names. 重要的是按逻辑顺序布局代码并使用有条理的变量名称。

You mentioned trying to use a list. 你提到过尝试使用列表。 However, I don't see how this is going to save you much time at all (or as I hinted eariler, what time you'll save on implementation, you'll lose during the debugging process). 但是,我没有看到这将如何为您节省很多时间(或者我暗示,在您实施时节省的时间,您将在调试过程中丢失)。 I'm guessing that the headaches are starting when you start sanity-checking the input of your JTextFields. 我猜你在开始理智检查JTextField的输入时会出现令人头疼的问题。 I'd suggest subclassing JTextField into MoneyTextField, PhoneNumberTextField, etc. which can be used to setup the ActionListener to stop people doing stupid things. 我建议将JTextField子类化为MoneyTextField,PhoneNumberTextField等,这可用于设置ActionListener以阻止人们做蠢事。

您可以查看我尝试解决此问题的方法: http//code.google.com/p/swing-formbuilder/

Netbeans has a gui builder that is reasonably good by most accounts. Netbeans有一个gui构建器,大多数帐户都相当不错。 This screen cast shows off the data binding feature that may be helpful for you. 此屏幕演示显示了可能对您有所帮助的数据绑定功能。

Another option may be to roll your own presentation framework which, depending on how complex you want it, is not that hard and sorta fun. 另一种选择可能是推出自己的演示框架,这取决于你想要的复杂程度,并不是那么难和有趣。 Just use reflection to pull fields out of an object and translate those fields into widgets and back again. 只需使用反射从对象中提取字段,然后将这些字段转换为小部件,然后再将其转换回来。 Make sure to use a layout manager to do most of the heavy lifting for you. 确保使用布局管理器为您完成大部分繁重的工作。

Yes, on the whole, gui stuff is not java's strong suit, but you should find it's good enough. 是的,总的来说,gui的东西不是java的强项,但是你应该觉得它很好。

if you are adventurous, you can try doing it in groovy, using griffon. 如果你喜欢冒险,你可以尝试使用griffon在groovy中进行。 Griffon is a GUI building framework that makes gui building easier by automating the repetitive parts. Griffon是一个GUI构建框架,通过自动化重复部分使gui构建更容易。 See http://griffon.codehaus.org/ for more details. 有关详细信息,请参阅http://griffon.codehaus.org/

If you know how to use grails, griffon will feel similar, as it is built using similar concepts and metaphors. 如果您知道如何使用grails,那么griffon会感觉类似,因为它是使用类似的概念和隐喻构建的。

Take a look at BetterBeansBinding . 看看BetterBeansBinding It's a tool which makes it easy to bind various JavaBean like properties between objects. 它是一个工具,可以轻松地在对象之间绑定各种类似JavaBean的属性。 You would set up an object which holds a request, and bind the properties of that object to the various gui components. 您将设置一个包含请求的对象,并将该对象的属性绑定到各种gui组件。 When you're done, just return the bound object and it should have all the fields filled out. 完成后,只需返回绑定对象,它应填写所有字段。

Note that I haven't actually used this before, but I've seen the concept and have done similar things before it was released. 请注意,我以前没有实际使用过这个,但是我已经看过这个概念,并且在它发布之前做了类似的事情。

I would create a this form from a XML file, something like 我会从XML文件中创建一个这样的表单

<form name="MyForm" package="com.nowhere.swing">
 <field component="javax.swing.JTextField" label="Enter your Name" pattern="[a-z]+" name="name"/>
 <field component="javax.swing.JTextArea" label="Enter your Comment" pattern="[a-z]+" name="comment"/>
</form>

and then, using XSLT I would then generate the java code for a Pojo and for the Swing interface using this definition. 然后,使用XSLT,我将使用此定义为Pojo和Swing接口生成java代码。

class MyFormPojo
 {
 private String name;
 private String comment;
 (...)
 } 

class MyFormPane extends JPanel
 {
 JTextField field4name;
  JTextArea field4comment;
 (...)
 MyFormPane()
   {
   (...)
   field4name= new JTextField();
   (.. create labels, etc...)
   }
 }

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

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