gpt4 book ai didi

Scala Swing 新手

转载 作者:行者123 更新时间:2023-12-01 11:04:00 29 4
gpt4 key购买 nike

我正在尝试为我正在做的应用程序创建一个登录窗口。我整天都在寻找一个例子,但我似乎找不到任何有帮助的东西。我的基本结构如下:

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

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

那么创建一个登录框不显示主机,登录显示主机后关闭的策略是什么?谢谢

最佳答案

这是工作示例。从我的一个项目中取出并为您稍微调整一下:

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

关于Scala Swing 新手,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7921182/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com