2017-04-08 103 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解决它,谢谢。 –