在javafx的gridpane中的窗格上放置标签

时间:2017-11-08 20:38:49

标签: java javafx gridpane

我想以编程方式将标签放在我的窗格中。我有一个11x12的网格窗格。这些字段位于外部字段中。 我需要实现的是将我的MonopolyConstants.FIELD_NAMES [I]添加到必须在窗格中的标签。

到目前为止,我的代码看起来像这样:

package gui;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import monopoly_import.MonopolyConstants;

/**
* FXML Controller class
*
* @author nj
*/

public class GUIController implements Initializable {

@FXML private GridPane grindPane;
@FXML private Label field0;

/**
 * Initializes the controller class.
 *
 * @param url
 */
@Override
public void initialize(URL url, ResourceBundle rb) {
    GridPane gridPane = new GridPane();
    gridPane.add(new Label("Item listing"), 0, 0);

    for (int i = 0; i < 1; i++) {
        Label field0 = new Label(MonopolyConstants.FIELD_NAMES[i]);
        gridPane.add(field0, i + 1, 0);
    }
}

}

这是FXML。

    <GridPane fx:id="grindPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="900.0" style="-fx-background-color: #E04134; -fx-background-repeat: stretch; -fx-background-position: center center;" xmlns="http://javafx.com/javafx/9" xmlns:fx="http://javafx.com/fxml/1" fx:controller="gui.GUIController">
  <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 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 hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
      <ColumnConstraints />
  </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 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 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>
   <children>
      <Pane prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: white;">
         <children>
            <Label fx:id="field0" layoutX="24.0" layoutY="17.0" text="START" />
         </children></Pane>
      <Pane prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: f1f1f1;" GridPane.columnIndex="1" />
      <Pane prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: white;" GridPane.columnIndex="2" />
      <Pane prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: f1f1f1;" GridPane.columnIndex="3" />
      <Pane prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: white;" GridPane.columnIndex="4" />
      <Pane prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: f1f1f1;" GridPane.columnIndex="5" />
      <Pane prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: white;" GridPane.columnIndex="6" />
      <Pane prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: f1f1f1;" GridPane.columnIndex="7" />
      <Pane prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: white;" GridPane.columnIndex="8" />
      <Pane prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: f1f1f1;" GridPane.columnIndex="9" />
      <Pane prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: white;" GridPane.columnIndex="9" GridPane.rowIndex="1" />
      <Pane prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: f1f1f1;" GridPane.columnIndex="9" GridPane.rowIndex="2" />
      <Pane prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: white;" GridPane.columnIndex="9" GridPane.rowIndex="3" />
      <Pane prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: f1f1f1;" GridPane.columnIndex="9" GridPane.rowIndex="4" />
      <Pane prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: white;" GridPane.columnIndex="9" GridPane.rowIndex="5" />
      <Pane prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: f1f1f1;" GridPane.columnIndex="9" GridPane.rowIndex="6" />
      <Pane prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: white;" GridPane.columnIndex="9" GridPane.rowIndex="7" />
      <Pane prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: f1f1f1;" GridPane.columnIndex="9" GridPane.rowIndex="8" />
      <Pane prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: white;" GridPane.columnIndex="9" GridPane.rowIndex="9" />
      <Pane prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: white;" GridPane.columnIndex="9" GridPane.rowIndex="11" />
      <Pane prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: f1f1f1;" GridPane.columnIndex="8" GridPane.rowIndex="11" />
      <Pane prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: white;" GridPane.columnIndex="7" GridPane.rowIndex="11" />
      <Pane prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: f1f1f1;" GridPane.columnIndex="6" GridPane.rowIndex="11" />
      <Pane prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: white;" GridPane.columnIndex="5" GridPane.rowIndex="11" />
      <Pane prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: f1f1f1;" GridPane.columnIndex="4" GridPane.rowIndex="11" />
      <Pane prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: white;" GridPane.columnIndex="3" GridPane.rowIndex="11" />
      <Pane prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: f1f1f1;" GridPane.columnIndex="2" GridPane.rowIndex="11" />
      <Pane prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: white;" GridPane.columnIndex="1" GridPane.rowIndex="11" />
      <Pane prefHeight="33.0" prefWidth="60.0" style="-fx-background-color: f1f1f1;" GridPane.rowIndex="11" />
      <Pane prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: f1f1f1;" GridPane.rowIndex="9" />
      <Pane prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: white;" GridPane.rowIndex="8" />
      <Pane prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: f1f1f1;" GridPane.rowIndex="7" />
      <Pane prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: white;" GridPane.rowIndex="6" />
      <Pane prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: f1f1f1;" GridPane.rowIndex="5" />
      <Pane prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: white;" GridPane.rowIndex="4" />
      <Pane prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: f1f1f1;" GridPane.rowIndex="3" />
      <Pane prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: white;" GridPane.rowIndex="2" />
      <Pane prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: f1f1f1;" GridPane.rowIndex="1" />
      <Pane prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: f1f1f1;" GridPane.columnIndex="9" GridPane.rowIndex="10" />
      <Pane prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: white;" GridPane.rowIndex="10" />
      <ImageView fitHeight="161.0" fitWidth="400.0" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="1" GridPane.columnSpan="8" GridPane.halignment="CENTER" GridPane.rowIndex="1" GridPane.rowSpan="10" GridPane.valignment="CENTER">
         <image>
            <Image url="@../MonopolyLogo.png" />
         </image>
      </ImageView>
   </children>
</GridPane>

在这里,您可以在“场景”构建器中查看它的外观。 Monopoly game

1 个答案:

答案 0 :(得分:0)

好的,首先如果您在控制器中使用@FXML,则不需要实例化小部件。只需通过public使用它们,或使用fx:id访问小部件。如果使用适当的GridPane gridPane = new GridPane();设置这些小部件,FXMLLoader将进行实例化。因此,stylesheets="@../style.css"不再是必需的。

作为第二个提示,以样式表的形式使用JavaFX样式概率,并避免使用内联样式膨胀FXML文件。我更喜欢为您的布局的根容器提供样式表引用,如styleClass。之后,您可以为所需的小部件提供适当的ResourceBundle属性。

针对您的问题:有几种可能性来解决这个问题。

  • 首先,您可以在FXML内部的GridPane的每个单元格中添加一个Label。 Imho您不需要为每个单元格设置单独的窗格,尤其是因为您可以使用所需的背景颜色,宽度和高度来定义标签。这样做的缺点是,你不能直接利用你的常数。相反,您可以使用<GridPane ...> ... <children> <Label styleClass="label-even" text="%00"/> <Label styleClass="label-odd" text="%01" GridPane.columnIndex="1"/> <Label styleClass="label-even" text="%10" GridPane.rowIndex="1"/> </children> </GridPane> 直接在FXML中访问其字符串。

要从ResourceBundle添加标签所需的样式和文本,您可以这样做:

strings_en.properties

要使捆绑文件(理想情况下名为final FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("sample.fxml"), ResourceBundle.getBundle("strings", Locale.getDefault())); )正常工作,您需要使用FXMLLoader来告诉他可以找到它的位置。这是在加载FXML文件时完成的,例如:

00=START
01=Label of first row and second column
...
10=Label of second column and first row
...

捆绑包可能需要这样的字符串(适合我的例子):

MonopolyConstants.FIELD_NAMES
  • 其次,您可以按照您已经尝试过的方式添加这些内容。在这里,您可以使用常量public class GUIController implements Initializable { @FXML private GridPane gridPane; @Override public void initialize(URL url, ResourceBundle rb) { gridPane.add(new Label(MonopolyConstants.FIELD_NAMES[0]), 0, 0); gridPane.add(new Label(MonopolyConstants.FIELD_NAMES[1]), 1, 0); gridPane.add(new Label(MonopolyConstants.FIELD_NAMES[2]), 2, 0); ... gridPane.add(new Label(MonopolyConstants.FIELD_NAMES[39]), 11, 11); } }

以下内容为了您的方便而剪裁:

{{1}}

我确信,有一种巧妙的方式可以更加动态地做到这一点,但是我没有那么多的业余时间;)

相关问题