2011-10-27 162 views
3

我試圖創建一個即時通訊應用程序的登錄窗口。我整天都在搜索一個例子,但我似乎找不到任何有用的東西。我的基本結構如下:斯卡拉鞦韆新手

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

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

所以沒有什麼大型機的表現來創建一個登錄框的戰略和登錄後關閉它,並顯示主機。謝謝

回答

6

這裏是工作示例。從我的一個項目把它和調整你一點點:

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

+0

感謝您的幫助.... –