2017-04-08 102 views
0

我想從昨天javaFX和場景生成器在一個非常簡單的應用程序中獲取按鈕單擊工作,但我試過的一切(通過以下一些教程或相關答案)它不起作用。 我創建了一個新的JavaFX項目,這些都是我的編輯位創建的文件:javaFX場景生成器按鈕事件不起作用

Main.java:

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

public class Main extends Application { 

    @Override 
    public void start(Stage primaryStage) { 
     try { 
      FXMLLoader loader = new 
     FXMLLoader(getClass().getResource("sample.fxml")); 
     loader.setController(new SampleController()); 
     Pane root = loader.load(); 
      Scene scene = new Scene(root,400,400); 
      scene.getStylesheets().add(getClass().getResource 
      ("application.css").toExternalForm()); 
      primaryStage.setScene(scene); 
      primaryStage.show(); 
     } catch(Exception e) { 
      e.printStackTrace(); 
     } 
    } 

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

SampleControler.java:

package application; 

import java.net.URL; 
import java.util.ResourceBundle; 
import javafx.event.ActionEvent; 
import javafx.fxml.FXML; 
import javafx.fxml.Initializable; 
import javafx.scene.control.Button; 
import javafx.scene.control.Label; 

public class SampleController implements Initializable{ 

    @FXML 
    Button bOk; 
    @FXML 
    Label lbTest; 

    private void handleButtonAction(ActionEvent event) { 
     // Button was clicked, do something... 
     lbTest.setText("Button Action\n"); 
    } 

    @Override 
    public void initialize(URL location, ResourceBundle resources) { 
     // TODO Auto-generated method stub 
     bOk.setOnAction(this::handleButtonAction); 
    } 

} 

樣品。 fxml:

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

<?import javafx.scene.control.Button?> 
<?import javafx.scene.control.Label?> 
<?import javafx.scene.layout.Pane?> 


<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" 
    minWidth="-Infinity" prefHeight="220.0" prefWidth="484.0" 
    xmlns="http://javafx.com/javafx/8.0.111" 
    xmlns:fx="http://javafx.com/fxml/1"> 
    <children> 
     <Button id="bOk" layoutX="214.0" layoutY="152.0" 
    mnemonicParsing="false" text="Button" /> 
     <Label id="lbTest" layoutX="214.0" layoutY="57.0" prefHeight="25.0" 
    prefWidth="138.0" text="Label" /> 
    </children> 
</Pane> 

上面沒有給出任何錯誤,但是當點擊按鈕時它不會做任何事情(應該改變Label的文本)。 我試圖在fxml文件上設置onAction,但這總是給出錯誤(無法解析onAction),無論我放在那裏,或者如果我從場景構建器或手動編輯fxml文件(我已創建該方法並編寫它在onAction中正確)。 我做錯了什麼?

現在我得到的錯誤:

javafx.fxml.LoadException: 
/home/workspace/testapp/bin/application/sample.fxml 

    at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601) 
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579) 
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441) 
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409) 
    at application.Main.start(Main.java:16) 
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863) 
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326) 
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294) 
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95) 
    at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method) 
    at com.sun.glass.ui.gtk.GtkApplication.lambda$null$49(GtkApplication.java:139) 
    at java.lang.Thread.run(Thread.java:745) 
Caused by: java.lang.NullPointerException 
    at application.SampleController.initialize(SampleController.java:27) 
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548) 

我使用FX後:身份證代替編號爲按鈕和標籤按鈕都按預期和上面的錯誤了。

回答

1

你忘了與FXML使用控制器:

<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" 
    minWidth="-Infinity" prefHeight="220.0" prefWidth="484.0" 
    xmlns="http://javafx.com/javafx/8.0.111" 
    xmlns:fx="http://javafx.com/fxml/1" 
    fx:controller="application.SampleController"> 

FXMLLoader loader = new FXMLLoader(getClass().getResource("sample.fxml")); 
loader.setController(new SampleController()); 
Pane root = loader.load(); 

而且你需要使用fx:id而不是id屬性注入對象到控制器:

<Button fx:id="bOk" layoutX="214.0" layoutY="152.0" 
     mnemonicParsing="false" text="Button" /> 
<Label fx:id="lbTest" layoutX="214.0" layoutY="57.0" prefHeight="25.0" 
     prefWidth="138.0" text="Label" /> 
+0

我做了第二個,但我現在得到一個錯誤(我將編輯我的答案) –

+0

fx:id解決它,謝謝。 –