JavaFX - 未修饰的窗口有一些问题

时间:2017-07-01 21:19:43

标签: user-interface javafx

我在JavaFX中创建了一个未修饰的窗口,但随后我的所有组件都消失了。

我的代码:

public class Main extends Application {

    private double xOffset = 0;
    private double yOffset = 0;

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

        primaryStage.initStyle(StageStyle.UNDECORATED);
        primaryStage.setResizable(false);

        root.setOnMousePressed(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                xOffset = event.getSceneX();
                yOffset = event.getSceneY();
            }
        });
        root.setOnMouseDragged(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                primaryStage.setX(event.getScreenX() - xOffset);
                primaryStage.setY(event.getScreenY() - yOffset);
            }
        });


        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root, 600, 400));
        primaryStage.show();
    }


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

    public void init(){



    }

}

这是我的FXML文件:

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.RadioButton?>
<?import javafx.scene.control.SplitPane?>
<?import javafx.scene.control.TitledPane?>
<?import javafx.scene.effect.DropShadow?>
<?import javafx.scene.effect.InnerShadow?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.Region?>
<?import javafx.scene.text.Font?>

<TitledPane animated="false" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" text="Menu" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
  <content>
    <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
         <children>
            <SplitPane dividerPositions="0.29797979797979796" prefHeight="350.0" prefWidth="600.0">
              <items>
                <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="338.0" prefWidth="207.0" style="-fx-background-color: #333333;">
                     <children>
                        <RadioButton fx:id="test" layoutX="10.0" layoutY="95.0" mnemonicParsing="false" text="RadioButton" textFill="WHITE">
                           <font>
                              <Font size="14.0" />
                           </font>
                           <effect>
                              <DropShadow color="#4a4a4a" />
                           </effect>
                        </RadioButton>
                        <RadioButton fx:id="startBot" layoutX="10.0" layoutY="14.0" mnemonicParsing="false" onAction="#startBot" prefHeight="37.0" prefWidth="152.0" text="Start BlackHole-Bot" textFill="WHITE">
                           <font>
                              <Font size="14.0" />
                           </font>
                           <effect>
                              <DropShadow color="#545353" />
                           </effect>
                        </RadioButton>
                        <RadioButton fx:id="startNotepad" layoutX="10.0" layoutY="51.0" mnemonicParsing="false" onAction="#startNotepad" prefHeight="32.0" prefWidth="177.0" text="Notepad" textFill="WHITE">
                           <font>
                              <Font size="14.0" />
                           </font>
                           <effect>
                              <DropShadow color="#4a4949" />
                           </effect>
                        </RadioButton>
                     </children>
                  </AnchorPane>
                <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="348.0" prefWidth="330.0" style="-fx-background-color: #1a1a1a;">
                     <children>
                      <Button fx:id="confirmBtn" alignment="CENTER" layoutX="163.0" layoutY="258.0" mnemonicParsing="false" onAction="#confirm" text="Confirm">
                           <font>
                              <Font size="18.0" />
                           </font>
                           <effect>
                              <DropShadow />
                           </effect>
                        </Button>
                     </children>
                  </AnchorPane>
              </items>
            </SplitPane>
         </children></AnchorPane>
  </content>
   <effect>
      <InnerShadow />
   </effect>
   <graphic>
      <Region prefHeight="23.0" prefWidth="213.0" />
   </graphic>
   <font>
      <Font size="24.0" />
   </font>
</TitledPane>

这就是它在Scene Builder中的样子:

SceneBuilder image

提前感谢您帮助我:D祝您有愉快的一天

1 个答案:

答案 0 :(得分:0)

您的问题的解决方案是使用类似AnchorPane的东西作为根节点。我不知道为什么会这样。