当前位置: 首页 > 知识库问答 >
问题:

JavaFX窗格没有相应地缩放

方航
2023-03-14

FXML代码:

<?xml version="1.0" encoding="UTF-8"?>

<?import java.net.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="600" minWidth="800" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="nl.hsleiden.keesjes.controllers.GameController">
    <stylesheets>
        <URL value="@../../../game.css" />
    </stylesheets>
    <top>
        <BorderPane styleClass="header-section">
            <left>
                <Button onAction="#showHowToPlay" prefHeight="75.0" prefWidth="175.0" text="HOWTOPLAY" textAlignment="CENTER" />
            </left>
            <center><Label text="Keezen" /></center>
            <right>
                <HBox spacing="5">
                    <Button onAction="#pauseGame" prefHeight="75.0" prefWidth="175.0" text="PAUSE GAME" textAlignment="CENTER" />
                    <Button onAction="#switchToMenu" prefHeight="75.0" prefWidth="175.0" text="LEAVE GAME" textAlignment="CENTER" />
                </HBox>
            </right>
        </BorderPane>
    </top>
   <left>
      <BorderPane minHeight="505.0" minWidth="600.0" BorderPane.alignment="CENTER">
         <bottom>
            <FlowPane minHeight="105.0" minWidth="600.0" styleClass="cards" BorderPane.alignment="CENTER" />
         </bottom>
         <center>
            <Pane minHeight="400.0" minWidth="600.0" styleClass="game" BorderPane.alignment="CENTER" />
         </center>
      </BorderPane>
   </left>
   <right>
       <VBox minHeight="505.0" minWidth="200.0" BorderPane.alignment="CENTER">
           <Pane minHeight="200.0" minWidth="200.0" styleClass="players"/>
           <Pane minHeight="200.0" minWidth="200.0" styleClass="event"/>
           <Pane minHeight="105.0" minWidth="200.0" styleClass="chat"/>
       </VBox>
   </right>
</BorderPane>

和相关的CSS代码:

.root {
    -fx-font-size: 14px;
    -fx-background-color: #FFFFFF;
    -fx-font-family: "Roboto Light";
}
.header-section {
    -fx-padding: 10px;
    -fx-font-size: 20px;
    -fx-background-color: teal;
}
.header-section Label{
    -fx-text-fill: #FFFFFF;
    -fx-font-size: 200%;
    -fx-padding: 10px;
}
.game {
    -fx-background-color: lightgray;
}
.cards{
    -fx-background-color: lightgreen
}
.players{
    -fx-background-color: lightblue;
}
.event {
    -fx-background-color: yellow;
}
.chat{
    -fx-background-color: lime;
}

我如何保持宽高比并根据分辨率拉伸窗格?例如。在1920x1080分辨率下,浅灰色应为1280x720。在800x600分辨率下,它是600x400。以及介于两者之间的一切。

共有1个答案

戎兴言
2023-03-14

所以经过大量的搜索,我终于找到了一个答案。

<?xml version="1.0" encoding="UTF-8"?>

<?import java.net.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.stage.Screen?>

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="600" minWidth="800" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="nl.hsleiden.keesjes.controllers.GameController">
    <fx:define>
        <Screen fx:factory="getPrimary" fx:id="screen" />
    </fx:define>
    <stylesheets>
        <URL value="@../../../game.css" />
    </stylesheets>
    <top>
        <BorderPane styleClass="header-section">
            <left>
                <Button onAction="#showHowToPlay" prefHeight="75.0" prefWidth="175.0" text="HOWTOPLAY" textAlignment="CENTER" />
            </left>
            <center><Label text="Keezen" /></center>
            <right>
                <HBox spacing="5">
                    <Button onAction="#pauseGame" prefHeight="75.0" prefWidth="175.0" text="PAUSE GAME" textAlignment="CENTER" />
                    <Button onAction="#switchToMenu" prefHeight="75.0" prefWidth="175.0" text="LEAVE GAME" textAlignment="CENTER" />
                </HBox>
            </right>
        </BorderPane>
    </top>
    <center>
       <BorderPane fx:id="game" prefHeight="${screen.visualBounds.height}" prefWidth="${screen.visualBounds.width}" BorderPane.alignment="CENTER">
           <center>
               <BorderPane>
                   <center>
                       <Pane prefHeight="${game.height*0.8}" prefWidth="${game.width*0.8}" styleClass="game"/>
                   </center>
                   <bottom>
                       <Pane prefHeight="${game.height*0.2}" prefWidth="${game.width*0.8}" styleClass="cards" />
                   </bottom>
               </BorderPane>
           </center>
           <right>
               <VBox>
                   <Pane prefHeight="${game.height*0.4}" prefWidth="${game.width*0.2}" styleClass="players" />
                   <Pane prefHeight="${game.height*0.4}" prefWidth="${game.width*0.2}" styleClass="event" />
                   <Pane prefHeight="${game.height*0.2}" prefWidth="${game.width*0.2}" styleClass="chat" />
               </VBox>
           </right>
       </BorderPane>
   </center>
</BorderPane>

我最后做的是定义“游戏”部分的屏幕大小。这样,我就可以使用百分比(浮动)计算不同的部分,并使用边框窗格对齐项目。我得到了我要找的东西。

 类似资料:
  • 我正在用JavaFX和场景构建器编程一个音乐播放器。我的问题是,如果我调整我的程序的窗口大小,UI不会随之增长。如何使UI具有响应性? 在我加了SplashScreen之前,它工作得很好。从那时起,我试图让它工作,但我不能找到任何解决方案在网上! 请帮帮我,提前谢谢! FXML: 飞溅FXML:

  • 问题我如何可以嵌入这个应用程序到SPlitPane,在左边将是另一个面板。 不幸的是,代码导致了错误的坐标,

  • 我需要相对于鼠标位置放大/缩小滚动窗格。 我目前通过将我的内容包装在一个组中并缩放组本身来实现缩放功能。我创建了一个带有自定义透视的新Scale对象。(枢轴设置为鼠标位置) 如何在不同级别的缩放中获得正确的鼠标位置?是否有一种完全不同的方法来缩放滚动窗格更容易?

  • 我无法在SceneBuilder(eclipse)中使用GridPane,当我尝试在项目中插入GridPane时,SceneBuilder会在没有提示任何消息的情况下退出。 这就是我在日志文件中找到的:

  • 问题内容: 我有一个40x40的网格,显示地图。当前,我有以下方法监听右键单击: 我要实现以下内容: 如果右键单击后选择了菜单项,则我想将右键单击的位置保存在变量中, 例如[32,12](如果右键单击跨越了32个网格窗格,向上为12块)。 关于如何执行此操作的任何建议? 提前致谢 问题答案: 向网格中的每个单元格添加一个侦听器,而不是向舞台添加一个鼠标侦听器。用于上下文菜单处理的适当侦听器是处理程

  • 我是一个Java/JavaFX程序员新手,我正在开发一个简单的JavaFX建筑设计工具,在这个工具中,您可以画出墙壁、地板等,因此对象(主要是线、圆、多边形、矩形图像)是在屏幕上绘制和创建的,而不是在运行之前创建的。