0

我正在尝试使用 kotlin 的桌面撰写进行一些练习,当我尝试实现AlertDialog元素时,我遇到了这个异常:

线程“AWT-EventQueue-0 @coroutine#3”中的异常 kotlin.NotImplementedError:未实现操作:未实现

我试图运行的代码:

fun main() {
    var text = mutableStateOf(0F)
    var num = mutableStateOf("a")
    Window(
        size = IntSize(500, 500),
        title = num.value,
        menuBar = MenuBar(
            Menu(
                name = "Test",
                MenuItem(
                    name = "Dodaj random",
                    onClick = {
                        text.value = text.value + Random.nextFloat()
                        if (text.value > 1F) {
                            text.value = 0F
                            num.value += "a"
                        }
                    },
                    shortcut = KeyStroke(Key.A)
                ),
                MenuItem(
                    name = "Exit",
                    onClick = {
                        AppManager.exit()
                    },
                    shortcut = KeyStroke(Key.L)
                )
            )
        )
    ) {

        MaterialTheme {
            Column(Modifier.fillMaxSize(), Arrangement.spacedBy(12.dp)) {
                LinearProgressIndicator(text.value.toFloat(), modifier = Modifier.align(Alignment.CenterHorizontally))
                val openDialog = remember { mutableStateOf(false) }
                Button(
                    onClick = {
                        openDialog.value = true
                    }) {
                    Text("Click me!")
                }
                if (openDialog.value) {
                    AlertDialog(
                        onDismissRequest = { openDialog.value = false },
                        title = { Text("Dialog title") },
                        text = { Text("Here is a text") },
                        confirmButton = {
                            Button(
                                onClick = {
                                    openDialog.value = false
                                }
                            ) { Text("Confirm butt") }
                        },
                        dismissButton = {
                            Button(
                                onClick = {
                                    openDialog.value = false
                                }
                            ) { Text("Diss butt") }
                        }
                    )
                }
                Button(
                    onClick = {
                        text.value += 0.1F
                        if (text.value > 1F) {
                            text.value = 0F
                            num.value += "a"
                        }
                    },
                    modifier = Modifier.align(Alignment.CenterHorizontally)
                ) {
                    Text(text.value.toString())
                }

                Button(
                    onClick = {
                        num.value += "a"
                    },
                    modifier = Modifier.align(Alignment.CenterHorizontally)
                ) {
                    Text(num.component1())
                }


            }
        }
    }
}

只有当我尝试单击“单击我”按钮时,我才会收到此异常,其他按钮才能正常工作。

我的进口

import androidx.compose.desktop.AppManager
import androidx.compose.desktop.Window
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.Button
import androidx.compose.material.LinearProgressIndicator
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.mutableStateOf
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.input.key.Key
import androidx.compose.ui.unit.IntSize
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.KeyStroke
import androidx.compose.ui.window.Menu
import androidx.compose.ui.window.MenuBar
import androidx.compose.ui.window.MenuItem
import kotlin.random.Random
4

1 回答 1

0

您的问题似乎与github 问题有关。

要解决它,只需将 compose 依赖项更新为 0.2.0-build132。

具有正确版本的我的 build.gradle 的完整示例:

import org.jetbrains.compose.compose

plugins {
    kotlin("jvm") version "1.4.20"
    id("org.jetbrains.compose") version "0.2.0-build132"
}

repositories {
    jcenter()
    maven("https://maven.pkg.jetbrains.space/public/p/compose/dev")
}

dependencies {
    implementation(compose.desktop.currentOs)
}

compose.desktop {
    application {
        mainClass = "MainKt"
    }
}
于 2020-12-02T11:56:25.527 回答