2015-03-31 51 views
1

我在JavaFX中製作風險版本。現在顯示遊戲數據,我正在使用顯示每個領土信息的標籤網格。現在我想在標籤上點擊鼠標聽衆,這樣我就可以通過點擊它們來測試遊戲功能,直到我製作出完整的遊戲板。除了當我嘗試在FXML文檔中添加偵聽器時,我收到錯誤。現在我在控制器中聲明標籤,將它們添加到集合中,以便我可以使用循環來綁定每個標籤的textProperty,然後將每個標籤添加到網格中。然後在FXML中我爲每個定義了鼠標點擊。 這裏是控制器代碼:使用FXML向JavaFX中的標籤添加鼠標單擊操作事件

@FXML 
private GridPane tGrid = new GridPane(); 

@FXML Label label1 = new Label(); 
@FXML Label label2 = new Label(); 
@FXML Label label3 = new Label(); 
@FXML Label label4 = new Label(); 
@FXML Label label5 = new Label(); 
@FXML Label label6 = new Label(); 
@FXML Label label7 = new Label(); 
@FXML Label label8 = new Label(); 
@FXML Label label9 = new Label(); 
@FXML Label label10 = new Label(); 
@FXML Label label11 = new Label(); 
@FXML Label label12 = new Label(); 
@FXML Label label13 = new Label(); 
@FXML Label label14 = new Label(); 
@FXML Label label15 = new Label(); 
@FXML Label label16 = new Label(); 
@FXML Label label17 = new Label(); 
@FXML Label label18 = new Label(); 
@FXML Label label19 = new Label(); 
@FXML Label label20 = new Label(); 
@FXML Label label21 = new Label(); 
@FXML Label label22 = new Label(); 
@FXML Label label23 = new Label(); 
@FXML Label label24 = new Label(); 
@FXML Label label25 = new Label(); 
@FXML Label label26 = new Label(); 
@FXML Label label27 = new Label(); 
@FXML Label label28 = new Label(); 
@FXML Label label29 = new Label(); 
@FXML Label label30 = new Label(); 
@FXML Label label31 = new Label(); 
@FXML Label label32 = new Label(); 
@FXML Label label33 = new Label(); 
@FXML Label label34 = new Label(); 
@FXML Label label35 = new Label(); 
@FXML Label label36 = new Label(); 
@FXML Label label37 = new Label(); 
@FXML Label label38 = new Label(); 
@FXML Label label39 = new Label(); 
@FXML Label label40 = new Label(); 
@FXML Label label41 = new Label(); 
@FXML Label label42 = new Label(); 


@Override 
public void initialize(URL url, ResourceBundle rb) { 
    anchor.setLeftAnchor(tGrid, 10.0); 
    anchor.setRightAnchor(pGrid, 10.0); 
    tGrid.setVisible(false); 
    pGrid.setVisible(false); 
    labels.add(label1); 
    labels.add(label2); 
    labels.add(label3); 
    labels.add(label4); 
    labels.add(label5); 
    labels.add(label6); 
    labels.add(label7); 
    labels.add(label8); 
    labels.add(label9); 
    labels.add(label10); 
    labels.add(label11); 
    labels.add(label12); 
    labels.add(label13); 
    labels.add(label14); 
    labels.add(label15); 
    labels.add(label16); 
    labels.add(label17); 
    labels.add(label18); 
    labels.add(label19); 
    labels.add(label20); 
    labels.add(label21); 
    labels.add(label22); 
    labels.add(label23); 
    labels.add(label24); 
    labels.add(label25); 
    labels.add(label26); 
    labels.add(label27); 
    labels.add(label28); 
    labels.add(label29); 
    labels.add(label30); 
    labels.add(label31); 
    labels.add(label32); 
    labels.add(label33); 
    labels.add(label34); 
    labels.add(label35); 
    labels.add(label36); 
    labels.add(label37); 
    labels.add(label38); 
    labels.add(label39); 
    labels.add(label40); 
    labels.add(label41); 
    labels.add(label42); 
    int k = 0; 
    for(int i = 0; i < 6; i++) { 
     for(int j = 0; j < game.getContinent(i).getTerritoryNum(); j++) { 
      labels.get(k).textProperty().bind(game.getContinent(i).getTerritory(j).getProperty()); 
      k += 1; 
     }//for 
    }//for 
    for (int i = 0; i < 42; i++) { 
     tGrid.add(labels.get(i), 0, i); 
    }//for 

這裏是FXML代碼(除所有的標籤將有onMouseClicked):

  <Label fx:id="label1"onMouseClicked="#labelAction" /> 
      <Label fx:id="label2"/> 
      <Label fx:id="label3"/> 
      <Label fx:id="label4"/> 
      <Label fx:id="label5"/> 
      <Label fx:id="label6"/> 
      <Label fx:id="label7"/> 
      <Label fx:id="label8"/> 
      <Label fx:id="label9"/> 
      <Label fx:id="label10"/> 
      <Label fx:id="label11"/> 
      <Label fx:id="label12"/> 
      <Label fx:id="label13"/> 
      <Label fx:id="label14"/> 
      <Label fx:id="label15"/> 
      <Label fx:id="label16"/> 
      <Label fx:id="label17"/> 
      <Label fx:id="label18"/> 
      <Label fx:id="label19"/> 
      <Label fx:id="label20"/> 
      <Label fx:id="label21"/> 
      <Label fx:id="label22"/> 
      <Label fx:id="label23"/> 
      <Label fx:id="label24"/> 
      <Label fx:id="label25"/> 
      <Label fx:id="label26"/> 
      <Label fx:id="label27"/> 
      <Label fx:id="label28"/> 
      <Label fx:id="label29"/> 
      <Label fx:id="label30"/> 
      <Label fx:id="label31"/> 
      <Label fx:id="label32"/> 
      <Label fx:id="label33"/> 
      <Label fx:id="label34"/> 
      <Label fx:id="label35"/> 
      <Label fx:id="label36"/> 
      <Label fx:id="label37"/> 
      <Label fx:id="label38"/> 
      <Label fx:id="label39"/> 
      <Label fx:id="label40"/> 
      <Label fx:id="label41"/> 
      <Label fx:id="label42"/> 

這裏是錯誤:

Exception in Application start method 
java.lang.reflect.InvocationTargetException 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:497) 
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389) 
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:497) 
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767) 
Caused by: java.lang.RuntimeException: Exception in Application start method 
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917) 
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$152(LauncherImpl.java:182) 
at com.sun.javafx.application.LauncherImpl$$Lambda$50/1343441044.run(Unknown Source) 
at java.lang.Thread.run(Thread.java:745) 
Caused by: javafx.fxml.LoadException: 
file:/C:/Users/Brent/Documents/NetBeansProjects/RiskFXML/dist/run1282679996/RiskFXML.jar!/riskfxml/FXML.fxml:34 

at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2605) 
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2547) 
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2445) 
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3218) 
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3179) 
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3152) 
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3128) 
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3108) 
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3101) 
at riskfxml.RiskFXML.start(RiskFXML.java:22) 
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$159(LauncherImpl.java:863) 
at com.sun.javafx.application.LauncherImpl$$Lambda$53/1527242123.run(Unknown Source) 
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$172(PlatformImpl.java:326) 
at com.sun.javafx.application.PlatformImpl$$Lambda$45/355629945.run(Unknown Source) 
at com.sun.javafx.application.PlatformImpl.lambda$null$170(PlatformImpl.java:295) 
at com.sun.javafx.application.PlatformImpl$$Lambda$48/1753953479.run(Unknown Source) 
at java.security.AccessController.doPrivileged(Native Method) 
at com.sun.javafx.application.PlatformImpl.lambda$runLater$171(PlatformImpl.java:294) 
at com.sun.javafx.application.PlatformImpl$$Lambda$47/1915503092.run(Unknown Source) 
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95) 
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) 
at com.sun.glass.ui.win.WinApplication.lambda$null$145(WinApplication.java:101) 
at com.sun.glass.ui.win.WinApplication$$Lambda$36/1963387170.run(Unknown Source) 
... 1 more 
Caused by: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[34,38] 
Message: Element type "Label" must be followed by either attribute specifications, ">" or "/>". 
at com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.next(XMLStreamReaderImpl.java:601) 
at javax.xml.stream.util.StreamReaderDelegate.next(StreamReaderDelegate.java:88) 
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2517) 
... 22 more 
Exception running application riskfxml.RiskFXML 
Java Result: 1 

更新

好吧,我刪除了所有'= new Label()',它擺脫了錯誤,導致問題是通過在fxml和控制器中定義它們來嘗試添加重複元素。現在我的問題是,如何將標籤添加到fxml中的gridpane中,位置與我在示例控制器代碼底部的循環中所做的位置相同?是一種將它們添加到fxml中的數組列表並添加它的方法?我知道我可以做

<Label fx:id="label1" onMouseClicked="#labelAction" GridPane.columnIndex="0" GridPane.rowIndex="0" /> 

爲每個標籤,但必須有一個更好的方式比手型和放置每一個標籤的權利?他們只需要在增加行的同一列中。

+0

你能識別FXML文件中的相關行(第34行)嗎?這不是錯誤的原因,但是你不應該初始化'@ FXML'注入的字段(即刪除語句的所有'= new Label()'部分,''new GridPane()'。此外,在Java代碼中不會有更好的效果,使用循環而不是FXML?) – 2015-03-31 22:22:34

+2

應該在'

+0

您是否聽說過[Scene Builder](https://docs.oracle.com/javafx/scenebuilder/1/get_started/prepare-for-tutorial.htm)?嘗試使用它來操縱您的fxml。它是工具您可以拖放組件,併爲您創建FXML代碼。 – ItachiUchiha 2015-04-07 01:54:43

回答

1

我正面臨着類似的問題,你和解決它以不同的方式: 在標籤的上面創建一個按鈕,並指定不透明度爲0

這樣的按鈕顯示不出來的最終用戶,但是當他們點擊標籤時,底層按鈕會顯示該操作。既然它是一個標準的javafx按鈕,你可以使用onAction =「#yourAction」,你甚至不需要改變標籤中的任何東西。

希望這有助於!