λ Tony's Blog λ
Java interop errata
Posted on August 13, 2008There are a few possibilities for improvement on this Scala Swing Example.
Removed unnecessary semi-colons
Inlined import
Removed
=
in main declaration (strongly advised on functions returningUnit
and especially more formain
)Removed a few unnecessary parentheses
import javax.swing._
import java.awt.event.{ActionEvent, ActionListener}
object HelloWorld extends JFrame("Hello Swing") {
def showButtonMessage(msg: String) =
showMessageDialog(null, String.format("""<html>Hello from <b>Scala</b>. Button %s pressed""", Array(msg)));
JOptionPane.
def main(args: Array[String]) {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
val button = new JButton("Click Me")
addActionListener((e:ActionEvent) => showButtonMessage(e.getActionCommand.toString))
button.
getContentPane add button
packsetVisible(true)
}
implicit def actionPerformedWrapper(func: (ActionEvent) => Unit) = new ActionListener {
def actionPerformed(e:ActionEvent) = func(e)
} }