2014-12-09 6 views
0

코드에 새 스테이지를 만들고 스테이지가 장식되어 있지 않아야합니다! 이 경우 새로 만든 스테이지에 드래그 기능이 없어집니다.VBox 깜박임 끌기

다음 코드를 작성 했으므로 이제 스테이지를 끌 수 있습니다. 그러나 스테이지는 깜박 거리기 때문에 한 곳이 아닌 일부 픽셀의 반경에서 깜박입니다. 이 문제를 어떻게 해결할 수 있습니까? 당신의 도움을 주셔서 감사합니다.

예 - CODE : 그래서 그것을 고정

 import javafx.application.Application; 
import javafx.beans.property.ObjectProperty; 
import javafx.beans.property.SimpleObjectProperty; 
import javafx.event.EventHandler; 
import javafx.geometry.Insets; 
import javafx.geometry.Point2D; 
import javafx.scene.Cursor; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.control.Label; 
import javafx.scene.control.MenuBar; 
import javafx.scene.input.MouseEvent; 
import javafx.scene.layout.HBox; 
import javafx.scene.layout.Pane; 
import javafx.scene.layout.Priority; 
import javafx.scene.layout.VBox; 
import javafx.stage.Stage; 
import javafx.stage.StageStyle; 

public class DragStuff extends Application { 

    VBox mainContainer; 
    Stage mainSt; 

    @Override 
    public void start(Stage mainStage) throws Exception { 
     // TODO Auto-generated method stub 
     mainStage.initStyle(StageStyle.UNDECORATED); 
     mainSt = mainStage; 
     mainContainer = new VBox(); 
     mainContainer.setStyle("-fx-background-color:red"); 

     Label headlineInformation = new Label("Testing"); 
     headlineInformation.getStyleClass().addAll("popup-label-name"); 
     headlineInformation.setMaxWidth(Double.MAX_VALUE); 

     Button closeButton = new Button("X"); 
     closeButton.setVisible(true); 
     closeButton.getStyleClass() 
       .addAll("popup-button", "popup-button-color"); 

     HBox headContainer = new HBox(); 
     HBox.setHgrow(headlineInformation, Priority.ALWAYS); 
     headContainer.setPadding(new Insets(5, 5, 5, 5)); 
     headContainer.getChildren().addAll(headlineInformation, closeButton); 

     Pane pane = new Pane(); 
     pane.getChildren() 
       .addAll(new Label(
         "Text                      Stuff")); 

     mainContainer.getChildren().addAll(headContainer, pane); 

     Scene sc = new Scene(mainContainer); 
     mainStage.setScene(sc); 
     mainStage.show(); 

     dragHandling(); 

    } 

    public void dragHandling() { 
     final ObjectProperty<Point2D> mouseKoordinates = new SimpleObjectProperty<>(); 
     mainContainer.setOnMousePressed(new EventHandler<MouseEvent>() { 
      @Override 
      public void handle(MouseEvent event) { 
       mouseKoordinates.set(new Point2D(event.getSceneX(), event 
         .getSceneY())); 
       mainContainer.getScene().setCursor(Cursor.HAND); 
      }; 
     }); 

     mainContainer.setOnMouseReleased(new EventHandler<MouseEvent>() { 
      @Override 
      public void handle(final MouseEvent arg0) { 
       mouseKoordinates.set(null); 
       mainContainer.getScene().setCursor(Cursor.DEFAULT); 
      } 
     }); 

     mainContainer.setOnMouseDragged(new EventHandler<MouseEvent>() { 
      @Override 
      public void handle(MouseEvent event) { 
       if (mouseKoordinates.get() != null) { 
        double x = event.getX(); 
        double deltaX = x - mouseKoordinates.get().getX(); 
        double y = event.getY(); 
        double deltaY = y - mouseKoordinates.get().getY(); 
        mainSt.setX(mainSt.getX() + deltaX); 
        mainSt.setY(mainSt.getY() + deltaY); 
//     mouseKoordinates.set(new Point2D(x, y)); 
       } 
      } 
     }); 
    } 

    public static void main(String[] args) { 
     launch(args); 
    } 
} 
+0

내 대답에 이미 언급했듯이'mouseKoordinates.set (new Point2D (x, y)); '를 사용하지 않아도됩니다. 그냥 제거하십시오. – ItachiUchiha

+0

@ItachiUchiha하지만 작동하지 않습니다 ... 시도해 보셨습니까 ... mouseKoordinates.set (...)로 해당 줄을 삭제하면 작동하지 않습니다. –

+0

예. 작동합니다. 당신은 setOnMouseDragged (...)에서만 그것을 제거 할 필요가 있습니다. setOnMousePressed (...) – ItachiUchiha

답변

1

우리가 잘못된 참조를했다 렸기 때문에, 그것은 작동하지 않습니다! 다음 코드는 완벽하게 작동합니다! X/Y 값이 아닌 ScreenX/Y 값을 마우스로 눌러 마우스 좌표를 설정해야합니다! 새로운 점을 설정하는 것도 중요합니다!

import javafx.application.Application; 
import javafx.beans.property.ObjectProperty; 
import javafx.beans.property.SimpleObjectProperty; 
import javafx.event.EventHandler; 
import javafx.geometry.Insets; 
import javafx.geometry.Point2D; 
import javafx.scene.Cursor; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.control.Label; 
import javafx.scene.control.MenuBar; 
import javafx.scene.input.MouseEvent; 
import javafx.scene.layout.HBox; 
import javafx.scene.layout.Pane; 
import javafx.scene.layout.Priority; 
import javafx.scene.layout.VBox; 
import javafx.stage.Stage; 
import javafx.stage.StageStyle; 

public class DragStuff extends Application { 

    VBox mainContainer; 
    Stage mainSt; 

    @Override 
    public void start(Stage mainStage) throws Exception { 
     // TODO Auto-generated method stub 
     mainStage.initStyle(StageStyle.UNDECORATED); 
     mainSt = mainStage; 
     mainContainer = new VBox(); 
     mainContainer.setStyle("-fx-background-color:red"); 

     Label headlineInformation = new Label("Testing"); 
     headlineInformation.getStyleClass().addAll("popup-label-name"); 
     headlineInformation.setMaxWidth(Double.MAX_VALUE); 

     Button closeButton = new Button("X"); 
     closeButton.setVisible(true); 
     closeButton.getStyleClass() 
       .addAll("popup-button", "popup-button-color"); 

     HBox headContainer = new HBox(); 
     HBox.setHgrow(headlineInformation, Priority.ALWAYS); 
     headContainer.setPadding(new Insets(5, 5, 5, 5)); 
     headContainer.getChildren().addAll(headlineInformation, closeButton); 

     Pane pane = new Pane(); 
     pane.getChildren() 
       .addAll(new Label(
         "Text                      Stuff")); 

     mainContainer.getChildren().addAll(headContainer, pane); 

     Scene sc = new Scene(mainContainer); 
     mainStage.setScene(sc); 
     mainStage.show(); 

     dragHandling(); 

    } 

    public void dragHandling() { 
     final ObjectProperty<Point2D> mouseKoordinates = new SimpleObjectProperty<>(); 
     mainContainer.setOnMousePressed(new EventHandler<MouseEvent>() { 
      @Override 
      public void handle(MouseEvent event) { 
       //Important to create the new Point with a Reference to the Screen! 
       mouseKoordinates.set(new Point2D(event.getScreenX(), event 
         .getScreenY())); 
       mainContainer.getScene().setCursor(Cursor.HAND); 
      }; 
     }); 

     mainContainer.setOnMouseReleased(new EventHandler<MouseEvent>() { 
      @Override 
      public void handle(final MouseEvent arg0) { 
       mouseKoordinates.set(null); 
       mainContainer.getScene().setCursor(Cursor.DEFAULT); 
      } 
     }); 

     mainContainer.setOnMouseDragged(new EventHandler<MouseEvent>() { 
      @Override 
      public void handle(MouseEvent event) { 
       if (mouseKoordinates.get() != null) { 
        //getScreenX and not getX 
        double x = event.getScreenX(); 
        double deltaX = x - mouseKoordinates.get().getX(); 
        //getScreenY and not getY 
        double y = event.getScreenY(); 
        double deltaY = y - mouseKoordinates.get().getY(); 
        mainSt.setX(mainSt.getX() + deltaX); 
        mainSt.setY(mainSt.getY() + deltaY); 
        //VERY IMPORANT 
        mouseKoordinates.set(new Point2D(x, y)); 
       } 
      } 
     }); 
    } 

    public static void main(String[] args) { 
     launch(args); 
    } 
}