javafx警报对话框

t0ybt7op  于 2021-06-30  发布在  Java
关注(0)|答案(2)|浏览(416)

我正在尝试用javafx弹出警报。不过,我可以看到ok按钮在英语中,但取消按钮是在韩语表示,像这样。请让我知道如何用英语做取消按钮。这是我代码的一部分:

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();
}
lnvxswe2

lnvxswe21#

欢迎来到stackoverflow!:)
你能检查一下你的默认语言环境吗?因为我的默认值是“en-en”,我得到值“ok”和“cancel”按钮。但是如果我在应用程序的开头加上那一行,那么我的字符就会和你相似,请看附件的截图。

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


请运行以下代码,使按钮内有英文文本。
我的poc申请代码是:
主.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>
r6hnlfcb

r6hnlfcb2#

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

Locale.setDefault(Locale.ENGLISH);

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

相关问题