scala/ScalaBook/chapter-06/textFields2.scala
import scala.swing._
import scala.swing.event._
object text extends SimpleGUIApplication {
val user_name = "apostolos"
val password = "HYa9gj"
var editDone = false
def top = new MainFrame {
title = "Login Screen"
val user_label = new Label("User Name ")
val user_field = new TextField {
columns = 10
shouldYieldFocus = (x:String) => (x == "apostolos" || x == "christos")
}
val pass_label = new Label("Password ")
val pass_field = new PasswordField(10) {
echoChar = '•'
}
val error_label = new Label(" ")
contents = new GridBagPanel {
var c = new Constraints
c.gridwidth = java.awt.GridBagConstraints.RELATIVE
add(user_label,c)
add(user_field,c)
c.gridwidth = java.awt.GridBagConstraints.REMAINDER
add(Swing.VStrut(50), c)
c.gridwidth = java.awt.GridBagConstraints.RELATIVE
add(pass_label,c)
add(pass_field,c)
c.gridwidth = java.awt.GridBagConstraints.REMAINDER
add(Swing.VStrut(50), c)
add(error_label,c)
border = Swing.EmptyBorder(50, 50, 50, 50)
}
listenTo(user_field,pass_field)
reactions += {
case EditDone(`pass_field`) =>
if ( user_field.text.length > 0 ) {
if (user_field.text == user_name &&
pass_field.password.deepMkString("") == password)
error_label.text = "Welcome!"
else
error_label.text = "Incorrect username/password!"
}
}
}
}