无法修复Java FX中GridPane周围的样式边框

时间:2018-11-28 19:05:56

标签: java javafx

我正在用Java 8创建Boggle游戏(使用Java FX创建GUI),但似乎无法弄清楚如何在GridPane(16个字母的骰子)周围设置固定边框,以便不根据窗口调整自身大小。

我已经相应地调整了窗口的大小(以容纳用户可以创建的大量单词),即使我已经应用CSS样式(-fx),我添加到GridPane中的白色细边框仍在继续调整自身大小-border-color和-fx-border-width),而不是包含游戏所有子级的BorderPane主容器。 GridPane位于BorderPane的居中位置。

有什么想法吗?

enter image description here

1 个答案:

答案 0 :(得分:1)

如果外部窗格也是GridPane,则可以将行的valignment和列的halignment设置为CENTER(版本A)。或者,将内部GridPane包装为HBoxVBox并将它们各自的alignment属性设置为CENTER(版本B)。两者都会导致相同的行为:

outcome

Main.java

package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(final Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        primaryStage.setScene(new Scene(root, 300, 275));
        primaryStage.show();
    }


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

sample.fxml版本A

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>

<GridPane gridLinesVisible="true" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity"
          prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/10.0.1">
    <columnConstraints>
        <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0"/>
        <ColumnConstraints halignment="CENTER" hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0"/>
        <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0"/>
    </columnConstraints>
    <rowConstraints>
        <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES"/>
        <RowConstraints minHeight="10.0" prefHeight="30.0" valignment="CENTER" vgrow="SOMETIMES"/>
        <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES"/>
    </rowConstraints>
    <GridPane gridLinesVisible="true" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity"
              minWidth="-Infinity" prefHeight="50.0" prefWidth="50.0"
              style="-fx-border-color: #ff00ff; -fx-border-width: 1;" GridPane.columnIndex="1" GridPane.rowIndex="1">
      <columnConstraints>
          <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0"/>
          <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0"/>
          <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0"/>
          <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0"/>
      </columnConstraints>
      <rowConstraints>
          <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES"/>
          <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES"/>
          <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES"/>
          <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES"/>
      </rowConstraints>
    </GridPane>
</GridPane>

sample.fxml版本B

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.VBox?>

<GridPane gridLinesVisible="true" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1">
    <columnConstraints>
        <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
        <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
        <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
    </columnConstraints>
    <rowConstraints>
        <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
        <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
        <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
    </rowConstraints>
    <HBox alignment="CENTER" GridPane.columnIndex="1" GridPane.rowIndex="1">
        <VBox alignment="CENTER">
            <GridPane gridLinesVisible="true" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity"
                      minWidth="-Infinity" prefHeight="50.0" prefWidth="50.0"
                      style="-fx-border-color: #ff00ff; -fx-border-width: 1;">
              <columnConstraints>
                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0"/>
                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0"/>
                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0"/>
                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0"/>
              </columnConstraints>
              <rowConstraints>
                  <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES"/>
                  <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES"/>
                  <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES"/>
                  <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES"/>
              </rowConstraints>
            </GridPane>
        </VBox>
    </HBox>
</GridPane>