简体   繁体   English

Java swing将多个文本字段保存到一个对象

[英]Java swing save several text field to a object

Im quite new to java swing but im creating a stock management program, i have now my window registration form were the user can insert, name, adress, username, password, etc, and i have a button below to save all data. 我对java swing很陌生,但我创建了一个库存管理程序,我现在有自己的窗口注册表格,用户可以输入,名称,地址,用户名,密码等,并且下面有一个按钮可以保存所有数据。 I want to e able to press the save button and all the data be saved to a new userObject, my problem is how to associate each textfield data so they can be saved to new object in save button. 我希望能够按一下保存按钮,并将所有数据保存到一个新的userObject中,我的问题是如何关联每个文本字段数据,以便可以将它们保存到保存按钮中的新对象中。 Can anyone give me some pointers on how to do this? 谁能给我一些如何做的指示?

Thanks! 谢谢!

I tried the code this way: 我以这种方式尝试了代码:

private void ButtonSaveActionPerformed(java.awt.event.ActionEvent evt) { 私有void ButtonSaveActionPerformed(java.awt.event.ActionEvent evt){

   User obj=new User();
   String userName = UserTextFieldName.getText();
   String userAdress = UserTextFieldAdress.getText();
   String userCitizenID = UserTextFieldCitizenID.getInteger();
   obj.setName(userName);
   obj.setAdress(userAdress);
   obj.setCitizenID(userCitizenID);

   usersArrayList.add(obj);

//this is were i would give a save correctly message and dispose the registration window. //这是我要给出正确保存消息并配置注册窗口的条件。

Is this a good alternative method?i could not make it working the other way with the ActionListener... 这是一种很好的替代方法吗?我无法通过ActionListener以其他方式使其工作...

You add an ActonListener to your button. 您将ActonListener添加到您的按钮。

Something like: 就像是:

JButton save = new JButton( "Save" );
save.addActionListener( new ActionListener()
{
    @Override
    public void actionPerformed(ActionEvent e)
    }
        UserObject user = new UserObject();
        user.setName( nameTextField.getText() );
        user.setAddress( addressTextField.getText() );
        ...
    }
});

Read the Swing tutorial for Swing basics. 阅读有关Swing基础知识的Swing教程

There are sections on: 有以下部分:

  1. How to Use Buttons
  2. How to Write an ActionListener

that each contain working examples to get your started. 每个都包含工作示例,以帮助您入门。

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

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