[Kotlin] 用Kotlin和Swing实现VB6中的MsgBox

VB6中的MsgBox

VB6 中有一个十分好用的过程MsgBox

MsgBox(Prompt, Button, Title,)

可以用来编写一些简单的对话框

Private Sub Command1_Click()
    Dim x As Integer
    x = MsgBox("你好", vbYesNo)
    If x = VbMsgBoxResult.vbYes Then
        MsgBox ("你点击了确定")
    End If
End Sub

在Swing中,并没有这个函数,我们来模拟一个

/**
 * @author Winterreisender
 * @license Apache-2.0
 */
import java.awt.BorderLayout
import java.util.concurrent.Semaphore
import javax.swing.*

// 垃圾Java,连MsgBox都没有

object MsgBox {
    /**
     * MsgBox not like VB with callback
     *
     * 垃圾Java,连MsgBox都没有
     *
     * @author Winterreisender
     * @param text The text to show in Dialog
     * @param title The title of JDialog window
     * @param owner The parent JFrame of Dialog
     * @param yesText The text of "Yes" button
     * @param noText The text of "No" button
     * @param onYes callback when "Yes" clicked
     * @param onNo callback when "No" clicked
     * @return A showing JDialog with Yes and No button
     */
    fun msgBoxAsync(
        text: String,
        title: String = "",
        owner: JFrame? = null,
        yesText: String = "确认",
        noText: String? = null,
        onYes: () -> Unit = {},
        onNo: () -> Unit = {}
    ) = JDialog(owner, title).apply {
        layout = BorderLayout()
        defaultCloseOperation = WindowConstants.DISPOSE_ON_CLOSE
        setLocationRelativeTo(owner)


        JLabel(text, JLabel.CENTER)
            .also { add(it, BorderLayout.CENTER) }

        Box(BoxLayout.X_AXIS).apply {
            Box.createHorizontalGlue().also(::add)
            JButton(yesText).apply {
                pack()
                addActionListener {
                    onYes()
                    dispose()
                }
            }.also(::add)
            if (noText != null) JButton(noText).apply {
                pack()
                addActionListener {
                    onNo()
                    dispose()
                }
            }.also(::add)
            Box.createHorizontalGlue().also(::add)


            pack()
            isVisible = true
        }.also { add(it, BorderLayout.SOUTH) }
    }.apply { pack(); isVisible = true }

    /**
     * Synchronized MsgBox like VB
     *
     * 垃圾Java,连MsgBox都没有
     *
     * @author Winterreisender
     * @param text The text to show in Dialog
     * @param title The title of JDialog window
     * @param owner The parent JFrame of Dialog
     * @param yesText The text of "Yes" button
     * @param noText The text of "No" button
     * @return A showing JDialog with Yes and No button
     */
    fun msgBox (
        text: String,
        title: String = "",
        owner: JFrame? = null,
        yesText: String = "确认",
        noText: String = "取消"
    ) :Boolean {
        var r: Boolean = false
        val sem = Semaphore(0)
            MsgBox.msgBoxAsync(text, title, owner, yesText, noText, { r = true;sem.release() }, { r = false; sem.release() })
        sem.acquire()
        return r
    }
}

fun main() {
    UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel")
	val r = MsgBox.msgBox("Text")
	if(r) {
		println("你点击了确定")
	}
    
}

上一篇:如何使C#COM类支持VB6中的参数化属性


下一篇:二叉树和尾递归