如何用AWT+KOTLIN打造一款属于自己的GUI工具

最简单的

首先写一个shell的工具类

不用写了我写好了 欢迎 https://github.com/ElisaMin/kotlinx-adb-fastboot/blob/master/kotlinx/heizi/utills/Utills.kt
所以说下面是使用的教程

首先把AWT的onPress和onClose 写好

import java.awt.event.MouseListener as MouseListenerInterface
import java.awt.event.WindowListener as WindowListenerInterface
//实现接口
open class MouseListener : MouseListenerInterface{
    override fun mouseReleased(e: MouseEvent?) {}
    override fun mouseEntered(e: MouseEvent?) {}
    override fun mouseClicked(e: MouseEvent?) {}
    override fun mouseExited(e: MouseEvent?) {}
    override fun mousePressed(e: MouseEvent?) {}
}
open class WindowListener : WindowListenerInterface{
    override fun windowDeiconified(e: WindowEvent?) {}
    override fun windowClosing(e: WindowEvent?) {}
    override fun windowClosed(e: WindowEvent?) {}
    override fun windowActivated(e: WindowEvent?) {}
    override fun windowDeactivated(e: WindowEvent?) {}
    override fun windowOpened(e: WindowEvent?) {}
    override fun windowIconified(e: WindowEvent?) {}
}
//用lambda表达式传入方法
fun closeListener(onClose:(WindowEvent?)->Unit):WindowListener{
    return object : WindowListener(){
        override fun windowClosing(e: WindowEvent?) {
            super.windowClosing(e)
            onClose(e)
        }
    }
}
//扩展函数写Panel的函数
fun Panel.addButton(label: String,doSomething:Button.()->Unit) = this.apply {
    add(Button(label).apply {
        addMouseListener(object : MouseListener() {
            override fun mousePressed(e: MouseEvent?) {
                super.mousePressed(e)
                doSomething()
            }
        })
    })
}

开始布局

如果用xml表达这个布局的话大致是这样的

<frame>
    <panel>
        <button text="按钮1"/>
        <button text="按钮2"/>
    </panel>
</frame>

所以说利用作用域函数翻译一下是

Frame().apply{
    Panel().apply{
        add(Button("按钮一"))
        add(Button("按钮二"))
    }.let{
        add(it)
    }
}

也可以写多个扩展函数如add panel/add button

fun addFrame(doSomething: Frame.() -> Unit):Frame = Frame().apply(doSomething)

fun Frame.addPanel(doSomething: Panel.() -> Unit):Panel{
    return Panel().also{
        add(it)
    }.apply(doSomething)
}
fun Panel.addButton(label: String,doSomething:Button.()->Unit) = this.apply {
    add(Button(label).apply {
        addMouseListener(object : MouseListener() {
            override fun mousePressed(e: MouseEvent?) {
                super.mousePressed(e)
                doSomething()
            }
        })
    })
}

用起来就像这样

fun main {
    addFrame {
        addPanel {
            addButton("按钮一"){ }
            addButton("按钮二"){ }
        }
    }
}

加点细节

import kotlinx.heizi.utills.PlatformTools.ADB

val adb = ADB
fun main {
    addFrame {
        addPanel {
            addButton("把laf_a用dd指令格式化"){
                val (isSuccess,message) = adb shell {"su -c dd if=/dev/zero of=/dev/block/boootdevice/by-name/laf_a bs=512 count=98432"}
            }
        }

        title ="测试用的小玩意"
        addWindowListener(closeListener { System.exit(0) })
        setBounds(10,20,200,300)
        isVisible = true

    }
}

完成

results matching ""

    No results matching ""