如何设置文本对齐方式

时间:2013-06-08 08:44:19

标签: javafx-2 javafx javafx-8

我有这个带文字的对话框。

public class DX57DC extends Application
{

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

    @Override
    public void start(Stage primaryStage) throws URISyntaxException, MalformedURLException
    {

        // Image
        ImageView iv = new ImageView(getClass().getResource("system-help.png").toExternalForm());

        // Text
        Text t = new Text();
        t.setTextAlignment(TextAlignment.LEFT);
        t.setText("Do you want to quit?");

        HBox thbox = new HBox(8); // spacing = 8
        thbox.getChildren().addAll(thbox);
        thbox.setAlignment(Pos.CENTER);

        // Buttons
        Button btnYes = new Button("Yes");
        Button btnNo = new Button("No");
        btnYes.setStyle("-fx-background-color:\n"
                + "        rgba(0,0,0,0.08),\n"
                + "        linear-gradient(#9a9a9a, #909090),\n"
                + "        linear-gradient(white 0%, #f3f3f3 50%, #ececec 51%, #f2f2f2 100%);\n"
                + "    -fx-background-insets: 0 0 -1 0,0,1;\n"
                + "    -fx-background-radius: 5,5,4;\n"
                + "    -fx-padding: 10 26 10 26;\n"
                + "    -fx-text-fill: #242d35;\n"
                + "    -fx-font-size: 14px;");

        btnNo.setStyle("-fx-background-color:\n"
                + "        rgba(0,0,0,0.08),\n"
                + "        linear-gradient(#9a9a9a, #909090),\n"
                + "        linear-gradient(white 0%, #f3f3f3 50%, #ececec 51%, #f2f2f2 100%);\n"
                + "    -fx-background-insets: 0 0 -1 0,0,1;\n"
                + "    -fx-background-radius: 5,5,4;\n"
                + "    -fx-padding: 10 26 10 26;\n"
                + "    -fx-text-fill: #242d35;\n"
                + "    -fx-font-size: 14px;");

        // Image layout
        VBox vbox = new VBox(7);
        vbox.getChildren().addAll(iv);
        vbox.setAlignment(Pos.CENTER);

        // Buttons layout
        HBox hbox = new HBox(8); // spacing = 8
        //hbox.setStyle("-fx-padding: 10; -fx-font-size: 10pt;");
        hbox.getChildren().addAll(btnYes, btnNo);
        hbox.setAlignment(Pos.CENTER);

        BorderPane bp = new BorderPane();
        bp.setStyle("-fx-background-color: linear-gradient(#ffffff,#f3f3f4);\n"
                + "    -fx-border-width: 1 1 1 1;\n"
                + "    -fx-border-color: #b4b4b4 transparent #b4b4b4 transparent;\n"
                + "    -fx-font-size: 1.083333em;\n"
                + "    -fx-text-fill: #292929;");

        bp.setPadding(new Insets(15, 20, 15, 20));
        //Button btnTop = new Button("Top");
        bp.setTop(null);
        //Button btnLeft = new Button("Left");
        bp.setLeft(vbox);
        //Button btnCenter = new Button("Center");
        bp.setCenter(t);
        //Button btnRight = new Button("Right");
        bp.setRight(null);
        //Button btnBottom = new Button("Bottom");
        bp.setBottom(hbox);
        Scene scene = new Scene(bp, 500, 160);
        primaryStage.setScene(scene);
        primaryStage.setTitle("Question Dialog");
        primaryStage.show();
    }
}

你能告诉我如何将文字移到图片附近。现在,文本位于边框窗格的中心。

1 个答案:

答案 0 :(得分:5)

与ImageView相同,将其包装在VBox中:)

VBox vbox2 = new VBox();
vbox2.getChildren().addAll(t);
vbox2.setAlignment(Pos.CENTER_LEFT);

然后将其添加到borderpane的中心:

bp.setCenter(vbox2);