import javafx.application.Application
import javafx.beans.property.SimpleObjectProperty
import javafx.geometry.Pos
import javafx.scene.image.Image
import javafx.stage.FileChooser
import tornadofx.*
import java.io.File
fun main()= Application.launch(MytestApp::class.java)
class MytestApp:App(Mytest::class)
class Mytest : View("Mytest") {
val bigImage= SimpleObjectProperty<Image>()
override val root = borderpane {
prefWidth=500.0
prefHeight=500.0
paddingAll=10
top = hbox(10) {
button("选择图片") {
action {
// initView()
val imgType = listOf("*.jpg", "*.png", "*.bmp", "*.gif")
val efset = arrayOf(FileChooser.ExtensionFilter("$imgType", imgType))
val imgFile = chooseFile("选择图片", efset, FileChooserMode.Single) {
// p初始目录为当前项目目录
initialDirectory = File(File("c:/").canonicalPath)
}
if(imgFile.isNotEmpty()){
val imgPath = imgFile.first().toString().replace("\\", "/")
bigImage.value=Image(File(imgPath).inputStream())
}
}
}
button("清除图片") {
action {
bigImage.value=null
}
}
alignment=Pos.CENTER
}
center =vbox(10){
imageview(bigImage){
fitHeight=400.0
fitWidth=400.0
}
alignment=Pos.CENTER
}
}
}