2019.01.14 JavaFx 활용하기
2019. 1. 14. 16:56ㆍJAVA
#JavaFX 활용하기
①MainApp : 메인화면 설정
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 | package com.exam.app; import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.stage.Stage; public class MainApp extends Application { @Override public void start(Stage primaryStage) throws Exception{ Parent root = FXMLLoader.load(getClass().getResource("view/Root.fxml")); Scene scene = new Scene(root); primaryStage.setScene(scene); primaryStage.setTitle("창 제목"); primaryStage.setResizable(true); primaryStage.show(); } public static void main(String[] args) { launch(args); }//main() } | cs |
② Root.fxml : 첫번째 화면
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 | <?xml version="1.0" encoding="UTF-8"?> <?import javafx.scene.control.Button?> <?import javafx.scene.control.CheckBox?> <?import javafx.scene.control.Label?> <?import javafx.scene.control.RadioButton?> <?import javafx.scene.control.Separator?> <?import javafx.scene.control.Slider?> <?import javafx.scene.control.TextArea?> <?import javafx.scene.control.ToggleGroup?> <?import javafx.scene.image.Image?> <?import javafx.scene.image.ImageView?> <?import javafx.scene.layout.AnchorPane?> <?import javafx.scene.text.Font?> <AnchorPane prefHeight="685.0" prefWidth="825.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.exam.app.view.RootController"> <children> <Label fx:id="label" alignment="CENTER" contentDisplay="TOP" layoutX="-8.0" layoutY="97.0" prefHeight="179.0" prefWidth="854.0" text="JavaFX" textFill="#ab5050"> <font> <Font size="87.0" /> </font> </Label> <Slider fx:id="slider" layoutX="260.0" layoutY="262.0" majorTickUnit="10.0" prefHeight="16.0" prefWidth="319.0" showTickLabels="true" showTickMarks="true" value="35.0" /> <Button fx:id="btn" layoutX="400.0" layoutY="309.0" mnemonicParsing="false" text="확인" /> <TextArea fx:id="textArea2" layoutX="35.0" layoutY="78.0" prefHeight="23.0" prefWidth="771.0" /> <TextArea fx:id="textArea1" layoutX="35.0" layoutY="34.0" prefHeight="23.0" prefWidth="771.0" /> <CheckBox fx:id="chkG" layoutX="35.0" layoutY="351.0" mnemonicParsing="false" text="Glasses" userData="안경" /> <CheckBox fx:id="chkH" layoutX="35.0" layoutY="378.0" mnemonicParsing="false" text="Hair" userData="모자" /> <ImageView fx:id="imageView" fitHeight="91.0" fitWidth="121.0" layoutX="124.0" layoutY="348.0" pickOnBounds="true" preserveRatio="true"> <image> <Image url="@images/geek.gif" /> </image> </ImageView> <Separator layoutX="260.0" layoutY="348.0" orientation="VERTICAL" prefHeight="99.0" prefWidth="9.0" /> <RadioButton layoutX="280.0" layoutY="351.0" mnemonicParsing="false" text="BubbleChart" UserData="BubbleChart.png"> <toggleGroup> <ToggleGroup fx:id="group" /> </toggleGroup> </RadioButton> <RadioButton layoutX="280.0" layoutY="423.0" mnemonicParsing="false" text="AreaChart" toggleGroup="$group" UserData="AreaChart.png" /> <RadioButton layoutX="280.0" layoutY="386.0" mnemonicParsing="false" selected="true" text="BarChart" toggleGroup="$group" userData="BarChart.png"/> <ImageView fx:id="radioImageView" fitHeight="108.0" fitWidth="217.0" layoutX="400.0" layoutY="348.0" pickOnBounds="true" preserveRatio="true"> <image> <Image url="@images/BarChart.png" /> </image> </ImageView> <Button fx:id="btnSecond" layoutX="693.0" layoutY="588.0" mnemonicParsing="false" prefHeight="63.0" prefWidth="113.0" text="다음화면" /> </children> </AnchorPane> | 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 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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | package com.exam.app.view; import java.io.IOException; import java.net.URL; import java.util.ResourceBundle; import javafx.beans.binding.Bindings; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.fxml.FXML; import javafx.fxml.FXMLLoader; import javafx.fxml.Initializable; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.CheckBox; import javafx.scene.control.Label; import javafx.scene.control.Slider; import javafx.scene.control.TextArea; import javafx.scene.control.Toggle; import javafx.scene.control.ToggleGroup; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.text.Font; import javafx.stage.Stage; public class RootController implements Initializable { @FXML private Label label; @FXML private Slider slider; @FXML private Button btn,btnSecond; @FXML private TextArea textArea1, textArea2; @FXML private CheckBox chkG, chkH; @FXML private ImageView imageView,radioImageView; @FXML private ToggleGroup group; @Override public void initialize(URL location, ResourceBundle resources) { /*다음 화면 이동 : 화면전환*/ btnSecond.setOnAction(event -> { try { Parent second =FXMLLoader.load(getClass().getResource("SecondRoot.fxml")); Scene scene = new Scene(second); Stage primaryStage =(Stage) btnSecond.getScene().getWindow(); primaryStage.setScene(scene); } catch (IOException e) { // TODO Auto-generated catch bloc"r e.printStackTrace(); } }); /* btn 동작 -> slider의 위치값 Console에 출력, 내부에서 실수로 출력 */ // slider.getValue()값은 실수이므로 int로 강제 형변환 btn.setOnAction(event -> { System.out.println("슬라이더 값: " + (int) (slider.getValue())); /*radio값 출력*/ Toggle toggle = group.getSelectedToggle(); System.out.println("선택된 라디오버튼의 값 :"+ toggle.getUserData().toString()); }); /* change 이벤트 */ slider.valueProperty().addListener(new ChangeListener<Number>() { // slier의 value값이 바뀔대 changed()메소드를 호출 -> Number타입을 호출 @Override public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) { label.setFont(new Font(newValue.doubleValue())); }// changed()메소드 });// slider{}, value값이 바뀔 때마다 특정메소드를 동작 /* 바인딩 */ // 단방향 바인딩,진행 방향 "<-" : textArea2 <-textArea1 // textArea2.textProperty().bind(textArea1.textProperty());;//StringProperty를 // 리턴,단방향 바인딩 // textArea2.textProperty().unbind();//단방향 바인딩 해제 /* 양방향 바인딩 */ // textArea2 <-> textArea1 Bindings.bindBidirectional(textArea1.textProperty(), textArea2.textProperty()); /* * 양방향 바인딩 해제 * Bindings.unbindBidirectional(textArea1.textProperty(),textArea2.textProperty( * )); */ // 체크박스관련 이벤트 스타트 chkH.setOnAction(checkboxEvenHandler); chkG.setOnAction(checkboxEvenHandler); // 라디오 버튼 그룹에 chageListener 등록 group.selectedToggleProperty().addListener(new ChangeListener<Toggle>() { @Override public void changed(ObservableValue<? extends Toggle> observable, Toggle oldValue, Toggle newValue) { String strName = newValue.getUserData().toString(); //이미지이름을 가져옴 radioImageView.setImage(new Image(getClass().getResource("images/"+strName).toString())); } }); //토글에 선택된 요소를 리턴 }// initialize{} /* checkBox actionEvent -> 체크박스는 복수 선택이 가능 checkBox상황에 따라 사진이미지 반경*/ EventHandler<ActionEvent> checkboxEvenHandler = new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent event) { if (chkG.isSelected() && chkH.isSelected()) { imageView.setImage(new Image(getClass().getResource("images/geek-glasses-hair.gif").toString())); String str1= chkG.getUserData().toString(); String str2= chkH.getUserData().toString(); System.out.println(str1+"+"+str2); } else if (chkG.isSelected()) { imageView.setImage(new Image(getClass().getResource("images/geek-glasses.gif").toString())); String str1= chkG.getUserData().toString(); System.out.println(str1); } else if (chkH.isSelected()) { imageView.setImage(new Image(getClass().getResource("images/geek-hair.gif").toString())); String str2= chkH.getUserData().toString(); System.out.println(str2); } else { imageView.setImage(new Image(getClass().getResource("images/geek.gif").toString())); } } };// checkboxEvenHandler() } | cs |
④ secondRoot.fxml : 두번째 화면
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 | <?xml version="1.0" encoding="UTF-8"?> <?import javafx.scene.control.Button?> <?import javafx.scene.control.ComboBox?> <?import javafx.scene.control.DatePicker?> <?import javafx.scene.control.Label?> <?import javafx.scene.control.PasswordField?> <?import javafx.scene.control.Separator?> <?import javafx.scene.control.TextArea?> <?import javafx.scene.control.TextField?> <?import javafx.scene.layout.AnchorPane?> <AnchorPane prefHeight="685.0" prefWidth="825.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.exam.app.view.SecondRootController"> <children> <Label layoutX="106.0" layoutY="102.0" text="제목" /> <Label layoutX="94.0" layoutY="134.0" text="비밀번호" /> <TextField fx:id="txtTitle" layoutX="156.0" layoutY="98.0" prefHeight="23.0" prefWidth="575.0" /> <PasswordField fx:id="txtPassword" layoutX="156.0" layoutY="130.0" prefHeight="23.0" prefWidth="150.0" /> <Label layoutX="103.0" layoutY="170.0" text="공개" /> <ComboBox fx:id="comboPublic" layoutX="156.0" layoutY="166.0" prefWidth="150.0" promptText="선택하세요" /> <DatePicker fx:id="dateExit" layoutX="568.0" layoutY="166.0" promptText="날짜를 선택하세요" /> <Label layoutX="498.0" layoutY="170.0" text="게시종료" /> <Label layoutX="100.0" layoutY="257.0" text="내용" /> <TextArea fx:id="txtContent" layoutX="94.0" layoutY="288.0" prefHeight="200.0" prefWidth="642.0" /> <Separator layoutX="92.0" layoutY="539.0" prefHeight="0.0" prefWidth="642.0" /> <Button fx:id="btnReg" layoutX="263.0" layoutY="558.0" mnemonicParsing="false" prefHeight="39.0" prefWidth="87.0" text="등록" /> <Button fx:id="btnClc" layoutX="479.0" layoutY="558.0" mnemonicParsing="false" prefHeight="39.0" prefWidth="87.0" text="취소" /> <Button fx:id="btnHome" layoutX="14.0" layoutY="14.0" mnemonicParsing="false" text="홈으로 돌아가기" /> <PasswordField fx:id="txtPassword" layoutX="156.0" layoutY="130.0" prefHeight="23.0" prefWidth="150.0" /> </children> </AnchorPane> | cs |
⑤ SecondRootController.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 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 | package com.exam.app.view; import java.io.IOException; import java.net.URL; import java.time.LocalDate; import java.util.ResourceBundle; import javafx.application.Platform; import javafx.collections.FXCollections; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.fxml.FXMLLoader; import javafx.fxml.Initializable; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.ComboBox; import javafx.scene.control.DatePicker; import javafx.scene.control.PasswordField; import javafx.scene.control.TextArea; import javafx.scene.control.TextField; import javafx.stage.Stage; public class SecondRootController implements Initializable { @FXML private Button btnHome,btnClc,btnReg; @FXML private TextField txtTitle; @FXML private PasswordField txtPassword; @FXML private ComboBox<String> comboPublic; @FXML private DatePicker dateExit; @FXML private TextArea txtContent; @Override public void initialize(URL location, ResourceBundle resources) { /* 홈화면이동:화면전환 */ btnHome.setOnAction(event->{ try { Parent root = FXMLLoader.load(getClass().getResource("Root.fxml")); Scene scene = new Scene(root); Stage primaryStage =(Stage)btnHome.getScene().getWindow(); primaryStage.setScene(scene); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }); /* 프로그램 종료 */ btnClc.setOnAction(event->handleBtnCancleAction(event)); /* 콤보박스에 글자 넣기*/ comboPublic.setItems(FXCollections.observableArrayList("공개","비공개")); /*등록버튼을 txtContent로 출력*/ btnReg.setOnAction(event->handleBtnRegAction(event)); }//initialize() // 버튼 종료 메소드 public void handleBtnCancleAction(ActionEvent event) { Platform.exit(); }//handleBtnCancleAction() // 등록 메소드 public void handleBtnRegAction(ActionEvent event) { String title =txtTitle.getText(); String password = txtPassword.getText(); String strPublic = comboPublic.getValue(); LocalDate localDate =dateExit.getValue(); String strDate =""; if(localDate != null) { strDate = localDate.toString(); } //String content = txtContent.getText(); System.out.println("제목:"+title); System.out.println("비밀번호 :"+password); System.out.println("공개설정:"+strPublic); System.out.println("종료일자:"+strDate); //내용출력 txtContent.setText("제목:"+title+"\n"+"비밀번호 :"+password+"\n"+"공개설정:"+strPublic+"\n"+"종료일자:"+strDate); }//handleBtnRegAction() } | cs |
⑥ 출력 화면
'JAVA' 카테고리의 다른 글
2019.01.18 파일 읽어오기 (0) | 2019.01.18 |
---|---|
2019.01.15 JavaFX에서 키보드 동작(액션) (1) | 2019.01.15 |
2018.01.11 멀티 스레드의 활용 (0) | 2019.01.11 |
2019.01.09 스레드 Thread, 예외처리 (0) | 2019.01.09 |
2019.01.09 컬렉션 프레임워크:Map,Set,list (0) | 2019.01.09 |