JavaFX 命令 link 对话框

JavaFX command link dialog

我想制作与本教程中描述的完全相同的对话框 http://code.makery.ch/blog/javafx-8-dialogs/。 对话框名为 "Command Link Dialog"。

不幸的是,由于这些对话框位于 JDK 8u40.

中,因此本教程已被弃用

有没有一种简单的方法可以使用没有 ControlsFX 的新 JDK 制作相同的对话框?

首先创建您自己的类似于 CommandLink 的按钮图形。在我的示例中,我使用 FontAwesomeFX 创建绿色箭头图标:

GridPane graphicGrid = new GridPane();
graphicGrid.setHgap(5);
graphicGrid.setVgap(5);

// Create an icon using FontAwesomeFX.
Text icon = GlyphsDude.createIcon(FontAwesomeIcon.ARROW_RIGHT);
icon.setFill(Color.GREEN);

Label headerLabel = new Label("Go to the Circus");
headerLabel.setStyle("-fx-font-size: 1.5em;");

Label detailsLabel = new Label("Watch acrobats fly around and clowns, of course.");

graphicGrid.add(icon, 0, 0);
graphicGrid.add(headerLabel, 1, 0);
graphicGrid.add(detailsLabel, 1, 1);

Button button = new Button("", graphicGrid);

然后使用 JavaFX dialogs tutorial 中的自定义对话框示例创建您自己的自定义对话框,并用您创建的按钮填充它。这是一个完整的例子:

import de.jensd.fx.glyphs.GlyphsDude;
import de.jensd.fx.glyphs.fontawesome.FontAwesomeIcon;
import javafx.application.Application;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Dialog;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class MCVE extends Application {
    @Override
    public void start(Stage stage) {
        Dialog dialog = new Dialog();

        GridPane content = new GridPane();
        content.setHgap(10);
        content.setVgap(5);

        Text headerIcon = GlyphsDude.createIcon(FontAwesomeIcon.INFO_CIRCLE, "3em");
        headerIcon.setFill(Color.BLUE);

        Label headerLabel = new Label("Where do you want to go?");
        headerLabel.setStyle("-fx-font-size: 1.5em;");

        CommandLink zooButton = new CommandLink("Go to the Zoo",
                "Here you will see zebras, monkeys, lions, elephants, and maybe also an alligator.");
        zooButton.setOnAction(e -> dialog.close());

        CommandLink circusButton = new CommandLink("Go to the Circus",
                "Watch acrobats fly around and clowns, of course.");
        circusButton.setOnAction(e -> dialog.close());

        CommandLink homeButton = new CommandLink("Stay Home", "Watch TV or play some board games with your siblings.");

        // To enable closing of the dialog. We need to add a hidden close
        // button. See this thread:
        // 
        dialog.getDialogPane().getButtonTypes().add(ButtonType.CLOSE);
        Node closeButton = dialog.getDialogPane().lookupButton(ButtonType.CLOSE);
        closeButton.managedProperty().bind(closeButton.visibleProperty());
        closeButton.setVisible(false);

        content.add(headerIcon, 0, 0);
        content.add(headerLabel, 1, 0);
        content.add(zooButton, 1, 1);
        content.add(circusButton, 1, 2);
        content.add(homeButton, 1, 3);

        dialog.getDialogPane().setContent(content);

        dialog.showAndWait();
    }

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

    }

    class CommandLink extends Button {
        public CommandLink(String header, String text) {
            GridPane graphicGrid = new GridPane();
            graphicGrid.setHgap(5);
            graphicGrid.setVgap(5);

            // Create an icon using FontAwesome.
            Text icon = GlyphsDude.createIcon(FontAwesomeIcon.ARROW_RIGHT);
            icon.setFill(Color.GREEN);

            Label btnHeaderLabel = new Label(header);
            btnHeaderLabel.setStyle("-fx-font-size: 1.5em;");

            Label detailsLabel = new Label(text);

            graphicGrid.add(icon, 0, 0);
            graphicGrid.add(btnHeaderLabel, 1, 0);
            graphicGrid.add(detailsLabel, 1, 1);

            setGraphic(graphicGrid);
        }
    }
}

结果如下:一个看起来有点像命令 Link 对话框的对话框。您将不得不尝试自己设计样式以使其完美。如果您想要原始信息图标,也可以使用官方对话框中的信息对话框。