자바 FX 문서 (12) FXML 파악 - 4 FXML 을 사용 하여 사용자 정의 컨트롤 을 만 듭 니 다.
4177 단어 JavaFX
원본 주소:http://docs.oracle.com/javase/8/javafx/fxml-tutorial/custom_control.htm
이 튜 토리 얼 에 서 는 텍스트 상자 와 단추 로 구 성 된 사용자 정의 컨트롤 을 만 들 것 입 니 다.다음 그림:
시작 하기 전에 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类(由
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);
}
}
실행 인 터 페 이 스 는 다음 과 같 습 니 다:
컨트롤 생 성 이 완료 되면 코드 나 FXML 에서 표준 컨트롤 을 사용 하 는 것 처럼 사용 할 수 있 습 니 다. 예 는 다음 과 같 습 니 다.
코드 샘플:
HBox hbox = new HBox();
CustomControl customControl = new CustomControl();
customControl.setText("Hello World!");
hbox.getChildren().add(customControl);
FXML 샘플:
4. 567913. custom controlExample. zip 를 다운로드 하여 완전한 소스 코드 를 볼 수 있 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Eclipse에서 javaFX 도입 (2020-4 월)javaSE에 javaFX가 동봉되지 않게 되어, 기존의 방법으로는 잘 되지 않는 것이 있는 것 같고, 도입에 망설였으므로 나중의 참고가 된다고 생각. 초보자이므로 손 부드럽게. macOS Catalina Eclip...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.