java - 使用 netbeans 和场景生成器运行程序之间的显示问题

标签 java javafx netbeans fxml

我面临一个显示问题,场景生成器的输出看起来与运行程序时的实际输出不同。这是一个例子: 左边是场景构建器的,右边是在 netbeans 上运行程序的。来自场景构建器的(左图)基于黑色方 block 具有相等的边。另一方面,来自 netbeans 的(右图)基于黑匣子具有不等边。还可以清楚地看到,右图像的尺寸比左图像大。我怎样才能解决这个问题?如何显示正确的图像以使两边相等?为什么在netbeans上运行时显示不同?

图片: enter image description here

您还可以在附件中找到 fxml 代码:

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

<?import com.jfoenix.controls.JFXButton?>
<?import com.jfoenix.controls.JFXPasswordField?>
<?import com.jfoenix.controls.JFXTextField?>
<?import de.jensd.fx.glyphs.fontawesome.FontAwesomeIconView?>
<?import java.lang.String?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>

<AnchorPane id="AnchorPane" prefHeight="439.0" prefWidth="300.0" styleClass="darkBackground" stylesheets="@../../resources/css/Consolidated.css" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="gui.controllers.LoginController">
   <children>
      <Pane prefHeight="148.0" prefWidth="300.0" styleClass="loginPaneBackground" AnchorPane.bottomAnchor="291.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
         <children>
            <Label fx:id="isConnected" alignment="CENTER" contentDisplay="CENTER" layoutX="25.0" layoutY="96.0" prefHeight="38.0" prefWidth="273.0" stylesheets="@../../resources/css/Consolidated.css" text="Login" textAlignment="CENTER" textFill="WHITE">
               <font>
                  <Font name="Roboto Regular" size="24.0" />
               </font>
               <styleClass>
                  <String fx:value="robotBold" />
                  <String fx:value="loginLabel" />
               </styleClass>
            </Label>
            <ImageView fitHeight="70.0" fitWidth="70.0" layoutX="126.0" layoutY="14.0" pickOnBounds="true" preserveRatio="true">
               <image>
                  <Image url="@../../resources/imgs/login_img.png" />
               </image>
            </ImageView>
         </children>
      </Pane>
      <VBox layoutX="6.0" layoutY="173.0" prefHeight="266.0" prefWidth="300.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0">
         <children>
            <HBox>
               <children>
                  <FontAwesomeIconView fill="WHITE" glyphName="USER" size="30" styleClass="icon_color">
                     <HBox.margin>
                        <Insets left="20.0" right="20.0" />
                     </HBox.margin>
                  </FontAwesomeIconView>
                  <JFXTextField fx:id="txtUsername" focusColor="#03a9f4" labelFloat="true" prefHeight="25.0" prefWidth="175.0" promptText="Username" stylesheets="@../../resources/css/Consolidated.css" unFocusColor="#bdbdbd">
                     <font>
                        <Font name="Century Gothic" size="12.0" />
                     </font>
                     <styleClass>
                        <String fx:value="txtFields" />
                        <String fx:value="robotReg" />
                     </styleClass>
                     <HBox.margin>
                        <Insets left="20.0" right="20.0" />
                     </HBox.margin>
                  </JFXTextField>
               </children>
               <VBox.margin>
                  <Insets bottom="10.0" left="20.0" right="20.0" top="10.0" />
               </VBox.margin>
            </HBox>
            <HBox>
               <children>
                  <FontAwesomeIconView fill="WHITE" glyphName="LOCK" size="30" styleClass="icon_color">
                     <HBox.margin>
                        <Insets left="20.0" right="20.0" />
                     </HBox.margin>
                  </FontAwesomeIconView>
                  <JFXPasswordField fx:id="txtPassword" focusColor="#03a9f4" labelFloat="true" prefHeight="23.0" prefWidth="175.0" promptText="Password" stylesheets="@../../resources/css/Consolidated.css" unFocusColor="#bdbdbd">
                     <font>
                        <Font name="Century Gothic Bold" size="12.0" />
                     </font>
                     <HBox.margin>
                        <Insets left="25.0" right="20.0" />
                     </HBox.margin>
                     <styleClass>
                        <String fx:value="txtFields" />
                        <String fx:value="robotReg" />
                     </styleClass>
                  </JFXPasswordField>
               </children>
               <VBox.margin>
                  <Insets bottom="10.0" left="20.0" right="20.0" top="10.0" />
               </VBox.margin>
            </HBox>
            <HBox>
               <children>
                  <FontAwesomeIconView fill="WHITE" glyphName="GEAR" size="30" styleClass="icon_color">
                     <HBox.margin>
                        <Insets left="18.0" right="20.0" />
                     </HBox.margin>
                  </FontAwesomeIconView>
                  <ComboBox fx:id="comboType" prefHeight="27.0" prefWidth="175.0" promptText="Type" stylesheets="@../../resources/css/Consolidated.css">
                     <styleClass>
                        <String fx:value="comboBox" />
                        <String fx:value="robotReg" />
                     </styleClass>
                     <opaqueInsets>
                        <Insets />
                     </opaqueInsets>
                     <HBox.margin>
                        <Insets left="20.0" right="20.0" />
                     </HBox.margin>
                     <padding>
                        <Insets bottom="3.0" left="-8.0" />
                     </padding>
                  </ComboBox>
               </children>
               <VBox.margin>
                  <Insets bottom="10.0" left="20.0" right="20.0" top="10.0" />
               </VBox.margin>
            </HBox>
            <HBox prefHeight="38.0" prefWidth="300.0">
               <children>
                  <JFXButton onAction="#Login" prefHeight="38.0" prefWidth="109.0" stylesheets="@../../resources/css/Consolidated.css" text="Login">
                     <styleClass>
                        <String fx:value="btns" />
                        <String fx:value="robotReg" />
                     </styleClass>
                     <HBox.margin>
                        <Insets left="27.25" right="27.25" />
                     </HBox.margin>
                  </JFXButton>
                  <JFXButton onAction="#cancel" prefHeight="38.0" prefWidth="109.0" stylesheets="@../../resources/css/Consolidated.css" text="Cancel">
                     <styleClass>
                        <String fx:value="btns" />
                        <String fx:value="robotReg" />
                     </styleClass>
                     <HBox.margin>
                        <Insets left="27.25" right="27.25" />
                     </HBox.margin>
                  </JFXButton>
               </children>
               <VBox.margin>
                  <Insets top="30.0" />
               </VBox.margin>
            </HBox>
         </children>
      </VBox>
   </children>
</AnchorPane>

最佳答案

您将 Label 的位置和大小设置为 Pane 中的绝对大小,这意味着 Label 保留其左上角坐标,但根据其首选大小调整大小。

您应该使用父布局来调整其可调整大小的子布局,而不是这样做。 VBox
Pane 替换为如下内容:

<VBox alignment="TOP_CENTER" prefHeight="148.0" prefWidth="300.0" styleClass="loginPaneBackground" AnchorPane.bottomAnchor="291.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" spacing="12.0">
   <children>
      <ImageView fitHeight="70.0" fitWidth="70.0" pickOnBounds="true" preserveRatio="true">
          <image>
              <Image url="@../../resources/imgs/login_img.png" />
          </image>
      </ImageView>
      <Label fx:id="isConnected" alignment="CENTER" maxWidth="Infinity"  text="Login" textFill="WHITE">
          <font>
              <Font name="Roboto Regular" size="24.0" />
          </font>
          <styleClass>
              <String fx:value="robotBold" />
              <String fx:value="loginLabel" />
          </styleClass>
       </Label>
   </children>
   <padding>
      <Insets bottom="24.0" top="14.0" />
   </padding>
</VBox>

关于java - 使用 netbeans 和场景生成器运行程序之间的显示问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49897859/

相关文章:

java.lang.RuntimeException : Unable to instantiate activity

java - 如何读取jar中的文件?

java - javafx 中的低级绑定(bind)(使用 computeValue)

java - 反编译Java项目并编译

web-services - Netbeans基本Http身份验证Jax-WS

带连字符的 Java 简单正则表达式

java - 使用 lambda 从 2 个现有对象创建新对象,总结 ArrayList 字段中各种对象的各种值

java - 在tabpane右侧添加一个按钮

java - 如何配置 Inno Setup 以检查 Visual C++ Redistributable Packages

无法反序列化访问实例字段和方法的 Java 8 lambda