Java 中的链接按钮和文本字段数组
我有一个文本字段列表和一个按钮列表。它们布置在表单上,按钮位于文本字段旁边。假设每个有 5 个,即 1 到 5。实际上,该数字将在运行时创建。
当用户单击按钮时,将打开一个新表单,指导用户创建字符串。当他们完成该表单后,按钮旁边的文本字段中将写入一个文本字符串。
我想在每个按钮和文本字段之间创建关系,以便我知道根据单击的按钮写入哪个文本字段。
这里有 Java 标准吗?如果没有,任何建议表示赞赏。
谢谢
I have a list of textfields, and a list of buttons. These are laid out on a form with the buttons next to the textfields. Let's say there are 5 of each, they are 1 through 5. In reality, the number will be created at runtime.
When the user clicks a button, a new form is opened, which guides the user through creating a string. When they finish on that form, a text string is written in to the text field next to the button.
I'd like to create a relationship between each button and text field so that I know which text field to write in based on which button was clicked.
Is there a Java standard here? If not, any suggestions appreciated.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
那么,可以包装或扩展
JButton
以添加JTextfield
字段。创建按钮后,将关联的文本字段传递给它,然后您可以传递该信息。Well either wrap or extend
JButton
to add aJTextfield
field. Upon creation of the Button, pass the associated textfield to it and then you can pass along that info.创建一个将文本字段作为参数的 ActionListener 类。类似于:
然后您可以将文本字段保存为侦听器类中的变量,并且当对话框关闭时您可以更新文本字段。
Create an ActionListener class that takes the text field as a parameter. Something like:
Then you can save the text field as a variable in your listener class and when the dialog closes you can update the text field.
你有两个列表,为什么不使用列表中的索引呢?索引
0
处的按钮与索引0
处的文本字段相关,依此类推。另一种选择是将它们包装在包含按钮和文本字段的对象中并运行单个列表。我会使用第二条路线,但仅使用列表的索引没有任何问题。
You have two lists, why not use the index in the list? Button at index
0
relates to textfield at index0
and so on. Another alternative would be wrap them in an object that contains the button and the textfield and run a single list.I would use the second route but there is nothing wrong with just using the index of the list.
基本上,您可以使用附加属性索引对 JButton 进行子类化。您可以在创建按钮时填充索引,它将引用文本字段数组中文本字段的索引。如果您愿意,您甚至可以为每个文本字段指定一个名称,并将它们添加到地图中,然后为按钮指定名称以查找所需的文本字段。
Basically you can subclass JButton with additional attribute index. You can populate the index when creating the buttons and it will refer to index of text field in array of text fields. If you want you can even have a name given to each text field and add them to a map and then give the name to button to lookup the required text field.