This commit is contained in:
Ingy döt Net 2013-10-27 22:24:23 +00:00
parent 6f050a029e
commit 776bba907c
3887 changed files with 59894 additions and 7280 deletions

View file

@ -6,9 +6,12 @@ object Interact extends SimpleSwingApplication {
def top = new MainFrame {
title = "Rosetta Code >>> Task: component interaction | Language: Scala"
val numberField = new TextField { text = "0" } // start at 0
val numberField = new TextField {
text = "0" // start at 0
horizontalAlignment = Alignment.Right
}
val incButton = new Button { text = "Increment" }
val incButton = new Button { text = "Increment" }
val randButton = new Button { text = "Random" }
// arrange buttons in a grid with 1 row, 2 columns
@ -17,30 +20,26 @@ object Interact extends SimpleSwingApplication {
}
// arrange text field and button panel in a grid with 2 row, 1 column
contents = new GridPanel(2, 1) {
contents ++= List(numberField, buttonPanel)
}
contents = new GridPanel(2, 1) { contents ++= List(numberField, buttonPanel) }
// listen for keys pressed in numberField and button clicks
listenTo(numberField.keys, incButton, randButton)
reactions += {
case kt: KeyTyped if (!kt.char.isDigit) => // if the entered char isn't a digit ...
kt.consume // ... eat the event (i.e. stop it from being processed)
case kt: KeyTyped if (!kt.char.isDigit) => // if the entered char isn't a digit
kt.consume // eat the event (i.e. stop it from being processed)
case ButtonClicked(`incButton`) =>
if (numberField.text.isEmpty()) numberField.text = "0"
if (numberField.text.isEmpty) numberField.text = "0"
// we use BigInt to avoid long overflow/number format exception
val biNumber = new BigInt(new java.math.BigInteger(numberField.text))
numberField.text = "" + (biNumber+1)
numberField.text = (BigInt(numberField.text) + 1).toString
case ButtonClicked(`randButton`) =>
import Dialog._
numberField.text = showOptions(buttonPanel,
message = "Are you sure?",
title = "Choose an option!",
entries = List("Yes", "No", "Cancel"),
initial = 2) match {
case Result.Yes => (Long.MaxValue * Math.random).toLong.toString
case _ => numberField.text
}
numberField.text = showOptions(buttonPanel, message = "Are you sure?",
title = "Choose an option!", entries = List("Yes", "No", "Cancel"),
initial = 2) match {
case Result.Yes => (Long.MaxValue * Math.random).toLong.toString
case _ => numberField.text
}
}
centerOnScreen()
}
}