這是無證的,但似乎工作,並且是「純FX」的解決方案,而不是依靠java.awt中的API或明知外部可執行文件的路徑。撥打Application.getHostServices().showDocument(...)
方法,並通過mailto:url作爲網址:
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class OpenDefaultBrowser extends Application {
@Override
public void start(Stage primaryStage) {
final HBox root = new HBox(5);
final TextField textField = new TextField("[email protected]");
final Button goButton = new Button("Mail");
EventHandler<ActionEvent> goHandler = new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
getHostServices().showDocument("mailto:"+textField.getText());
}
};
textField.setOnAction(goHandler);
goButton.setOnAction(goHandler);
root.getChildren().addAll(textField, goButton);
final Scene scene = new Scene(root, 250, 150);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}