2014-11-03 83 views
4

我如何確定JavaFX中的舞臺/窗口插頁?在搖擺,我可以簡單的寫:JavaFX 8:舞臺鑲嵌(窗飾厚度)?

JFrame frame = new JFrame(); 
Insets insets = frame.getInsets(); 

什麼是JavaFX中相當於得到了邊框的大小和窗口的標題欄?

回答

4

您可以通過查看相對於窗口寬度和高度的場景邊界來確定這些。

給定Scene scene;scene.getX()scene.getY()給出窗口內的Scene的x和y座標。這些分別相當於左邊和頂部的插圖。

的右側和底部稍微棘手,但

scene.getWindow().getWidth()-scene.getWidth()-scene.getX() 

給出正確的插圖,同樣

scene.getWindow().getHeight()-scene.getHeight()-scene.getY() 

給出了底部插圖。

這些值當然只有在場景放置在窗口中並且窗口在屏幕上可見時纔有意義。

如果你真的想要一個Insets對象,你可以這樣做以下(如果邊框或標題欄更改尺寸顯示的窗口之後,甚至會留下來有效):

import javafx.application.Application; 
import javafx.beans.binding.Bindings; 
import javafx.beans.binding.ObjectBinding; 
import javafx.geometry.Insets; 
import javafx.geometry.Pos; 
import javafx.scene.Scene; 
import javafx.scene.control.Label; 
import javafx.scene.layout.VBox; 
import javafx.stage.Stage; 

public class WindowInsetsDemo extends Application { 

    @Override 
    public void start(Stage primaryStage) { 

     Label topLabel = new Label(); 
     Label leftLabel = new Label(); 
     Label rightLabel = new Label(); 
     Label bottomLabel = new Label(); 


     VBox root = new VBox(10, topLabel, leftLabel, bottomLabel, rightLabel); 
     root.setAlignment(Pos.CENTER); 

     Scene scene = new Scene(root, 600, 400); 


     ObjectBinding<Insets> insets = Bindings.createObjectBinding(() -> 
     new Insets(scene.getY(), 
       primaryStage.getWidth()-scene.getWidth() - scene.getX(), 
       primaryStage.getHeight()-scene.getHeight() - scene.getY(), 
       scene.getX()), 
       scene.xProperty(), 
       scene.yProperty(), 
       scene.widthProperty(), 
       scene.heightProperty(), 
       primaryStage.widthProperty(), 
       primaryStage.heightProperty() 
      ); 

     topLabel.textProperty().bind(Bindings.createStringBinding(() -> "Top: "+insets.get().getTop(), insets)); 
     leftLabel.textProperty().bind(Bindings.createStringBinding(() -> "Left: "+insets.get().getLeft(), insets)); 
     rightLabel.textProperty().bind(Bindings.createStringBinding(() -> "Right: "+insets.get().getRight(), insets)); 
     bottomLabel.textProperty().bind(Bindings.createStringBinding(() -> "Bottom: "+insets.get().getBottom(), insets)); 

     primaryStage.setScene(scene); 
     primaryStage.show(); 
    } 

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

謝謝。很好的解決方案。 – tobain 2014-11-04 07:02:24