JavaFX-无法获得自定义按钮来打印hello world

时间:2014-12-20 23:20:33

标签: javafx javafx-2 javafx-8 scenebuilder

我一直试图在单独的FXML文件中获取其中一个按钮,以打印" Hello"它被导入我的主场景后的世界。我正在使用scenebuilder并尝试将媒体按钮代码与应用程序的其余部分分开。我已经提到了很多教程,但是看不出我出错的地方。以下是相关文件(我遗漏了一些代码以使其更具可读性):

从我的主场景AudioPlayer.fxml(mediaBoxAnchor是包含我的无功能按钮的组件):

<HBox layoutX="751.0" layoutY="20.0" prefHeight="19.0" prefWidth="159.0" spacing="5.0" styleClass="head" AnchorPane.rightAnchor="20.0" AnchorPane.topAnchor="20.0">
         <children>
            <TextField layoutX="751.0" layoutY="14.0" />
         </children>
      </HBox>
      <Slider layoutX="196.0" layoutY="25.0" prefHeight="16.0" prefWidth="828.0" AnchorPane.rightAnchor="195.0" />
      <AnchorPane fx:id="mediaBoxAnchor" layoutX="14.0" layoutY="20.0">
         <children>
            <HBox fx:id="mediaControlHBox">
               <children>
                  <Button fx:id="previousButton" mnemonicParsing="false" text="Prev" />
                  <Button fx:id="playButton" mnemonicParsing="false" text="Play" />
                  <Button fx:id="nextButton" mnemonicParsing="false" text="Next" />
               </children>
            </HBox>
         </children>
      </AnchorPane>

MediaPlaybackHBoxWidget:

public class MediaPlaybackHBoxWidget extends AnchorPane {

@FXML
private Button previousButton, playButton, nextButton;

public MediaPlaybackHBoxWidget() {
    FXMLLoader fxmlLoader = new FXMLLoader(
            getClass().getResource("/fxml/MediaPlaybackHBox.fxml"));

    fxmlLoader.setRoot(this);
    fxmlLoader.setController(this);

    try {
        fxmlLoader.load();
    } catch (IOException exception) {
        throw new RuntimeException(exception);
    }

   }
}

MediaPlaybackHBox.fxml:

<fx:root fx:id="mediaBoxAnchor" type="AnchorPane" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.mycompany.mavenproject1.MediaPlaybackHBoxController">
   <children>
      <HBox fx:id="mediaControlHBox">
         <children>
            <Button fx:id="previousButton" mnemonicParsing="false" text="Prev" />
            <Button fx:id="playButton" mnemonicParsing="false" text="Play" />
            <Button fx:id="nextButton" mnemonicParsing="false" text="Next" />
         </children>
      </HBox>
   </children>
</fx:root>

MediaPlaybackHBoxController:

public class MediaPlaybackHBoxController implements Initializable {

@FXML
private Button previousButton, playButton, nextButton;

@FXML
MediaPlaybackHBoxWidget mpw;

@Override
public void initialize(URL location, ResourceBundle resources) {

    playButton.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent event) {
            System.out.println("Hello World!");
        }
    });

  }
}

关于我失踪的任何线索?

1 个答案:

答案 0 :(得分:0)

我很确定我是以错误的方式解决这个问题。 Oracle Introduction to FXML的控制器部分似乎很有帮助。