ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • JavaFX - CustomPopUp
    Java 2018. 2. 14. 11:09
    반응형
    package CustomPopUp;
        
    import javafx.application.Application;
    import javafx.fxml.FXMLLoader;
    import javafx.scene.Parent;
    import javafx.scene.Scene;
    import javafx.stage.Stage;


    public class Main extends Application {
        @Override
        public void start(Stage primaryStage) {
            try {
                FXMLLoader loader = new FXMLLoader(getClass().getResource("Custom.fxml"));          
                Parent root = loader.load();
                
                Scene scene = new Scene(root,400,400);
                scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
                primaryStage.setScene(scene);
                primaryStage.show();
            } catch(Exception e) {
                e.printStackTrace();
            }
        }
        
        public static void main(String[] args) {
            launch(args);
        }
    }








    package CustomPopUp;

    import java.io.IOException;
    import java.net.URL;
    import java.util.ResourceBundle;

    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.Label;
    import javafx.stage.Modality;
    import javafx.stage.Stage;
    import javafx.stage.StageStyle;

    public class MainController implements Initializable {
        
        @FXML private Button btn;
        @FXML private Button closeBtn;
        @FXML private Label msg;
        
        Stage stage;
        Stage pop;
        
        @Override
        public void initialize(URL location, ResourceBundle resources) {
            
        }
        
        // 팝업 버튼을 눌렀을 경우
        public void Popup() {
            // 팝업은 변경불가, 변경하려면 StyleSheet 사용
            // stage는 변경 가능
            
            // 1. Dialogue 설정
            // StageStyle.DECORATED <- 일반적인 윈도우
            pop = new Stage(StageStyle.DECORATED);
            
            // 2. 스테이지 취득
            stage = (Stage) btn.getScene().getWindow();
            
            // 3. pop Stage에 대한 옵션 설정
            pop.initModality(Modality.WINDOW_MODAL); // 창의 형태
            pop.initOwner(stage); // 부모창 설정
            pop.setTitle("팝업창"); // 제목 설정
            
            try {
                // 4. fxml 불러오기
                Parent fxml = FXMLLoader.load(getClass().getResource("Custom.fxml"));
                // 4. Scene에 fxml(Ctrl + LayOut) 추가
                Scene sc = new Scene(fxml);
                // 5. Scene을 스테이지에 추가
                pop.setScene(sc);
                // 6. 스테이지 게시
                pop.show();
                
            } catch (IOException e) {
                e.printStackTrace();
            }
            
        }
        
        public void closeBtn() {
            msg.setText("Close 버튼을 누르셨네요!");
            pop = (Stage) closeBtn.getScene().getWindow();
            pop.close();
        }
        
    }






    <?xml version="1.0" encoding="UTF-8"?>

    <?import javafx.scene.control.Button?>
    <?import javafx.scene.layout.BorderPane?>

    <BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="400.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="CustomPopUp.MainController">
    <center>
    <Button fx:id="btn" mnemonicParsing="false" onAction="#Popup" text="Button" BorderPane.alignment="CENTER" />
    </center>
    </BorderPane>





    <?xml version="1.0" encoding="UTF-8"?>

    <?import javafx.scene.control.Button?>
    <?import javafx.scene.control.Label?>
    <?import javafx.scene.layout.AnchorPane?>


    <AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="200.0" prefWidth="250.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="CustomPopUp.MainController">
    <children>
    <Label fx:id="msg" alignment="TOP_CENTER" layoutX="10.0" layoutY="23.0" text="확인하셨나요?" textFill="#e80a0a" AnchorPane.bottomAnchor="140.0" AnchorPane.leftAnchor="10.0" AnchorPane.rightAnchor="10.0" AnchorPane.topAnchor="23.0" />
    <Button fx:id="closeBtn" layoutX="101.0" layoutY="125.0" mnemonicParsing="false" prefHeight="50.0" prefWidth="50.0" text="Close" AnchorPane.bottomAnchor="52.0" AnchorPane.leftAnchor="80.0" AnchorPane.rightAnchor="80.0" AnchorPane.topAnchor="125.0" />
    </children>
    </AnchorPane>


    반응형

    'Java' 카테고리의 다른 글

    STS, Elcipse - 기본 설정  (0) 2018.07.17
    Java OOP 정리  (0) 2018.07.05
    JavaFX - FXML - Scene Builder  (0) 2018.02.08
    Java - JavaFX  (0) 2018.02.08
    Java - Lambda  (0) 2018.02.07

    댓글

Designed by Tistory.