从 swingbuilder 返回一个值
在下面的脚本中,如何获取返回的值而不是我无法读取的 println ? 我在 Filemaker 中使用 Groovy,因此需要显式返回“答案”,即密码或 TRUE,而且我似乎不明白如何让代码等到它最终到达输入正确密码的位置。
import groovy.swing.SwingBuilder
import java.awt.event.ActionListener
import java.awt.event.ActionEvent
import static javax.swing.WindowConstants.DISPOSE_ON_CLOSE
count = 2
def answer
SwingBuilder.build{
frame( id:'root', title:'Enter your PASSWORD', location:[100,100], show:true, pack:true, defaultCloseOperation:DISPOSE_ON_CLOSE){
//lookAndFeel 'nimbus'
flowLayout()
label('Password')
passwordField(id:'pass', columns:12)
button('click', actionPerformed:{
ActionEvent e->
if (pass.text == 'password'){
optionPane().showMessageDialog( root, 'OK')
//RETURN something at this point only when I get here
dispose()
} else if (count == 1) {
optionPane().showMessageDialog(root, 'INCORRECT PASSWORD\nPROCEDURE HALTED', 'PASSWORD', 2)
dispose()
} else {
count--
message = count ==1 ?'try':'tries'
optionPane().showMessageDialog(root, "${count} ${message} left", 'NOT VALID', 0)
}// end if
pass.text=''
}// end action
)// end button
}
}
How do I get a value returned as opposed to println which I can not read, in the following script?
I am using Groovy inside Filemaker so need an explicit return 'answer' i.e. the password or a TRUE, and I do not seem to understand how to make the code wait till it finally gets to the point where the correct password is entered.
import groovy.swing.SwingBuilder
import java.awt.event.ActionListener
import java.awt.event.ActionEvent
import static javax.swing.WindowConstants.DISPOSE_ON_CLOSE
count = 2
def answer
SwingBuilder.build{
frame( id:'root', title:'Enter your PASSWORD', location:[100,100], show:true, pack:true, defaultCloseOperation:DISPOSE_ON_CLOSE){
//lookAndFeel 'nimbus'
flowLayout()
label('Password')
passwordField(id:'pass', columns:12)
button('click', actionPerformed:{
ActionEvent e->
if (pass.text == 'password'){
optionPane().showMessageDialog( root, 'OK')
//RETURN something at this point only when I get here
dispose()
} else if (count == 1) {
optionPane().showMessageDialog(root, 'INCORRECT PASSWORD\nPROCEDURE HALTED', 'PASSWORD', 2)
dispose()
} else {
count--
message = count ==1 ?'try':'tries'
optionPane().showMessageDialog(root, "${count} ${message} left", 'NOT VALID', 0)
}// end if
pass.text=''
}// end action
)// end button
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
pass.text.equals("password")
添加一个名为
successfulLogin
的布尔字段,并将其设置为true
,当 框架可见?您可能只想使用 JOptionPane。Add a boolean field called
successfulLogin
and set it totrue
when thepass.text.equals("password")
When do you actually make your frame visible? You might want to just go with JOptionPane.