You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JavaFX:如何在中文Windows 7下让FileChooser按钮显示英文?

How to Display English Button Text in JavaFX FileChooser on Chinese Windows 7

Got it, let's break down how to handle this. Since JavaFX's FileChooser pulls button text directly from your Windows 7 system's native dialog (set to Chinese here), there's no direct way to override this via JavaFX's core APIs. But don't worry—here are a few practical workarounds:

1. Build a Custom File Selection Dialog

The most reliable way is to create your own file picker using standard JavaFX controls. This lets you fully control every text element, including button labels.

Here's a minimal example to get you started:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.DirectoryChooser;
import javafx.stage.Stage;
import java.io.File;

public class CustomFileChooser extends Application {
    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Custom File Chooser");
        
        Label selectedFileLabel = new Label("No file selected");
        Button openButton = new Button("Open");
        Button cancelButton = new Button("Cancel");
        
        // Use DirectoryChooser to get base directory, expand with TreeView for full file browsing
        DirectoryChooser dirChooser = new DirectoryChooser();
        openButton.setOnAction(e -> {
            File selectedDir = dirChooser.showDialog(primaryStage);
            if (selectedDir != null) {
                selectedFileLabel.setText("Selected: " + selectedDir.getAbsolutePath());
            }
        });
        
        cancelButton.setOnAction(e -> primaryStage.close());
        
        VBox layout = new VBox(10, selectedFileLabel, openButton, cancelButton);
        layout.setPadding(new javafx.geometry.Insets(20));
        
        primaryStage.setScene(new Scene(layout, 400, 300));
        primaryStage.show();
    }
    
    public static void main(String[] args) {
        launch(args);
    }
}

Pros: Full control over all UI text; no reliance on native system settings.
Cons: You'll need to add extra logic for filtering files, navigating directories, and handling different file types—this takes more code than using the native FileChooser.

2. Temporarily Switch JVM Locale Before Opening FileChooser

You can change the JVM's default locale to English right before launching the FileChooser, then switch it back afterward. This tricks the native dialog into using English text, though it might have side effects if other parts of your app depend on the system locale.

Example code:

import javafx.application.Application;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import java.util.Locale;

public class FileChooserLocaleFix extends Application {
    @Override
    public void start(Stage primaryStage) {
        // Save the original system locale
        Locale originalLocale = Locale.getDefault();
        
        try {
            // Switch to English locale temporarily
            Locale.setDefault(Locale.ENGLISH);
            
            // Launch the FileChooser
            FileChooser fileChooser = new FileChooser();
            fileChooser.showOpenDialog(primaryStage);
        } finally {
            // Restore the original locale to avoid breaking other app parts
            Locale.setDefault(originalLocale);
        }
    }
    
    public static void main(String[] args) {
        launch(args);
    }
}

Pros: Quick to implement; retains all native dialog functionality.
Cons: Might affect concurrent UI elements or locale-dependent logic in your app. Test thoroughly to avoid unexpected behavior.

3. Use a Third-Party Library (Optional)

Libraries like ControlsFX offer enhanced file selection components that support customizing button labels. Their alternative FileChooser and DirectoryChooser let you override text directly, though this adds an external dependency to your project.


内容的提问来源于stack exchange,提问作者Chiukpnick Chiukp

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 07:03:22