Scene Builder 예제1
Scene Builder를 이용해서 화면 구현
ListView 내용은 코드로 작성
package ex1;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.ListView;
import javafx.stage.Stage;
public class Ex3 extends Application{
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
FXMLLoader loader = new FXMLLoader(getClass().getResource("ex3.fxml"));
Parent form = loader.load();
ListView<String> lv = (ListView<String>)form.lookup("#listView"); // 식별자#: listView id를 가진 것을 찾아서 인스턴스의 참조값 반환
lv.getItems().addAll("아이템1", "아이템2", "아이템3");
Scene scene = new Scene(form);
primaryStage.setScene(scene);
primaryStage.setTitle("ex3");
primaryStage.show();
}
}
Scene Builder 예제2
.
package ex1;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Ex4 extends Application{
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
FXMLLoader loader = new FXMLLoader(getClass().getResource("ex4.fxml"));
Parent form = loader.load();
primaryStage.setScene(new Scene(form));
primaryStage.setTitle("ex4");
primaryStage.show();
}
}
Scene Builder 예제3
- 이벤트 구현
package ex2;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.control.Button;
import javafx.stage.Stage;
public class Ex1 extends Application{
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
FXMLLoader loader = new FXMLLoader(getClass().getResource("ex1.fxml"));
Parent form = loader.load();
TextField id = (TextField)form.lookup("#id");
PasswordField pw = (PasswordField)form.lookup("#pw");
Button loginBtn = (Button)form.lookup("#loginBtn");
// 로그인 버튼 클릭했을 때 이벤트
loginBtn.setOnMouseClicked((e) -> {
String userId = id.getText();
String userPw = pw.getText();
System.out.println("userId: " + userId);
System.out.println("userPw: " + userPw);
});
primaryStage.setScene(new Scene(form));
primaryStage.setTitle("ex1");
primaryStage.show();
}
}
Controller 사용해서 구현하기
1) Scene Builder 에서 컨트롤러 설정
2) 메인
package ex2;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.control.Button;
import javafx.stage.Stage;
public class Ex2 extends Application{
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
FXMLLoader loader = new FXMLLoader(getClass().getResource("ex2.fxml"));
Parent form = loader.load();
Ex2Controller con = loader.getController();
primaryStage.setScene(new Scene(form));
primaryStage.setTitle("ex2");
primaryStage.show();
}
}
3) 컨트롤러
package ex2;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
public class Ex2Controller implements Initializable{
// @FXML 어노테이션은 lookup 기능을 해줌
@FXML TextField id;
@FXML PasswordField pw;
@FXML Button loginBtn;
@Override
public void initialize(URL location, ResourceBundle resources) {
// TextField id = (TextField)form.lookup("#id");
// PasswordField pw = (PasswordField)form.lookup("#pw");
// Button loginBtn = (Button)form.lookup("#loginBtn");
//
// 로그인 버튼 클릭했을 때 이벤트
loginBtn.setOnMouseClicked((e) -> {
String userId = id.getText();
String userPw = pw.getText();
System.out.println("userId: " + userId);
System.out.println("userPw: " + userPw);
});
}
}
'오늘의 공부 & 기록' 카테고리의 다른 글
맥 os 자바(JDK) 버전 변경 (0) | 2023.03.25 |
---|---|
3월 24일 javaFx (0) | 2023.03.24 |
3월 22일 javaFx (0) | 2023.03.22 |
3월 21일 javafx (0) | 2023.03.21 |
3월 20일 javajx (0) | 2023.03.20 |