Scala Swing 新手 [英] Scala Swing Newbie

查看:39
本文介绍了Scala Swing 新手的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为正在执行的应用程序创建登录窗口.我已经搜索了一整天的例子,但我似乎找不到任何有帮助的东西.我的基本结构如下:

Im trying to create an login window for an app im doing. I have searched all day for an example but I cant seem to find anything that helps. My basic structure is as follows:

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

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

那么在登录并显示主机后创建一个没有主机显示和关闭它的登录框的策略是什么.谢谢

So whats the strategy to create a login box without the mainframe showing and closing it after login and displaying the mainframe. thanks

推荐答案

这是工作示例.从我的一个项目中获取并为您进行了一些调整:

Here is working example. Took it from one of my projects and adjusted a little bit for you:

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()
}

如您所见,我通常使用模态对话框,因此它会在应用程序初始化期间阻塞.有两种结果:要么用户成功登录并看到您的主框架,要么他关闭登录对话框并抛出 IllegalStateException.

As you can see I'm generally using modal dialog, so it will block during application initialization. There 2 outcomes: either user makes successful login and sees your main frame or he closes login dialog and IllegalStateException would be thrown.

这篇关于Scala Swing 新手的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆