Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Projects

Create a new scala project in IntelliJ

Add scala-swing to sbt

// build.sbt
ThisBuild / version := "0.1.0-SNAPSHOT"

ThisBuild / scalaVersion := "3.3.3"

lazy val root = (project in file("."))
  .settings(
    name := "untitled"
  )

libraryDependencies += "org.scala-lang.modules" %% "scala-swing" % "3.0.0"

Project 1: Celcius Fahrenheit Converter


import scala.swing._
import scala.swing.event.ButtonClicked

object CelsiusFahrenheitConverter extends SimpleSwingApplication {
  def top = new MainFrame {
    title = "Celsius to Fahrenheit Converter"

    // Components
    val textField = new TextField { columns = 10 }
    val convertButton = new Button { text = "Convert" }
    val resultLabel = new Label { text = "Result: " }

    // Layout
    contents = new FlowPanel {
      contents += new Label("Celsius: ")
      contents += textField
      contents += convertButton
      contents += resultLabel
    }

    // Logic for conversion
    listenTo(convertButton)
    reactions += {
      case ButtonClicked(_) =>
        val celsius = textField.text.toDouble
        val fahrenheit = celsius * 9/5 + 32
        resultLabel.text = f"Result: $fahrenheit%.2f Fahrenheit"
    }

    size = new Dimension(300, 100)
    centerOnScreen()
  }
}

Project 2a: Number guess

Write a number guess game on the command line

  • generate a random
  • ask the user to guess the number
  • tell the user if the number is smaller, larger or equal.

Project 2b: Number guess

Write a number guess game as a swing application

Project 3a: Todo List

Create a todo list on the command line

Project 3b: Todo List

Create a todo list as a swing application

Project 4: TicTacToe

Do this one after the Project: Calculator