在以下脚本中,如何获取返回的值而不是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
}
}
答案 0 :(得分:2)
添加名为successfulLogin
的布尔字段,并在true
pass.text.equals("password")
你什么时候让你的框架可见?您可能想要使用JOptionPane。
import javax.swing.JComponent
import javax.swing.JPasswordField
import javax.swing.JLabel
import javax.swing.JOptionPane
public boolean loginSuccessful() throws Exception {
final JPasswordField passwordField = new JPasswordField();
final JComponent[] components = [new JLabel("Password"), passwordField];
if (JOptionPane.showConfirmDialog(null, components, "Enter your PASSWORD", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE) == JOptionPane.OK_OPTION) {
// user clicked ok
if (new String(passwordField.getPassword()).equals("password")) {
// password matches
return true;
}
}
return false;
}