2013-08-16 2 views
0

다음은 어떤 오류 또는 경고가 발생했을 때 대화 상자를 표시하는 코드입니다 ( ). 텍스트와 버튼을 정렬 할 수 없습니다. 두 가지가 서로를 덮는다.javafx의 대화 상자에서 텍스트와 버튼을 정렬 할 수 없습니다.

//INITIALIZING SCENE FOR dialogStage 
    StackPane dialogStackPane = new StackPane(); 
    dialogStackPane.setAlignment(Pos.CENTER); 
    dialogStackPane.setLayoutX(0); 
    dialogStackPane.setLayoutY(0); 
    dialogStackPane.setPrefWidth(250.0); 
    dialogStackPane.setPrefHeight(150.0); 

    // dialogStage height and width 
    dialogStage.setHeight(150.0); 
    dialogStage.setWidth(250.0); 

    //Scene for dialogStage 
    sceneDialog = new Scene(dialogStackPane, 150.0, 250.0); 
    sceneDialog.getStylesheets().add("/styles/Login.css"); 

    // Dialog box button 
    btnDialog = new Button("close"); 
    btnDialog.setMinSize(10.0, 20.0); 
    ObservableList ol = dialogStackPane.getChildren(); 
    ol.add(textWarning); 
    ol.add(btnDialog); 
    textWarning.setX(0); 
    textWarning.setY(0); 

    btnDialog.setLayoutX(0); 
    btnDialog.setLayoutY(0); 

    // SETTING THE MODALITY OF THE DIALOG BOX 
    dialogStage.initModality(Modality.APPLICATION_MODAL); 
    dialogStage.setTitle("Warning"); 
    dialogStage.setScene(sceneDialog);   

    // Setting event listener on warning dialogStage button b 
    btnDialog.addEventFilter(MouseEvent.ANY, new EventHandler<MouseEvent>() { 
     // This handles the Mouse event of the dialog box button. 
     @Override 
     public void handle(MouseEvent t) { 
      if (t.getEventType() == MouseEvent.MOUSE_CLICKED) { 
       dialogStage.close(); 
      } 
     } 
    }); 

    textWarning.setLayoutX(20); 
    textWarning.setLayoutY(200); 

// textWarning.setTextOrigin (VPos.TOP); // textWarning.setTextAlignment (TextAlignment.LEFT);

// Sets the warning box 
    dialogStage.setResizable(false);  

어떤 도움을 주시면 감사하겠습니다.

+0

나는이 반드시 귀하의 질문에 대답을 알고 있지만 사용으로 간주 가지고 GUI - 빌더, [자바 FX 장면 빌더 (같은 http://www.oracle.com/technetwork/java/javafx/tools/ index.html)? –

+0

나는 동적으로 생성되어야하는 경고 상자를 만들고 있는데 별도의 컨트롤러가 필요하지 않습니다. 모르겠다 fxml 파일을 사용하여 프로그램에서 경고 상자를 표시 할 수 있습니까? –

답변

1

StackPane 대신 AnchorPane을 사용하십시오. 그런 다음 AnchorPane 클래스의 정적 메소드로 노드를 배치 할 수 있습니다.

AnchorPane pane = new AnchorPane(); 
... 
AnchorPane.setLeftAnchor(textWarning, 50.0); 
AnchorPane.setLeftAnchor(btnDialog, 50.0); 
AnchorPane.setTopAnchor(textWarning, 20.0); 
AnchorPane.setTopAnchor(btnDialog, 50.0); 
... 
pane.getChildren().addAll(textWarning, btnDialog); 
... 
sceneDialog = new Scene(pane, 150.0, 250.0); 
... 
+0

대단히 감사합니다. –

관련 문제