tornadofx使用imageview顯示圖片,使用chooseFile選擇圖片

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
        }
    }
}
相關文章
相關標籤/搜索