這裏是工作示例。從我的一個項目把它和調整你一點點:
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()
}
正如你看到的,我一般採用模態對話框,所以它會在應用程序初始化期間阻止。有2個結果:用戶成功登錄並看到您的主框架,或者他關閉登錄對話框並將拋出IllegalStateException
。
感謝您的幫助.... –