【问题标题】:JavaFX - Undecorated windows has some problemsJavaFX - 未装饰的窗口有一些问题
【发布时间】:2017-12-05 12:54:23
【问题描述】:

我在 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 祝您有美好的一天

【问题讨论】:

  • 你能发布 FXML(和 CSS,如果你有样式表的话)吗?
  • 是的,等一下
  • 嗯。唔。它与未装饰的舞台无关:如果我将其注释掉,那么它具有相同的行为。如果我也使窗口可调整大小,然后在打开时调整它的大小,则会出现内容....不知道发生了什么。
  • 这是我的猜测。 It is not recommended to set the MinHeight, PrefHeight, or MaxHeight for this control. Unexpected behavior will occur because the TitledPane's height changes when it is opened or closed. Here。由于您将 TitlePane 设置为根节点并设置了舞台的高度和宽度。您固有地设置了 TitlePanes 的高度和宽度。

标签: user-interface javafx


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2011-12-29
    • 2015-03-21
    • 2013-08-12
    • 2014-06-27
    • 2015-02-16
    • 1970-01-01
    • 2012-10-23
    • 2013-02-20
    • 1970-01-01
    相关资源
    最近更新 更多