FXML 문서의 컨트롤러 클래스를 만들어 UI 구성 요소와 관련하여 수행해야하는 모든 기능을 수행 할 수 있습니다. 해당 클래스의 필드에 @FXML
으로 주석을 달 수 있으며 FXMLLoader
으로 채워지고 fx:id
특성이 필드 이름과 일치합니다.
자세한 내용은 tutorial을 통해 작업하고 Introduction to FXML documentation을 살펴보십시오.
간단한 예 :
Sample.fxml :
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Button?>
<VBox xmlns:fx="http://javafx.com/fxml/1" fx:controller="SampleController">
<Label fx:id="countLabel"/>
<Button fx:id="incrementButton" text="Increment" onAction="#increment"/>
</VBox>
SampleController.java :
import javafx.fxml.FXML;
import javafx.scene.control.Label;
public class SampleController {
private int count = 0 ;
@FXML
private Label countLabel ;
@FXML
private void increment() {
count++;
countLabel.setText("Count: "+count);
}
}
SampleMain.java : 흥미로운
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class SampleMain extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
Scene scene = new Scene(FXMLLoader.load(getClass().getResource("Sample.fxml")), 250, 75);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
고마워! 그것은 훌륭한 해결책입니다. 문제가 해결되었습니다! – JavaPanic