17.5 java FX 이벤트 처리

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package sec05.exam01_event_handler;
 
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
 
public class AppMain extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {
        HBox root = new HBox();
        root.setPrefSize(20050);
        root.setAlignment(Pos.CENTER); // 수평 중앙 연결
        root.setSpacing(20);    
        
        Button btn1 = new Button("버튼1"); // 제네럴을 이용
        btn1.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                System.out.println("버튼1 클릭");
            }
        });
        
        Button btn2 = new Button("버튼2"); // 람다식을 이용
        btn2.setOnAction(event->System.out.println("버튼2 클릭"));
        
        root.getChildren().addAll(btn1, btn2); // HBox에 btn1과 btn2를 한번에 추가
        Scene scene = new Scene(root);
        
        primaryStage.setTitle("AppMain");
        primaryStage.setScene(scene);
        primaryStage.setOnCloseRequest(event->System.out.println("종료 클릭")); // 람다식을 이용
        primaryStage.show();
    }
    
    public static void main(String[] args) {
        launch(args);
    }
}
 
cs


17.5.2 FXML 컨트롤러(Controller)



AppMain.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package sec05.exam02_fxml_controller;
 
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
 
public class AppMain extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {
        Parent root = (Parent)FXMLLoader.load(getClass().getResource("root.fxml"));
        Scene scene = new Scene(root);
        
        primaryStage.setTitle("AppMain");
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    
    public static void main(String[] args) {
        launch(args);
    }
}
cs


root.fxml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?xml version="1.0" encoding="UTF-8"?>
 
<?import javafx.scene.layout.*?>
<?import javafx.scene.control.*?>
 
<HBox xmlns:fx="http://javafx.com/fxml" 
        fx:controller="sec05.exam02_fxml_controller.RootController"
        prefHeight="50.0" prefWidth="200.0"   
        alignment="CENTER" spacing="20.0" >
   <children>
      <Button fx:id="btn1" text="버튼1" />
      <Button fx:id="btn2" text="버튼2" />
      <Button fx:id="btn3" text="버튼3" /> // 직접 <Button text="버튼3" onAction="#handleBtn3Action"> (* 이벤트 처리 메소드 매핑)
   </children>
</HBox>
 
cs
RootController.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package sec05.exam02_fxml_controller;
 
import java.net.URL;
import java.util.ResourceBundle;
 
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
 
public class RootController implements Initializable {
    @FXML private Button btn1; // btn 객체 주입
    @FXML private Button btn2;
    @FXML private Button btn3; // 직접 fxml을 쓰면 이거 쓸 필요없음
 
    @Override
    public void initialize(URL location, ResourceBundle resources) {
        btn1.setOnAction(new EventHandler<ActionEvent>() { // 직접 EventHandler 생성 후 등록
            @Override
            public void handle(ActionEvent event) {
                handleBtn1Action(event);
            }
        });
        btn2.setOnAction(event->handleBtn2Action(event)); // 람다식 이용
        btn3.setOnAction(event->handleBtn3Action(event));
    }
    
    public void handleBtn1Action(ActionEvent event) { 
        System.out.println("버튼1 클릭"); 
    }
    public void handleBtn2Action(ActionEvent event) { 
        System.out.println("버튼2 클릭"); 
    }
    public void handleBtn3Action(ActionEvent event) { 
        System.out.println("버튼3 클릭"); 
    }
}
 
cs


'JAVA > CONCEPT' 카테고리의 다른 글

JAVA chapter17. javaFX. 17.7 JavaFX 컨트롤(1)  (0) 2018.01.13
JAVA chapter17. javaFX. 17.6 java FX 속성 감시와 바인딩  (0) 2018.01.13
공백  (0) 2017.12.30
공백  (0) 2017.12.30
JAVA chapter17. javaFX. 17.4 JavaFX 컨테이너  (0) 2017.12.03
Posted by 너래쟁이
: