【问题标题】:Put labels on panes in gridpane in javafx在javafx的gridpane中的窗格上放置标签
【发布时间】:2018-04-21 16:50:50
【问题描述】:

我想以编程方式将标签放在我的窗格中。我有一个 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

【问题讨论】:

  • 您的 for 循环似乎不正确,不是吗? ;)
  • 是的。现在它只启动第一个字段。但我想有可能在不手动为每个字段设置标签的情况下制作标签?
  • 当然。如果您将 FXML 添加到问题中,我会对其进行调整。顺便说一句:是否有必要从 FIELD_NAMES 常量中获取标签的字符串?
  • 刚刚添加。 :-) 关于 field_names 这是因为这是我在逻辑中使用的常量。
  • 好的,所以只是为了确保我理解正确:您不想通过 Java 代码实例化标签,但可以通过 FXML 添加它们(就像您已经为您的 START 标签所做的那样)?

标签: java javafx gridpane


【解决方案1】:

好的,首先,如果您在控制器中使用initialize,则不需要实例化小部件。只需通过@FXML 使用它们或使用public 使小部件可访问。如果这些小部件设置了正确的fx:id,FXMLLoader 将进行实例化。因此,这个GridPane gridPane = new GridPane(); 将不再需要。

作为第二个技巧,以样式表的形式利用 JavaFX 样式概率,并避免使用内联样式使 FXML 文件变得臃肿。我更喜欢给你的布局的根容器一个样式表引用,比如stylesheets="@../style.css"。之后,您可以为所需的小部件提供适当的styleClass 属性。

对于您的问题:有多种解决方法。

  • 首先,您可以在 FXML 内的 GridPane 的每个单元格中添加一个标签。 恕我直言,您不需要为每个单元格设置单独的窗格,尤其是因为您可以使用所需的背景颜色、宽度和高度定义标签。这样做的一个缺点是,你不能直接使用你的常量。相反,您可以使用 ResourceBundle 直接在 FXML 中访问其字符串。

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

<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>

要使捆绑文件(最好命名为strings_en.properties)工作,您需要您的 FXMLLoader 告诉他在哪里可以找到它。这是在加载 FXML 文件时完成的,例如:

final FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("sample.fxml"),
            ResourceBundle.getBundle("strings", Locale.getDefault()));

捆绑包可能包含这样的字符串(适合我的示例):

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);
    }
}

我敢肯定,有一种更动态的巧妙方法可以做到这一点,但 atm 我没有这么多空闲时间 ;)

【讨论】:

    猜你喜欢
    • 2016-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-08
    • 1970-01-01
    • 1970-01-01
    • 2016-01-27
    • 1970-01-01
    相关资源
    最近更新 更多