본문 바로가기
Programming/Scala

[Scala] 스윙 어플리케이션 만들기

by NAMP 2015. 12. 24.

import scala.swing._ 를 추가합니다.


package week1

import scala.swing._


object FirstSwingApp extends SimpleSwingApplication {

  def top = new MainFrame {

    title = "스윙 어플리케이션"

    contents = new Button {

      text = "여기를 클릭"

    }

  }

} 


실행 화면


package week1

import scala.swing._


object FirstSwingApp extends SimpleSwingApplication {

  def top = new MainFrame {

    title = "스윙 어플리케이션"

    val button = new Button {

      text = "여기를 클릭"

    }

    val label = new Label {

      text = "버튼 클릭 미등록"

    }

  

    contents = new BoxPanel(Orientation.Vertical){

      contents += button

      contents += label

      border = Swing.EmptyBorder(30, 30, 10, 30)      

    }

  }  

}



실행화면







버튼에 이벤트를 추가합니다. 


import scala.swing.event._


를 추가합니다.


package week1

import scala.swing._

import scala.swing.event._


object FirstSwingApp extends SimpleSwingApplication {

  def top = new MainFrame {

    title = "스윙 어플리케이션"

    val button = new Button {

      text = "여기를 클릭"

    }

    val label = new Label {

      text = "버튼 클릭 미등록"

    }

  

    contents = new BoxPanel(Orientation.Vertical){

      contents += button

      contents += label

      border = Swing.EmptyBorder(30, 30, 10, 30)      

    }

    

    listenTo(button)

    var nClicks = 0

    reactions += {

      case ButtonClicked(b) =>

        nClicks += 1

        label.text = "클릭 수 : "+ nClicks

    }

  }

}


실행 화면




버튼을 클릭하면, 해당 클릭 횟수가 출력됩니다.





FirstSwingApp.scala


FirstSwingApp.class






댓글