|
1、新建LoginKit工具类
importjavafx.beans.property.SimpleBooleanPropertyimportjavafx.beans.property.SimpleObjectPropertyimportjavafx.beans.property.SimpleStringPropertyimportjavafx.scene.control.Buttonimportjavafx.scene.control.PasswordFieldimportjavafx.scene.control.TextFieldimportjavafx.scene.layout.Backgroundimportjavafx.scene.layout.BackgroundFillimportjavafx.scene.paint.Color/**
* object 对象,相当于java单利
*/objectUserName {valtext = SimpleStringProperty()
}//密码objectPassWord {valtext = SimpleStringProperty()
}//改变颜色objectBackGround {//设置颜色valblueColor = SimpleObjectProperty
(Background(BackgroundFill(Color.BLUE,null,null)))valcyanColor = SimpleObjectProperty(Background(BackgroundFill(Color.CYAN,null,null)))valisRightColor = SimpleBooleanProperty(false)
}/**
* 数据绑定
*/classLoginKit{//用户名字段的绑定funuserName(): TextField {returnTextField().apply {
textProperty().bindBidirectional(UserName.text)
}
}//密码字段的绑定funpassWord(): PasswordField {returnPasswordField().apply {
textProperty().bindBidirectional(PassWord.text)
}
}//登录按钮方法funloginBtn(): Button {returnButton("登录").apply {//通过事件获取用户名与密码的值setOnAction {
println("账号:${UserName.text.value}")
println("密码:${PassWord.text.value}")
}
}
}//清除按钮funclearBtn(): Button {returnButton("清除").apply {
setOnAction {
UserName.text.value =""PassWord.text.value =""}
}
}
}
2、新建登录视图类
这里使用kotlin的apply比较多,要有DSL的思想才好编写代码packagecom.rub.viewimportcom.rub.kit.BackGroundimportcom.rub.kit.LoginKitimportjavafx.application.Applicationimportjavafx.beans.binding.Whenimportjavafx.geometry.Insetsimportjavafx.geometry.Posimportjavafx.scene.Sceneimportjavafx.scene.control.Labelimportjavafx.scene.layout.BorderPaneimportjavafx.scene.layout.HBoximportjavafx.scene.layout.VBoximportjavafx.stage.StageclassLoginView:Application() {overridefunstart(stage:Stage?){//!!表示告诉kotlin编辑器,此值不要检查了。valst = stage!!varloginKit = LoginKit()//创建面板valborderPane = BorderPane().apply {
backgroundProperty().bind(
When(BackGround.isRightColor).then(BackGround.cyanColor).otherwise(BackGround.blueColor)
)//鼠标进入界面改成true,即为青色,鼠标离开界面改成false,即为蓝色,setOnMouseEntered { BackGround.isRightColor.value =true}
setOnMouseExited { BackGround.isRightColor.value =false}//内边距里padding = Insets(40.0)
center = VBox(20.0).apply {
alignment = Pos.CENTER
children.add(HBox().apply {
alignment = Pos.CENTER
children.addAll(Label("账号:"), loginKit.userName())
})
children.add(HBox().apply {
alignment = Pos.CENTER
children.addAll(Label("密码:"), loginKit.passWord())
})
children.add(HBox(60.0).apply {//两个按钮之间的距离translateX =10.0alignment = Pos.CENTER
children.addAll(loginKit.loginBtn(), loginKit.clearBtn())
})
}
}valscene = Scene(borderPane)
st.apply {this.scene = scene
sizeToScene()
title ="登录界面"//窗体不可变isResizable =falseshow()
}
}
}//启动入口类funmain()= Application.launch(LoginView::class.java)
3、运行结果
功能实现鼠标进入界面改成true,即为青色,鼠标离开界面改成false,即为蓝色,点击按钮获取到文本框的值
image.png
|
|