IT 개발노트

javafx 본문

기초튼튼/JAVA

javafx

limsungju 2019. 3. 5. 10:11

1. AnchorPane

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
package Java.sungju.JavaProject3;
 
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
 
public class AnchorPane01 extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {
        AnchorPane anchorPane = new AnchorPane();
        Button button1 = new Button("Add111");
        Button button2 = new Button("Add222");
        AnchorPane.setTopAnchor(button1, 10.0);
        AnchorPane.setRightAnchor(button1, 10.0);
        AnchorPane.setBottomAnchor(button2, 10.0);
        AnchorPane.setLeftAnchor(button2, 10.0);
        anchorPane.getChildren().addAll(button1, button2);
        primaryStage.setTitle("AnchoPaneEx");
        primaryStage.setScene(new Scene(anchorPane,300,250));
        primaryStage.show();
    }
 
    public static void main(String[] args) {
        launch(args);
    }
}
cs

2. BorderPane

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
package Java.sungju.JavaProject3;
 
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
 
public class BorderPane01 extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {
        BorderPane borderPane = new BorderPane();
        Button[] btn = new Button[5];
        String[] strBtn = { "Top""Center""Bottom""Left""Right" };
        for (int i = 0; i < btn.length; i++) {
            btn[i] = new Button(strBtn[i]);
        }
        borderPane.setTop(btn[0]);
        borderPane.setCenter(btn[1]);
        borderPane.setBottom(btn[2]);
        borderPane.setLeft(btn[3]);
        borderPane.setRight(btn[4]);
        primaryStage.setTitle("BorderPaneEx");
        primaryStage.setScene(new Scene(borderPane, 300100)); // 배경 크기 설정
        primaryStage.show();
    }
 
    public static void main(String[] args) {
        launch(args);
    }
}
cs

3. FlowPane

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 Java.sungju.JavaProject3;
 
import java.util.ArrayList;
 
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Orientation;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;
 
public class FlowPane01 extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {
        // FlowPane flowPane = new FlowPane(); // 가로로 나열하기
        FlowPane flowPane = new FlowPane(Orientation.VERTICAL); // 세로로 나열하기
        ArrayList<Button> lstBtn = new ArrayList<Button>();
        // flowPane.setColumnHalignment(HPos.LEFT); // 버튼 나열방식
 
        // 위, 오른쪽, 아래, 왼쪽 간격 조정
        flowPane.setPadding(new Insets(101010100));
 
        for (int i = 0; i < 5; i++) {
            lstBtn.add(new Button("btn" + (i * i * i * i * 10)));
            flowPane.getChildren().add(lstBtn.get(i));
        }
 
        primaryStage.setTitle("FlowPane");
        Scene sc = new Scene(flowPane, 300250);
        primaryStage.setScene(sc);
        primaryStage.show();
 
    }
 
    public static void main(String[] args) {
        launch(args);
    }
}
cs

4. GridPane

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
package Java.sungju.JavaProject3;
 
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
 
public class GridPane01 extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {
        GridPane gridpane = new GridPane();
        Button button = new Button("Button");
        Label label = new Label("Label");
        Label label1 = new Label("Label11");
        
        // 인덱스를 아무리 증가시켜도 현재 늘어난 값에서 한칸만 증가된다
        GridPane.setRowIndex(button, 0); // 세로로 순번 0부터시작 
        GridPane.setColumnIndex(button, 0); // 가로로 순번 0부터시작
        GridPane.setRowIndex(label1, 1);
        GridPane.setColumnIndex(label1, 1);
        
        // Constraints는 RowIndex와 ColumnIndex를 한번에 지정하는 것
        GridPane.setConstraints(label, 02); // 가로, 세로
        gridpane.getChildren().addAll(button, label, label1);
        primaryStage.setTitle("GridPane");
        primaryStage.setScene(new Scene(gridpane, 300100));
        primaryStage.show();
    }
 
    public static void main(String[] args) {
        launch(args);
    }
}
cs
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
package Java.sungju.JavaProject3;
 
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
 
public class GridPane02 extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {
        GridPane gridpane = new GridPane();
        Button button = new Button("Button");
        Label label = new Label("Label");
        gridpane.setPadding(new Insets(20));//위, 오른쪽, 아래, 왼쪽 위치조정
        gridpane.setHgap(50); // 가로 갭
        gridpane.setVgap(10); // 세로 갭
        gridpane.setRowIndex(button, 0);
        gridpane.setColumnIndex(button, 0);
        gridpane.setConstraints(label, 11);
        gridpane.getChildren().addAll(button, label);
        gridpane.add(new Button("1, 0"), 10);
        gridpane.add(new Label("0, 1"), 01);
        primaryStage.setTitle("GridPaneEx");
        primaryStage.setScene(new Scene(gridpane,300,300));
        primaryStage.show();
    }
 
    public static void main(String[] args) {
        launch(args);
    }
}
cs
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
package Java.sungju.JavaProject3;
 
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
 
public class GridPane03 extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {
        GridPane gridpane = new GridPane();
        gridpane.setPadding(new Insets(20));// 위, 오른쪽, 아래, 왼쪽 위치조정
        gridpane.setHgap(50); // 가로 갭
        gridpane.setVgap(10); // 세로 갭
        gridpane.add(new Button("Button"), 00);
        gridpane.add(new Button("1, 0"), 10);
        gridpane.add(new Label("0, 1"), 01);
        gridpane.add(new Label("Label"), 11);
        FlowPane canvas = new FlowPane();
        canvas.getChildren().add(new Button("merge col(0), row(2)"));
        canvas.setAlignment(Pos.CENTER);
        canvas.setStyle("-fx-background-color: black;"); // rgb 색상표검색해서 찾기
        gridpane.add(canvas, 0221); // 가로, 세로, 병합개수, 세로병합
 
        primaryStage.setTitle("GridPaneEx");
        primaryStage.setScene(new Scene(gridpane, 300300));
        primaryStage.show();
    }
 
    public static void main(String[] args) {
        launch(args);
    }
}
cs

5. HBox

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 Java.sungju.JavaProject3;
 
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Priority;
import javafx.stage.Stage;
 
public class HBox01 extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {
        //VBox vbox = new VBox(10); // 세로 나열
        HBox hbox = new HBox(10); // 가로 나열, 중간 라인의 패딩값 ()안에 설정
        hbox.setSpacing(10); // 간격 조절
        hbox.setPadding(new Insets(10));
        Pane canvas1 = new Pane();
        Pane canvas2 = new Pane();
        canvas1.setStyle("-fx-background-color: blue;");
        canvas1.setPrefSize(200150);
        canvas2.setStyle("-fx-background-color: red;");
        canvas2.setPrefSize(200150);
        
        //HBox.setHgrow(canvas1, Priority.ALWAYS); // 가변적 크기 창을 늘리면 같이 늘어남
        //HBox.setHgrow(canvas2, Priority.ALWAYS);
        HBox.setHgrow(canvas1, Priority.NEVER); // 고정된 크기 창을 늘려도 안늘어남
        HBox.setHgrow(canvas2, Priority.NEVER);
        
        hbox.getChildren().addAll(canvas1, canvas2);
        Scene scene = new Scene(hbox);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
 
    public static void main(String[] args) {
        launch(args);
    }
}
cs

6. ImageView

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
package Java.sungju.JavaProject3;
 
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
 
public class ImageView01 extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {
        //가로로 나열된다.
        HBox hb = new HBox();
        AnchorPane ap = new AnchorPane();
        // 이미지가 다른 경로에 있을경우 패키지 경로까지 적어줘야한다.
        //ImageView iv = new ImageView("/img/smilerere.png");
        ImageView iv1 = new ImageView();
        iv1.setImage(new Image("/img/smilerere.png"));
        ImageView iv2 = new ImageView();
        iv2.setImage(new Image("/img/smilerere.png"));
        iv2.setRotate(45);
        //Rectangle2D viewportRect = new Rectangle2D(40, 35, 110, 110);
        //iv2.setViewport(viewportRect);
        //iv2.setLayoutX(40);
        //iv2.setLayoutY(35);
        ap.getChildren().addAll(iv2);
        hb.getChildren().addAll(iv1, ap);
        hb.setPrefSize(1024768);
        //ap.setPrefSize(300, 200);
        primaryStage.setTitle("View Test");
        primaryStage.setScene(new Scene(hb,400,200));
        primaryStage.show();
    }
 
    public static void main(String[] args) {
        launch(args);
    }
}
cs

7 ListView

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
package Java.sungju.JavaProject3;
 
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ListView;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
 
public class ListView01 extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {
        AnchorPane ap = new AnchorPane();
        ListView<String> listView = new ListView<String>();
        listView.getItems().addAll("item1""item2""item3");
        
        // 사이즈 조정
        listView.setPrefSize(10070);
        
        // 위치 조정
        listView.setLayoutX(50);
        listView.setLayoutY(50);
        
        ap.getChildren().addAll(listView);
        ap.setPrefSize(300200);
        primaryStage.setTitle("View Test");
        primaryStage.setScene(new Scene(ap));
        primaryStage.show();
    }
 
    public static void main(String[] args) {
        launch(args);
    }
}
cs

8. Pane, Scene, Stage, Label, Toolkit, Dimension 사용

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
45
46
47
48
49
package Java.sungju.JavaProject3;
 
import java.awt.Dimension;
import java.awt.Toolkit;
 
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.text.Font;
import javafx.stage.Stage;
 
public class Main extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {
        // 창 띄우기 이름은 setTitle로 지정
        primaryStage.setTitle("Quiz01");
        primaryStage.show();
 
        // 문구 내용
        Label lb = new Label("Infiscap world!");
        // 문구 크기
        lb.setFont(new Font(100));
 
        // lb를 넘겨주면 글씨가 띄어진다. ,값,값 입력시 사이즈 조정가능
        // 창 크기 조정
        Scene scene = new Scene(lb, 1000700);
 
        primaryStage.setScene(scene);
 
        // 2초후에
        Thread.sleep(2000);
 
        // awt값으로 불러오기
        Toolkit tk = Toolkit.getDefaultToolkit();
        Dimension screenSize = tk.getScreenSize();
        // 디스플레이 사이즈 얻어오기
        int x = screenSize.width;
        int y = screenSize.height;
        // 창 위치 조정 디스플레이사이즈/2 - Scene사이즈/2 하면 중간위치
        primaryStage.setX(x / 2 - 500);
        primaryStage.setY(y / 2 - 350);
        primaryStage.show();
 
    }
 
    public static void main(String[] args) {
        launch(args);
    }
}
cs

9. StackPane

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
package Java.sungju.JavaProject3;
 
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
 
public class StackPane01 extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {
        StackPane stack = new StackPane();
        // 사각형 사이즈, 색상지정
        Rectangle rect = new Rectangle(150100, Color.RED);
        Label lbl = new Label("Go!");
        // Pos.좌표를 이용한 위치 설정
        //StackPane.setAlignment(lbl, Pos.TOP_LEFT);
        //StackPane.setAlignment(rect, Pos.BOTTOM_RIGHT);
        StackPane.setMargin(rect, new Insets(100,8,8,8));
        StackPane.setMargin(lbl, new Insets(100,8,8,8));
        stack.getChildren().addAll(rect, lbl);
        primaryStage.setTitle("StackPaneEx");
        primaryStage.setScene(new Scene(stack,300,250));
        primaryStage.show();
    }
 
    public static void main(String[] args) {
        launch(args);
    }
}
cs