Java7到Java8 - Javafx应用程序中元素之间的间距问题

时间:2014-11-05 11:34:53

标签: java javafx javafx-2 javafx-8

Java7到Java8 - UI问题,Javafx应用程序中元素之间的间距。

这不是重复问题,因为我已经确定了默认皮肤。

当我使用Java7运行我的应用程序时,它显示正确的UI,具有适当的背景颜色。 但是当我使用Java8运行相同的应用程序时,它显示的UI与Jav​​a7的UI不同。 某些元素重叠,例如GridPane中的按钮,GridPane中的元素之间的某些间距减小,而背景颜色未正确设置。

目前,我在FXML文件中引用了CSS,但是在删除之后我进行了测试。仍存在问题。

请提供解决方案,以便我可以使用Java7和Java8为javafx应用程序提供相同的UI。

我尝试通过以下两种方式解决问题。 1.在控制器的舞台方法中使用System.setProperty(" javafx.userAgentStylesheetUrl"," CASPIAN")。
2.在命令行上使用-Djavafx.userAgentStylesheetUrl = caspian运行。

以下是样本
的代码 的 t1.fxml

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

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

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
   <center>
      <VBox prefHeight="200.0" prefWidth="100.0" BorderPane.alignment="CENTER">
         <children>
            <StackPane prefHeight="150.0" prefWidth="200.0">
               <children>
                  <HBox prefHeight="100.0" prefWidth="200.0">
                     <children>
                         <GridPane maxHeight="285.0" maxWidth="285.0" minHeight="286.0" minWidth="200.0" prefHeight="286.0" prefWidth="278.0" HBox.hgrow="ALWAYS">
                            <columnConstraints>
                              <ColumnConstraints halignment="LEFT" hgrow="SOMETIMES" maxWidth="110.0" minWidth="10.0" prefWidth="50.0" />
                              <ColumnConstraints halignment="LEFT" hgrow="SOMETIMES" maxWidth="110.0" minWidth="10.0" prefWidth="50.0" />
                                <ColumnConstraints halignment="LEFT" hgrow="SOMETIMES" maxWidth="110.0" minWidth="10.0" prefWidth="50.0" />
                                <ColumnConstraints halignment="CENTER" hgrow="SOMETIMES" maxWidth="162.0" minWidth="10.0" prefWidth="100.0" />
                            </columnConstraints>
                            <rowConstraints>
                              <RowConstraints maxHeight="36.0" minHeight="10.0" percentHeight="5.0" prefHeight="26.0" vgrow="SOMETIMES" />
                              <RowConstraints maxHeight="66.0" minHeight="10.0" percentHeight="19.0" prefHeight="66.0" vgrow="SOMETIMES" />
                              <RowConstraints maxHeight="66.0" minHeight="10.0" percentHeight="19.0" prefHeight="66.0" vgrow="SOMETIMES" />
                                <RowConstraints maxHeight="66.0" minHeight="10.0" percentHeight="19.0" prefHeight="66.0" vgrow="SOMETIMES" />
                                <RowConstraints maxHeight="55.0" minHeight="10.0" percentHeight="19.0" prefHeight="55.0" vgrow="SOMETIMES" />
                                <RowConstraints maxHeight="55.0" minHeight="10.0" percentHeight="19.0" prefHeight="55.0" valignment="CENTER" vgrow="SOMETIMES" />
                            </rowConstraints>
                             <children>
                                <BorderPane prefHeight="200.0" prefWidth="200.0" GridPane.columnIndex="3" GridPane.rowIndex="2">
                                 <center>
                                    <Button maxHeight="50.0" maxWidth="105.0" minHeight="50.0" minWidth="105.0" mnemonicParsing="false" prefHeight="50.0" prefWidth="105.0" styleClass="keyButton" BorderPane.alignment="CENTER" />
                                 </center></BorderPane>
                              <VBox prefHeight="200.0" prefWidth="100.0" />
                              <Button contentDisplay="CENTER" maxHeight="50.0" maxWidth="50.0" minHeight="50.0" minWidth="50.0" mnemonicParsing="false" prefHeight="50.0" prefWidth="50.0" styleClass="keyButton" text="7" GridPane.rowIndex="2" />
                              <Button maxHeight="50.0" maxWidth="50.0" minHeight="50.0" minWidth="50.0" mnemonicParsing="false" prefHeight="50.0" prefWidth="50.0" styleClass="keyButton" text="8" GridPane.columnIndex="1" GridPane.rowIndex="2" />
                              <Button maxHeight="50.0" maxWidth="50.0" minHeight="50.0" minWidth="50.0" mnemonicParsing="false" prefHeight="50.0" prefWidth="50.0" styleClass="keyButton" text="9" GridPane.columnIndex="2" GridPane.rowIndex="2" />
                              <Button maxHeight="50.0" maxWidth="50.0" minHeight="50.0" minWidth="50.0" mnemonicParsing="false" prefHeight="50.0" prefWidth="50.0" styleClass="keyButton" text="4" GridPane.rowIndex="3" />
                              <Button maxHeight="50.0" maxWidth="50.0" minHeight="50.0" minWidth="50.0" mnemonicParsing="false" prefHeight="50.0" prefWidth="50.0" styleClass="keyButton" text="5" GridPane.columnIndex="1" GridPane.rowIndex="3" />
                              <Button maxHeight="50.0" maxWidth="50.0" minHeight="50.0" minWidth="50.0" mnemonicParsing="false" prefHeight="50.0" prefWidth="50.0" styleClass="keyButton" text="6" GridPane.columnIndex="2" GridPane.rowIndex="3" />
                              <Button maxHeight="50.0" maxWidth="50.0" minHeight="50.0" minWidth="50.0" mnemonicParsing="false" prefHeight="50.0" prefWidth="50.0" styleClass="keyButton" text="1" GridPane.rowIndex="4" />
                              <Button maxHeight="50.0" maxWidth="50.0" minHeight="50.0" minWidth="50.0" mnemonicParsing="false" prefHeight="50.0" prefWidth="50.0" styleClass="keyButton" text="2" GridPane.columnIndex="1" GridPane.rowIndex="4" />
                              <Button maxHeight="50.0" maxWidth="50.0" minHeight="50.0" minWidth="50.0" mnemonicParsing="false" prefHeight="50.0" prefWidth="50.0" styleClass="keyButton" text="3" GridPane.columnIndex="2" GridPane.rowIndex="4" />
                              <Button maxHeight="50.0" maxWidth="105.0" minHeight="50.0" minWidth="105.0" mnemonicParsing="false" prefHeight="50.0" prefWidth="105.0" styleClass="keyButton" text="0" GridPane.columnSpan="2" GridPane.rowIndex="5" />
                              <Button maxHeight="50.0" maxWidth="50.0" minHeight="50.0" minWidth="50.0" mnemonicParsing="false" prefHeight="50.0" prefWidth="50.0" styleClass="keyButton" text="," GridPane.columnIndex="2" GridPane.rowIndex="5" />
                              <Button maxHeight="50.0" maxWidth="105.0" minHeight="50.0" minWidth="105.0" mnemonicParsing="false" prefHeight="50.0" prefWidth="105.0" styleClass="keyButton" GridPane.columnIndex="3" GridPane.rowIndex="3" />
                              <Button maxHeight="104.0" maxWidth="105.0" minHeight="104.0" minWidth="105.0" mnemonicParsing="false" prefHeight="104.0" prefWidth="105.0" styleClass="keyButton" text="Enter" GridPane.columnIndex="3" GridPane.rowIndex="4" GridPane.rowSpan="2147483647" />
                              <VBox prefHeight="200.0" prefWidth="100.0" GridPane.rowIndex="1" />
                             </children>
                          </GridPane>
                     </children>
                  </HBox>
               </children>
            </StackPane>
         </children>
      </VBox>
   </center>

</BorderPane>

的style.css

.keyButton{
    -fx-font:bold 18px "Arial";
    -fx-background-color: rgb(240,240,240);
    -fx-border-radius: 10;
    -fx-background-radius:10;
    -fx-text-fill: black;
    -fx-border-color:rgb(206,206,206);
}
.keyButton:pressed{    
    -fx-background-color:rgb(217,217,217);
}

MainApp.java

public class MainApp extends Application {

     @Override
        public void start(Stage stage) throws Exception {
          Parent root = FXMLLoader.load(getClass().getResource("/fxml/t1.fxml"));

            Scene scene = new Scene(root);
            scene.getStylesheets().add("/style/Style.css");
            System.setProperty( "javafx.userAgentStylesheetUrl", "CASPIAN" );
            stage.setTitle("JavaFX and Maven");
            stage.setScene(scene);
            stage.show();


        }

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


}

我使用-Djavafx.userAgentStylesheetUrl = caspian参数运行应用程序

查看Java7中的这种XML差异&amp; Java8如下面的链接图像所示

Java7 http://i.stack.imgur.com/7fURv.png

Java8 http://i.stack.imgur.com/ejBAy.png

0 个答案:

没有答案