2014-10-12 79 views
0

我在FXML文檔中使用id爲matrix的GridPane。我想爲矩陣中的每個單元添加標籤。 這裏是我的控制器:在JavaFX中向GridPane添加標籤時遇到問題

package application.view; 

import java.awt.Button; 
import java.awt.Insets; 
import java.awt.Label; 

import javafx.fxml.FXML; 
import javafx.scene.Node; 
import javafx.scene.layout.GridPane; 

public class viewController { 

    @FXML 
    private GridPane matrix; 
    private Label[][] label = new Label[10][10]; 

    public viewController() { 
    } 

    @FXML 
    private void initialize() { 

    } 

    @FXML 
    private void setMatrix() { 
     for (int i = 0; i < label.length; i++) { 
      for (int j = 0; j < label[i].length; j++) { 
       label[i][j].setText("1"); 
       matrix.add(label[i][j], i, j); 
      } 
     } 
    } 

} 

這裏是我的FXML文件:

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

<?import javafx.geometry.Insets?> 
<?import javafx.scene.effect.*?> 
<?import javafx.scene.control.*?> 
<?import java.lang.*?> 
<?import javafx.scene.layout.*?> 

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="400.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.view.viewController"> 
    <children> 
     <SplitPane dividerPositions="0.8467336683417085" orientation="VERTICAL" prefHeight="200.0" prefWidth="160.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"> 
     <items> 
      <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0"> 
       <children> 
        <GridPane fx:id="matrix" alignment="CENTER" disable="true" gridLinesVisible="true" layoutX="73.0" layoutY="125.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"> 
        <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> 
        <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> 
        </GridPane> 
       </children> 
      </AnchorPane> 
      <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0"> 
       <children> 
        <HBox alignment="CENTER" prefHeight="65.0" prefWidth="200.0" spacing="10.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"> 
        <children> 
         <Button fx:id="refresh" mnemonicParsing="false" text="Refresh" /> 
         <Button fx:id="findBlock" mnemonicParsing="false" text="Find Largest Block" /> 
        </children> 
        </HBox> 
       </children> 
      </AnchorPane> 
     </items> 
     </SplitPane> 
    </children> 
</AnchorPane> 

正如你可以看到我拿到ID矩陣的GridPane。在那裏我想在每個單元格中添加一個標籤。該標籤顯然會有一個數字。唯一給我錯誤的是matrix.add()。它說不能轉換爲節點。請幫忙。謝謝!

回答

0

嘗試使用javafx.scene.control.Label類。

2

像user4132613說的,導入​​而不是java.awt.Label。實際上,絕對不要在JavaFX中使用AWT類。

而且你已經創建了標籤數組,但是你沒有實例化數組中的對象。方法setMatrix()應該是:

@FXML 
private void setMatrix() { 
    for (int i = 0; i < label.length; i++) { 
     for (int j = 0; j < label[i].length; j++) { 
      label[i][j] = new Label(); // This is missing in the original code 
      label[i][j].setText("1"); 
      matrix.add(label[i][j], i, j); 
     } 
    } 
} 
+0

非常感謝!我一直在試圖弄清楚這兩個小時的情況,而且這一點很容易解決。哇。我想這是學習過程。 – Nazariy1995 2014-10-12 14:43:20

+0

這是一個不幸的決定,AWT和JavaFX共享許多控件名稱,就像Label的情況一樣。但是,當代碼甚至沒有編譯時,檢查導入的類型是一種很好的做法。更好的辦法是在IDE中設置排除自動導入的完整路徑(如java.awt。* path here)。 Eclipse和IntelliJ IDEA都支持該選項。 – user645859 2014-10-12 15:04:17