자바 FX 문서 (12) FXML 파악 - 4 FXML 을 사용 하여 사용자 정의 컨트롤 을 만 듭 니 다.

4177 단어 JavaFX
성명: 전재 가 필요 하 시 면 출처 를 밝 혀 주 십시오.http://blog.csdn.net/originer
원본 주소:http://docs.oracle.com/javase/8/javafx/fxml-tutorial/custom_control.htm
이 튜 토리 얼 에 서 는 텍스트 상자 와 단추 로 구 성 된 사용자 정의 컨트롤 을 만 들 것 입 니 다.다음 그림:
JavaFX文档(12)掌握FXML——4使用FXML来创建自定义控件_第1张图片
시작 하기 전에 IDE 가 자바 FX 8 을 지원 하 는 지 확인 해 야 합 니 다.또한 FXML 프로젝트 의 기본 구조 (자바, fxml, Controller 파일) 에 대해 알 아야 합 니 다.
프로젝트 생 성
IDE 에서 CustomControl Example 프로젝트 를 만 들 고 CustomControl Example. java, CustomControl. java, custom 을 만 듭 니 다.control. fxml 파일.
기본 사용자 인터페이스 만 들 기
TextField 와 Button 인 스 턴 스 를 포함 한 간단 한 사용자 정의 컨트롤 을 정의 합 니 다.루트 용 기 는 javafx. scene. layot. VBox 클래스 입 니 다.
1. custom 편집control. fxml 파일 2. 코드 를 다음 과 같이 변경 합 니 다.




 
    
    

创建一个控制器

在本例中CustomControl类继承了VBox类(由元素所定义的类型),并且在其构造方法中设置其自身既是root、也是FXML的Controller。当文档被加载时,CustomControl实例将会由文档内容所填充而成。

将CustomControl类的代码改成如下所示:
package customcontrolexample;

import java.io.IOException;

import javafx.beans.property.StringProperty;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;

public class CustomControl extends VBox {
    @FXML private TextField textField;

    public CustomControl() {
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource(
"custom_control.fxml"));
        fxmlLoader.setRoot(this);
        fxmlLoader.setController(this);

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

    public String getText() {
        return textProperty().get();
    }

    public void setText(String value) {
        textProperty().set(value);
    }

    public StringProperty textProperty() {
        return textField.textProperty();
    }

    @FXML
    protected void doSomething() {
        System.out.println("The button was clicked!");
    }
}

FXML 원본 파일 불 러 오기, Stage 와 Scene 정의
Custom Control Example. java 파일 은 main 입 구 를 포함 하고 stage 와 scene 을 정의 하 며 FXML 원본 파일 을 불 러 옵 니 다.이러한 종류의 사용자 정의 컨트롤 클래스 를 통 해 FXML 원본 파일 을 불 러 옵 니 다.
Custom Control Example 클래스 코드 를 다음 으로 변경 합 니 다.
package customcontrolexample;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class CustomControlExample extends Application{

	@Override
	public void start(Stage stage) throws Exception {
		CustomControl customControl = new CustomControl();
		customControl.setText("Hello!");
		stage.setScene(new Scene(customControl));
		stage.setTitle("Custom Control");
		stage.setWidth(300);
		stage.setHeight(200);
		stage.show();
	}
	public static void main(String[] args) {
		launch(args);
	}

}

실행 인 터 페 이 스 는 다음 과 같 습 니 다:
JavaFX文档(12)掌握FXML——4使用FXML来创建自定义控件_第2张图片
컨트롤 생 성 이 완료 되면 코드 나 FXML 에서 표준 컨트롤 을 사용 하 는 것 처럼 사용 할 수 있 습 니 다. 예 는 다음 과 같 습 니 다.
코드 샘플:
HBox hbox = new HBox();
CustomControl customControl = new CustomControl();
customControl.setText("Hello World!");
hbox.getChildren().add(customControl);

FXML 샘플:
4. 567913. custom controlExample. zip 를 다운로드 하여 완전한 소스 코드 를 볼 수 있 습 니 다.

좋은 웹페이지 즐겨찾기