2016-10-29 76 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中遇到问题.... –

相关问题