본문 바로가기
비트 장기/JAVA

9/11 spring

by woohyun22 2018. 9. 11.

javafx ui만들기    



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package prj100;
 
import javafx.application.Application;
import javafx.stage.Stage;
 
public class prj100 extends Application{
    @Override
    public void start(Stage primaryStage) throws Exception {
        // TODO Auto-generated method stub
        System.out.println(2);
        primaryStage.show();
        System.out.println(3);
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println(1);
        launch(); //런처함수내부에서는  반드시 start함수를 출력하게 되어있다.
        System.out.println(4);//창을 닫으면 런쳐코드가 끝난다.
        
        
    }
}
cs



버튼class에 이벤트 연결하는법


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
50
51
52
53
package prj100;
 
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class prj100 extends Application{
    @Override
    public void start(Stage primaryStage) throws Exception {
        VBox root = new VBox();
        root.setPrefSize(400300);//만들고자 하는 창의 가로400 세로 300으로 만들어준다.
        //
        Button btn1 = new Button("버튼 1");
        btn1.setOnAction(new EventHandler<ActionEvent>() {
 
            @Override
            public void handle(ActionEvent event) {
                // TODO Auto-generated method stub
                System.out.println("버튼이 눌러짐");
            }
        });
        Button btn2 = new Button("버튼 2");
        btn2.setOnAction(new EventHandler<ActionEvent>() {
 
            @Override
            public void handle(ActionEvent event) {
                // TODO Auto-generated method stub
                System.out.println("호랑이");
            }
        });
        root.getChildren().addAll(btn1,btn2);
 
        //
        Scene scene = new Scene(root);
        primaryStage.setScene(scene);
        System.out.println(2);
        primaryStage.setTitle("server");
        primaryStage.show();
        System.out.println(3);
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println(1);
        launch(); //런처함수내부에서는  반드시 start함수를 출력하게 되어있다.
        System.out.println(4);//창을 닫으면 런쳐코드가 끝난다.
 
 
    }
}
 
cs



333


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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package prj100;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class prj100 extends Application{
    @Override
    public void start(Stage primaryStage) throws Exception {
        VBox root = new VBox();
        root.setPrefSize(400300);//만들고자 하는 창의 가로400 세로 300으로 만들어준다.
        //
        Button btn1 = new Button("접속");
        btn1.setOnAction(new EventHandler<ActionEvent>() {
 
            @Override
            public void handle(ActionEvent event) {
                // TODO Auto-generated method stub
                System.out.println("클라이언트");
            }
        });
        Button btn2 = new Button("버튼 2");
        btn2.setOnAction(new EventHandler<ActionEvent>() {
 
            @Override
            public void handle(ActionEvent event) {
                // TODO Auto-generated method stub
                System.out.println("호랑이");
            }
        });
        TextArea textarea = new TextArea();
        TextField textfield = new TextField();//import할떄 javafx가 들어간 것으로 해줘야됩니다.
        textfield.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                // TODO Auto-generated method stub
                System.out.println("코끼리");
                String s = textfield.getText();
                textarea.appendText(s+"\n");
                textfield.setText("");
            }
        });
        root.getChildren().addAll(btn1,btn2,textfield,textarea);
        //
        Scene scene = new Scene(root);
        primaryStage.setScene(scene);
        System.out.println(2);
        primaryStage.setTitle("server");
        primaryStage.show();
        System.out.println(3);
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println(1);
        launch(); //런처함수내부에서는  반드시 start함수를 출력하게 되어있다.
        System.out.println(4);//창을 닫으면 런쳐코드가 끝난다.
    }
}
 
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package prj100;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class prj100 extends Application{
    @Override
    public void start(Stage primaryStage) throws Exception {
        VBox root = new VBox();
        root.setPrefSize(400300);//만들고자 하는 창의 가로400 세로 300으로 만들어준다.
        //
        Button btn1 = new Button("버튼 1");
        Button btn2 = new Button("버튼 2");
        Button btn3 = new Button("버튼 3");
        Button btn4 = new Button("버튼 4");
        Button btn5 = new Button("버튼 5");
        btn1.setOnAction(new EventHandler<ActionEvent>() {
 
            @Override
            public void handle(ActionEvent event) {
                // TODO Auto-generated method stub
                System.out.println("버튼1");
                btn1.setVisible(false);//버턴1 사라져라
            }
        });
        btn2.setOnAction(new EventHandler<ActionEvent>() {
 
            @Override
            public void handle(ActionEvent event) {
                // TODO Auto-generated method stub
                System.out.println("버튼2");
                btn1.setVisible(true);//버턴 1 나와라
            }
        });
        btn3.setOnAction(new EventHandler<ActionEvent>() {
 
            @Override
            public void handle(ActionEvent event) {
                // TODO Auto-generated method stub
                System.out.println("버튼3");
                btn3.setDisable(true);//비활성화
            }
        });
        btn4.setOnAction(new EventHandler<ActionEvent>() {
 
            @Override
            public void handle(ActionEvent event) {
                // TODO Auto-generated method stub
                System.out.println("버튼4");
                btn3.setDisable(false);
                btn2.setVisible(true);
            }
        });
        btn5.setOnAction(new EventHandler<ActionEvent>() {
 
            @Override
            public void handle(ActionEvent event) {
                // TODO Auto-generated method stub
                System.out.println("버튼5");
                btn1.setText("호랑이");//이벤트안에서 남의 객체를 얼마든지 바꿀수 있다.
                btn2.setVisible(false);
                btn3.setDisable(true);
            }
        });
        root.getChildren().addAll(btn1,btn2,btn3,btn4,btn5);
        //
        Scene scene = new Scene(root);
        primaryStage.setScene(scene);
        System.out.println(2);
        primaryStage.setTitle("server");
        primaryStage.show();
        System.out.println(3);
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println(1);
        launch(); //런처함수내부에서는  반드시 start함수를 출력하게 되어있다.
        System.out.println(4);//창을 닫으면 런쳐코드가 끝난다.
    }
}
 
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package prj100;
 
 
 
import javafx.application.Platform;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class prj100 extends Application{
    @Override
    public void start(Stage primaryStage) throws Exception {
        VBox root = new VBox();
        root.setPrefSize(400300);//만들고자 하는 창의 가로400 세로 300으로 만들어준다.
        //
        Button btn1 = new Button("버튼 1");
        Button btn2 = new Button("버튼 2");
        Button btn3 = new Button("버튼 3");
        Button btn4 = new Button("버튼 4");
        Button btn5 = new Button("버튼 5");
        Thread t = new Thread() {
            @Override
            public void run() {
                System.out.println("앵무새");
            }
        };
        
        t.start();//thread run실행
        btn1.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                // TODO Auto-generated method stub
                System.out.println("버튼1");
                btn1.setVisible(false);//버턴1 사라져라
                new Thread() {//스레드 형식 두가지
                    @Override
                    public void run() {
                        System.out.println("소나무");
//스레드 안에서 컨트롤의 상태를 변경시키는 것은 1차적으로 위법이다.
                        javafx.application.Platform.runLater(()->{btn1.setText("대나무");});
                    }
                }.start();
            }
        });
        btn2.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                // TODO Auto-generated method stub
                System.out.println("버튼2");
                btn1.setVisible(true);//버턴 1 나와라
            }
        });
        btn3.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                // TODO Auto-generated method stub
                System.out.println("버튼3");
                btn3.setDisable(true);//비활성화
            }
        });
        btn4.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                // TODO Auto-generated method stub
                System.out.println("버튼4");
                btn3.setDisable(false);
                btn2.setVisible(true);
            }
        });
        btn5.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                // TODO Auto-generated method stub
                System.out.println("버튼5");
                btn1.setText("호랑이");//이벤트안에서 남의 객체를 얼마든지 바꿀수 있다.
                btn2.setVisible(false);
                btn3.setDisable(true);
            }
        });
        root.getChildren().addAll(btn1,btn2,btn3,btn4,btn5);
        //
        Scene scene = new Scene(root);
        primaryStage.setScene(scene);
        System.out.println(2);
        primaryStage.setTitle("server");
        primaryStage.show();
        System.out.println(3);
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println(1);
        launch(); //런처함수내부에서는  반드시 start함수를 출력하게 되어있다.
        System.out.println(4);//창을 닫으면 런쳐코드가 끝난다.
    }
}
 
cs


728x90

'비트 장기 > JAVA' 카테고리의 다른 글

9/7 java 총정리 시작  (0) 2018.09.07
java복습 1  (0) 2018.08.29
8/14 java for문 연습  (0) 2018.08.14
8/8 자바 정리 보충  (0) 2018.08.08
8/7 (8/4에 이어서 기본 예제 형식)  (0) 2018.08.07

댓글