Scala Swing yangi a'zosi

Im ilova qilish uchun kirish oynasini yaratishga urinaman. Men butun kunni bir misol uchun qidirdim, ammo yordam beradigan biron narsa topa olmayapman. Asosiy tuzilmam quyidagilar:

// App.scala
object App extends SimpleSwingApplication {
  val ui = new BorderPanel {
    //content
  }

  def top = new MainFrame {
    title = "title"
    contents = ui
  }
}

Xo'sh, asosiy strategiya qanday qilib asosiy paragrafsiz login qutisini yaratish va unga kirishdan keyin va ota-ekranni ko'rsatgan holda yopish kerak. rahmat

3

1 javoblar

Mana misol. Loyihalarimdan birini oldi va siz uchun biroz o'zgartirildi:

import swing._
import scala.swing.BorderPanel.Position._

object App extends SimpleSwingApplication {
  val ui = new BorderPanel {
    //content
  }

  def top = new MainFrame {
    title = "title"
    contents = ui
  }

  val auth = new LoginDialog().auth.getOrElse(throw new IllegalStateException("You should login!!!"))
}

case class Auth(userName: String, password: String)

class LoginDialog extends Dialog {
  var auth: Option[Auth] = None
  val userName = new TextField
  val password = new PasswordField

  title = "Login"
  modal = true

  contents = new BorderPanel {
    layout(new BoxPanel(Orientation.Vertical) {
      border = Swing.EmptyBorder(5,5,5,5)

      contents += new Label("User Name:")
      contents += userName
      contents += new Label("Password:")
      contents += password
    }) = Center

    layout(new FlowPanel(FlowPanel.Alignment.Right)(
      Button("Login") {
        if (makeLogin()) {
          auth = Some(Auth(userName.text, password.text))
          close()
        } else {
          Dialog.showMessage(this, "Wrong username or password!", "Login Error", Dialog.Message.Error)
        }
      }
    )) = South
  }

  def makeLogin() = true//here comes you login logic

  centerOnScreen()
  open()
}

Ko'rib turganingizdek, odatda modal muloqot oynayapman, shuning uchun dasturni ishga tushirish paytida bloklanadi. U erda 2 ta natijalar: foydalanuvchi muvaffaqiyatli login qiladi va asosiy ramkangizni ko'radi yoki u tizimga kirishni o'chiradi va IllegalStateException tashlanadi.

6
qo'shib qo'ydi
Yordamingiz uchun tashakkur ....
qo'shib qo'ydi muallif Ray Goodwin, manba