简体   繁体   English

按下按钮后如何检查是否填充了 TextField

[英]How to check, after a Button is pressed, if a TextField was filled in

I believe my question is pretty simple but i am not really sure if i have the solution to it.我相信我的问题很简单,但我不确定我是否有解决方案。 The closest i've found to it is something like:我发现的最接近它的是:

if (exampleTextField.getText().trim().isEmpty()){ ... }

All in all, i want to know how to filter the objects that are created based on that specific fact.总而言之,我想知道如何过滤基于该特定事实创建的对象。

As an example (i'll just say something random, hope it makes sence): I have 2 types of student classes, a class named "UGStudent" for UnderGraduate Students and a class named "PGStudent" for Post Graduate Students.举个例子(我只是随便说点什么,希望它有意义):我有两种类型的学生课程,一个名为“UGStudent”的本​​科生课程和一个名为“PGStudent”的研究生课程。 Let's suppose i have a JFrame with some TextFields and one single JButton "Add Student".假设我有一个带有一些 TextField 的 JFrame 和一个 JButton“添加学生”。 Let's also suppose that the TextFields i have are 4 : "Last Name", "Full Name", "GPA" and "Graduation Year".我们还假设我拥有的 TextFields 是 4:“姓氏”、“全名”、“GPA”和“毕业年份”。 When the button is pressed a new object is created and added into a respective ArrayList.当按下按钮时,会创建一个新对象并将其添加到相应的 ArrayList 中。 So as you'd probably guess, if the field "Graduation Year" is filled we'd like a "PGStudent" to be created, otherwise we'd like a "UGStudent" to be created.因此,您可能会猜到,如果“毕业年份”字段已填满,我们希望创建一个“PGStudent”,否则我们希望创建一个“UGStudent”。

Any suggested solutions?任何建议的解决方案?

Not sure of your exact question, but if you're wanting to listen for whether a JTextField's text has been modified, for instance to know whether or not to enable a JButton or its Action, then use a DocumentListener.不确定您的确切问题,但如果您想监听 JTextField 的文本是否已被修改,例如了解是否启用 JButton 或其操作,则使用 DocumentListener。

Swing, as with most event-driven GUI libraries, has many listener types that will enable you to listen to the state of the components of your GUI. Swing 与大多数事件驱动的 GUI 库一样,有许多侦听器类型,使您能够侦听 GUI 组件的状态。

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

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