Tornadofx是基於JavaFx的一個kotlin實現的框架
之后看情況補充..
1.讀取resources文件夾中的文件
如圖
想要讀取config.properties文件,有兩種方法
在class文件中,可以這樣寫
//獲得輸入流
val resourceAsStream = javaClass.classLoader.getResourceAsStream("config.properties")
在MainView.kt文件,可以這樣寫:
//需要 “/”
val steam = resources.stream("/config.properties")
2.設置窗口的標題、圖標、窗口大小
新建一個resources文件夾,將此文件夾設置為resources文件夾
class MainView : View() {
override val root = vbox {
//設置標題
title = "這是標題"
//設置圖標
addStageIcon(Image("img/file.png"))
//設置窗口大小
setPrefSize(500.0, 400.0)
}
}
開發者推薦寫在init
方法里:
class MainView : View() {
init {
title = "APK簽名驗證破解工具 by star-sone"
setStageIcon(Image("img/file.png"))
}
override val root = vbox {
//設置窗口大小
setPrefSize(500.0, 400.0)
}
}
設置標題還可以這樣設置:
class MainView : View("這是標題") {
override val root = vbox {
...
}
}
3.文字與按鈕
文字的常用樣式設置
text("hello"){
//設置方向
alignment = Pos.TOP_CENTER
style {
//設置加粗
fontWeight = FontWeight.BOL
//字體大小,第二個參數是單位,一個枚舉類型
fontSize = Dimension(18.0, Dimension.LinearUnits.px)
//設置顏色,c方法是tornadofx中的提取顏色方法
fill= c("red")
}
}
文字按鈕
override val root = vbox {
button("按鈕")
}
按鈕點擊
override val root = vbox {
button("按鈕"){
//設置最小寬度使用默認,不然會出現省略號
setMinSize(Button.USE_PREF_SIZE, Button.USE_PREF_SIZE)
action{
println("點擊按鈕")
}
}
}
圖片按鈕
override val root = vbox {
button(){
graphic = imageview("img/file.png") {
//設置大小
fitHeight = 30.0
fitWidth = 30.0
//設置圖片按比例縮放,只設置fitHeight或fitWidth即可有效果
isPreserveRatio=true
}
setOnAction{
println("點擊按鈕")
}
}
}
按鈕樣式
override val root = vbox {
button("按鈕"){
style {
//設置背景顏色
backgroundColor += c("#66bef1")
//設置按鈕文字顏色
textFill = c("white")
//粗體
fontWeight = FontWeight.BOLD
//字體大小,第二個參數是單位,一個枚舉類型
fontSize = Dimension(20.0, Dimension.LinearUnits.px)
}
setOnAction{
println("點擊按鈕")
}
}
}
按鈕設置鼠標滑過陰影
jfxbutton("按鈕"){
setOnMouseEntered { style { backgroundColor += c(0, 0, 0, 0.1)
//圓形陰影
//backgroundRadius += box(20.percent)
} }
setOnMouseExited { style {} }
}
4.表單
登錄界面
override val root = vbox {
form{
fieldset {
field("用戶名") {
textfield()
}
field("密碼") {
passwordfield()
}
}
}
}
獲取輸入框的數值
class MainView : View("Hello TornadoFX") {
//兩種寫法都可以
var userTf: TextField by singleAssign()
var passwordTf by singleAssign<TextField>()
override val root = vbox {
form{
fieldset {
field("用戶名") {
userTf = textfield()
}
field("密碼") {
passwordTf = passwordfield()
}
field(){
button("登錄"){
println(usetTf.text)//用戶名
println(passwordTf.text)//密碼
//登錄操作...
}
}
}
}
}
輸入框取消默認選中
JavaFx中,第一個輸入框TextField會默認被選中,有時候不希望被選中,可以設置isFocusTraversable = false
,如果希望全部輸入框不被選中,則全部的輸入框都是設置上面的那個屬性
class MainView : View("Hello TornadoFX") {
//兩種寫法都可以
var userTf: TextField by singleAssign()
var passwordTf by singleAssign<TextField>()
override val root = vbox {
form{
fieldset {
field("用戶名") {
userTf = textfield(){
//設置此屬性
isFocusTraversable = false
}
}
field("密碼") {
passwordTf = passwordfield()
}
field(){
button("登錄"){
println(usetTf.text)//用戶名
println(passwordTf.text)//密碼
//登錄操作...
}
}
}
}
}
表單驗證
//創建驗證器
val context = ValidationContext()
//需要驗證的控件,一般是對輸入框
val input = TextField()
//驗證是否符合條件,輸入框不能為空
val validator = context.addValidator(input, input.textProperty()) {
//這里的it就是代表了該輸入框的數值
if(it.isNullOrBlank()){
//輸入框為空,則彈出此信息
error("輸入不能為空")
}else{
null
}
}
//返回是否通過驗證的結果,這里可以在button按鈕的點擊事件觸發
val result = context.validtate()
固定大小的文本
有時候需要固定大小的文本,文字超過此寬度,后面的會以省略號來隱藏,這個時候可以使用Label,不過考慮到用戶友好度,我們可以加個懸浮窗以便用戶可以查看文本的全部內容
label("hello"){
prefWidth = 100.0
//鼠標懸浮在上面可以顯示提示框
tooltip = Tooltip(this.text)
}
5.居中
override val root = vbox {
hbox{
alignment = Pos.CENTER
button("按鈕")
}
}
我沒有設置hbox大小,所有hbox大小和button高度一樣,實現了水平居中
6.單選框
我這里用了Kfoenix,jfxradiobutton是Kfoenix中的控件,jfxradiobutton和radiobutton一樣
override val root = vbox {
togglegroup {
jfxradiobutton("選項1") {
isSelected = true
setOnAction {
}
}
jfxradiobutton("選項2") {
setOnAction {
}
}
jfxradiobutton("選項3") {
setOnAction {
}
}
}
}
7.菜單欄
override val root = vbox {
menubar {
menu("幫助") {
item("關於") {
//設置點擊事件
setOnAction {
}
}
}
menu("文件"){
}
}
...
}
8.打開新窗口
內部窗口
button("Open editor") {
action {
openInternalWindow(Editor::class)
}
}
新窗口
find(AboutView::class).openModal()
9.View傳遞數據
10.顯示隱藏
比如有一個按鈕默認是禁用的,只有當某個單選框被選中的時候,,這個按鈕才會啟用
val radiobutton = radiobutton(){
}
button{
disable{
radiobutton.isSelected
}
}
11.實時視圖
由於是使用kotlin來開發界面,所有,沒有像之前那樣使用fxml可以直接預覽界面,但是,開發提供了一個實時視圖,方便我們在debug模式可以快速看到界面,無需重啟應用程序
配置
有兩種配置方式
1.MyApp中添加如下代碼
class MyApp: App(MainView::class,Styles::class){
init {
reloadViewsOnFocus()
}
}
2.在debug配置中添加一個參數--alive-views
使用
修改View,之后按下Ctrl+F9,等待重載完成,應用的界面就會發生變化
PS:同理還有個--live-stylesheets
參數(對應reloadStylesheetsOnFocus方法),用來開啟樣式的熱更新
12.css樣式
內聯樣式
override val root = vbox {
button{
style {
backgroundColor += c("blue")
}
}
}
定義css文件
package com.wan.noveldownloader.app
import javafx.scene.text.FontWeight
import tornadofx.*
class Styles : Stylesheet() {
companion object {
val MyTab by cssclass()
}
init {
MyTab{
backgroundColor += c("#64b7ea")
//偽標簽,懸浮
and(hover){
backgroundColor+=c("#6495ED")
}
}
}
}
使用addClass方法添加
button{
addClass(Styles.MyTab)
}
13.單選框 RadioButton
點擊按鈕輸出選中的radioButton的userData數據
PS:userData可以是任意類型
import javafx.scene.control.ToggleGroup
import tornadofx.*
class TestView : View("My View") {
var toggle by singleAssign<ToggleGroup>()
override val root = vbox {
toggle = togglegroup {
radiobutton("是") {
userData = 0
}
radiobutton("否") {
isSelected = true
userData = 1
}
}
button {
action {
//輸出選中button的userData數據
println(toggle.selectedToggle.userData)
}
}
}
}