JavaFX 警报对话框

JavaFX Alert Dialog

我正在尝试在 javaFX 中制作警报弹出窗口。但是,我可以看到英文的 OK 按钮,但是 CANCEL 按钮用韩语表示,例如 this。请让我知道如何用英文制作取消按钮。 这是我的代码的一部分:

Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.setTitle("Message");
alert.setHeaderText("Are you sure you want to clear all nodes?");
Optional<ButtonType> result = alert.showAndWait();
if (result.get().equals(ButtonType.OK)) { 
       gc.clearRect(0, 0, canvas.getWidth(), canvas.getHeight());
       bst.clear(); 
       height = 20;
       width = 740;
       ratiowidth = 740;
}else if (result.get().equals(ButtonType.NO)) {
       alert.close();
}

欢迎来到 Whosebug! :)

你能检查一下你的默认语言环境是什么吗?因为我的默认值是“en-EN”并且开箱即用,所以我得到值“OK”和“Cancel”按钮。但是,如果我在应用程序的开头添加该行,那么我的字符将与您相似,请参阅所附的屏幕截图。

Locale.setDefault(new Locale("ja", "Japan"));

请运行下面的代码让按钮里面有英文文本。

我的POC申请代码是:

Main.java

package sample;

import javafx.application.Application;
import javafx.scene.control.Alert;
import javafx.stage.Stage;

import java.util.Locale;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        //Locale.setDefault(new Locale("ja", "Japan"));
        Locale.setDefault(new Locale("en", "English"));

        Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
        alert.setTitle("Message");
        alert.setHeaderText("Are you sure you want to clear all nodes?");
    }


    public static void main(String[] args) {
        launch(args);
    }
}

pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>al.musi.jacek</groupId>
    <artifactId>javafx-alert-dialog</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>15.0.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.openjfx/javafx-fxml -->
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-fxml</artifactId>
            <version>15.0.1</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.openjfx</groupId>
                <artifactId>javafx-maven-plugin</artifactId>
                <version>0.0.5</version>
                <configuration>
                    <mainClass>C:\Users\re\Documents\GitHub\javafx-alert-dialog\src\main\java\sample\Main.java</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

另一个答案太长了。你只需要

Locale.setDefault(Locale.ENGLISH);

创建对话框之前的任何位置,最好是在 main 或 start 方法的顶部。