Application: Event Handling
scala> import java.awt._ import java.awt._ scala> import java.awt.event._ import java.awt.event._ scala> import javax.swing._ import javax.swing._ scala> implicit def mekeAction(action:(ActionEvent) =>Unit) = new ActionListener { | override def actionPerformed(event: ActionEvent){ action(event)} | } mekeAction: ((java.awt.event.ActionEvent) => Unit)java.lang.Object with java.awt.event.ActionListener scala> val frame = new JFrame frame: javax.swing.JFrame = javax.swing.JFrame[frame0,0,22,0x0,invalid,hidden,layout=java.awt.BorderLayout,title=,resizable,normal,defaultCloseOperation=HIDE_ON_CLOSE,rootPane=javax.swing.JRootPane[,0,0,0x0,invalid,layout=javax.swing.JRootPane$RootLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=449,maximumSize=,minimumSize=,preferredSize=],rootPaneCheckingEnabled=true] scala> val textArea = new JTextArea(20,50) textArea: javax.swing.JTextArea = javax.swing.JTextArea[,0,0,0x0,invalid,layout=javax.swing.plaf.basic.BasicTextUI$UpdateHandler,alignmentX=0.0,alignmentY=0.0,border=javax.swing.plaf.basic.BasicBorders$MarginBorder@d0bbb5,flags=296,maximumSize=,minimumSize=,preferredSize=,caretColor=javax.swing.plaf.ColorUIResource[r=0,g=0,b=0],disabledTextColor=javax.swing.plaf.ColorUIResource[r=128,... scala> val button1 = new JButton("Click Me!") button1: javax.swing.JButton = javax.swing.JButton[,0,0,0x0,invalid,alignmentX=0.0,alignmentY=0.5,border=apple.laf.CUIAquaButtonBorder$Dynamic@47898f,flags=288,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=javax.swing.plaf.InsetsUIResource[top=0,left=2,bottom=0,right=2],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=fal... scala> val button2 = new JButton("Exit") button2: javax.swing.JButton = javax.swing.JButton[,0,0,0x0,invalid,alignmentX=0.0,alignmentY=0.5,border=apple.laf.CUIAquaButtonBorder$Dynamic@47898f,flags=288,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=javax.swing.plaf.InsetsUIResource[top=0,left=2,bottom=0,right=2],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=fal... scala> val count = 0 count: Int = 0 scala> button1.addActionListener((e:ActionEvent) => { textArea.append("I was clicked!\n") }) scala> val panel = new JPanel panel: javax.swing.JPanel = javax.swing.JPanel[,0,0,0x0,invalid,layout=java.awt.FlowLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=9,maximumSize=,minimumSize=,preferredSize=] scala> panel.add(button1) res3: java.awt.Component = javax.swing.JButton[,0,0,0x0,invalid,alignmentX=0.0,alignmentY=0.5,border=apple.laf.CUIAquaButtonBorder$Dynamic@47898f,flags=288,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=javax.swing.plaf.InsetsUIResource[top=0,left=2,bottom=0,right=2],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=false,r... scala> panel.add(button2) res4: java.awt.Component = javax.swing.JButton[,0,0,0x0,invalid,alignmentX=0.0,alignmentY=0.5,border=apple.laf.CUIAquaButtonBorder$Dynamic@47898f,flags=288,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=javax.swing.plaf.InsetsUIResource[top=0,left=2,bottom=0,right=2],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=false,r... scala> frame.add(panel,BorderLayout.NORTH) scala> frame.add(textArea) res6: java.awt.Component = javax.swing.JTextArea[,0,0,0x0,invalid,layout=javax.swing.plaf.basic.BasicTextUI$UpdateHandler,alignmentX=0.0,alignmentY=0.0,border=javax.swing.plaf.basic.BasicBorders$MarginBorder@d0bbb5,flags=296,maximumSize=,minimumSize=,preferredSize=,caretColor=javax.swing.plaf.ColorUIResource[r=0,g=0,b=0],disabledTextColor=javax.swing.plaf.ColorUIResource[r=128,g=128,b... scala> frame.pack() scala> frame.setVisible(true)
exitは後から実装してみてねという設問でもあるのだろう。
Fix the Exit button so that it calls frame.setVisible(false). How did you do that?
scala> button2.addActionListener((e:ActionEvent) => { frame.setVisible(false)})
あった。