2016-10-29 77 views
5

我有以下問題:如何在不同分辨率下自動調整JavaFx中的窗口大小?

我已經創建了全高清桌面上一個JavaFX窗口,我設置這樣的情景:

Scene scene = new Scene(root,1475,1015); 

當我運行1360*760在筆記本電腦上的應用分辨率,我看不到整個應用程序,我無法調整它。

如何設置我的應用程序自動調整桌面/筆記本電腦的功能和它的分辨率和尺寸?

回答

2

我相信你正在尋找這個

GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice(); 
int width = gd.getDisplayMode().getWidth(); 
int height = gd.getDisplayMode().getHeight(); 

這將允許您使用您的設備的屏幕大小和所有你需要做的調整是使你的程序中的對象的長/寬與屏幕的寬度和高度成正比。

+0

但如何,如果我有家長根內的另一個窗格和TabPane和TableView中那些在這個尺寸設置應用程序將調整?是否會自動調整大小? –

+0

您需要製作屏幕尺寸比例以及iirc。 –

0

提的是:

1)你必須使用建立在JavaFX的佈局(BorderPane,GridPane ....等...)

2)沒有automaticall.You有編程它這樣做。

3)這是一個常見的例子,你想知道沒有任務欄。在你getVisualBounds()...

主要發揮這種情況下,屏幕(寬度或高度)(在Windows,Linux和Mac上,Solaris)上主題


你問Responsive Design。下面是你要make.Although究竟是不是最好的解決辦法,這個我的意思是可以有更好的表現來修改一個例子(我還添加了一些代碼如果它是移動窗口StageStyle.UNDECORATED拖動窗口已經看到這一點):

enter image description here

import javafx.application.Application; 
import javafx.scene.Cursor; 
import javafx.scene.Scene; 
import javafx.scene.input.MouseButton; 
import javafx.scene.layout.BorderPane; 
import javafx.scene.paint.Color; 
import javafx.stage.Screen; 
import javafx.stage.Stage; 
import javafx.stage.StageStyle; 

public class FX extends Application { 

    int screenWidth = (int) Screen.getPrimary().getBounds().getWidth(); 
    int screenHeight = (int) Screen.getPrimary().getBounds().getHeight(); 

    Stage stage; 
    Scene scene; 

    int initialX; 
    int initialY; 

    @Override 
    public void start(Stage s) throws Exception { 

     // root 
     BorderPane root = new BorderPane(); 
     root.setStyle("-fx-background-color:rgb(186,153,122,0.7); -fx-background-radius:30;"); 

     // Responsive Design 
     int sceneWidth = 0; 
     int sceneHeight = 0; 
     if (screenWidth <= 800 && screenHeight <= 600) { 
      sceneWidth = 600; 
      sceneHeight = 350; 
     } else if (screenWidth <= 1280 && screenHeight <= 768) { 
      sceneWidth = 800; 
      sceneHeight = 450; 
     } else if (screenWidth <= 1920 && screenHeight <= 1080) { 
      sceneWidth = 1000; 
      sceneHeight = 650; 
     } 

     // Scene 
     stage = new Stage(); 
     stage.initStyle(StageStyle.TRANSPARENT); 
     scene = new Scene(root, sceneWidth, sceneHeight, Color.TRANSPARENT); 

     // Moving 
     scene.setOnMousePressed(m -> { 
      if (m.getButton() == MouseButton.PRIMARY) { 
       scene.setCursor(Cursor.MOVE); 
       initialX = (int) (stage.getX() - m.getScreenX()); 
       initialY = (int) (stage.getY() - m.getScreenY()); 
      } 
     }); 

     scene.setOnMouseDragged(m -> { 
      if (m.getButton() == MouseButton.PRIMARY) { 
       stage.setX(m.getScreenX() + initialX); 
       stage.setY(m.getScreenY() + initialY); 
      } 
     }); 

     scene.setOnMouseReleased(m -> { 
      scene.setCursor(Cursor.DEFAULT); 
     }); 

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

    /** 
    * Main Method 
    * 
    * @param args 
    */ 
    public static void main(String[] args) { 
     launch(args); 
    } 

} 
+0

但是根內的其他組件將如何調整大小? –

+0

@Dina Bogdan這就是爲什麼你使用Layouts.It會自動完成,但你必須定義邏輯。我的意思是說,在全高清3容器顯示在窗口上。1200 *分辨率可能或可能不能顯示3個容器。因此,您將只顯示2個或以不同佈局顯示它們。您必須閱讀有關該書的書籍。無法在一個問題中回答......這就像您正在製作計算機應用程序你想在手機上顯示... – GOXR3PLUS

+0

我明白你在說什麼......我在想我可以使用像相對位置或類似的東西。我想在所有類型的計算機上運行我的應用程序,並且在GUI中遇到問題.... –

相關問題