Anonymous user
GUI component interaction: Difference between revisions
→{{header|Scala}}
Line 1,423:
title = "Rosetta Code >>> Task: component interaction | Language: Scala"
val numberField = new TextField {
text = "0" // start at 0
horizontalAlignment = Alignment.Right
}▼
val incButton
val randButton = new Button { text = "Random" }
// arrange buttons in a grid with 1 row, 2 columns
val buttonPanel = new GridPanel(1, 2) {
contents ++= List(incButton, randButton)
}
// arrange text field and button panel in a grid with 2 row, 1 column
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 ...
Line 1,447 ⟶ 1,448:
// we use BigInt to avoid long overflow/number format exception
val biNumber = new BigInt(new java.math.BigInteger(numberField.text))
numberField.text = "" + (biNumber + 1)
case ButtonClicked(`randButton`) =>
import Dialog._
numberField.text = showOptions(buttonPanel, message = "Are you sure?",
title = "Choose an option!",
case
case _
}▼
▲ }
}
centerOnScreen()
}
}</lang>
|