我正在尝试创建一个应用程序的登录窗口。我一整天都在寻找一个例子,但我似乎找不到任何有帮助的东西。我的基本结构如下:
// App.scala
object App extends SimpleSwingApplication {
val ui = new BorderPanel {
//content
}
def top = new MainFrame {
title = "title"
contents = ui
}
}那么创建一个没有大型机的登录框的策略是什么,在登录和显示大型机之后显示和关闭它。谢谢
发布于 2011-10-28 04:29:01
这是一个有效的例子。取自我的一个项目,并为您做了一点调整:
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。
https://stackoverflow.com/questions/7921182
复制相似问题