尝试让我的应用程序阶段在加载时调整大小

时间:2019-04-16 04:40:17

标签: java javafx scene stage

我希望单击链接时调整窗口大小。当用户单击“忘记密码”超链接时,窗口应缩小。但是我无法改变窗口大小。我一直在尝试用另一个阶段设置大小,但是它不起作用。

我尝试在其控制器的构造函数中设置阶段,但这也不起作用。

公共类Main扩展Application {     私人ConnectToDb db;

@Override
public void start(Stage primaryStage) throws IOException {
    db = new ConnectToDb();
    try {
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(getClass().getResource("/MainLayout.fxml"));
        loader.setController(new MainController());

        primaryStage.setTitle("Ameenazon");
        primaryStage.setWidth(800);
        primaryStage.setHeight(800);

        Scene scene = new Scene(loader.load());
        scene.getStylesheets().add("/StoreHomePageCSS.css");
     //   primaryStage.setScene(new Scene(loader.load()));
        primaryStage.setScene(scene);
        primaryStage.show();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
public static void main(String[] args) {
    launch(args);
}
}

登录控制器

public class LoginController {
    @FXML
   private Hyperlink forgotPasswordHyperlink = new Hyperlink();
   private Stage stage = new Stage();

public LoginController(MainController mainController) {
    this.mainController = mainController;
}
private void initialize() {
    forgotPasswordHyperlink.setOnAction(e ->{
        try{
            FXMLLoader loader = new FXMLLoader(getClass().getResource("/ForgotPasswordLayout.fxml"));
            loader.setController(new ForgotPasswordController(mainController));

            stage.setWidth(300);
            stage.setHeight(300);
            mainController.getContentPane().getChildren().clear();
            mainController.getContentPane().getChildren().add(loader.load());
        } catch (IOException d){
            d.printStackTrace();
        }
    });
    }

忘记密码控制器

public class ForgotPasswordController {    
    private MainController mainController;

    public ForgotPasswordController(MainController mainController){
        this.mainController = mainController;
}

忘记密码fxml

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="300.0" prefWidth="300.0" xmlns="http://javafx.com/javafx/8.0.172-ea" xmlns:fx="http://javafx.com/fxml/1">
   <center>
  <GridPane alignment="CENTER" hgap="5.0" vgap="5.0" BorderPane.alignment="CENTER">
     <children>
        <Label alignment="CENTER" text="Username: " GridPane.columnIndex="0" GridPane.rowIndex="5" />
        <TextField GridPane.columnIndex="4" GridPane.columnSpan="2" GridPane.rowIndex="5" />
        <ButtonBar prefHeight="40.0" prefWidth="200.0" GridPane.columnIndex="4" GridPane.rowIndex="6">
          <buttons>
              <Button fx:id="btnCancel" alignment="BASELINE_CENTER" minWidth="13.0" mnemonicParsing="false" prefHeight="13.0" text="Cancel" />
              <Button mnemonicParsing="false" prefHeight="27.0" prefWidth="168.0" text="Send Email" />
          </buttons>
        </ButtonBar>
     </children>
     <BorderPane.margin>
        <Insets />
     </BorderPane.margin>
  </GridPane>
  </center>
  <top>
  <Label text="Forgot Password?" BorderPane.alignment="CENTER">
     <font>
        <Font size="26.0" />
     </font>
  </Label>
  </top>
 </BorderPane>

登录fxml

<BorderPane maxHeight="390" maxWidth="600" minHeight="-Infinity" 
minWidth="-Infinity" prefHeight="390.0" prefWidth="600.0" 
xmlns="http://javafx.com/javafx/8.0.172-ea" 
xmlns:fx="http://javafx.com/fxml/1">
   <center>
       <GridPane BorderPane.alignment="CENTER">
          <children>
              <VBox prefHeight="235.0" prefWidth="205.0" 
    GridPane.columnIndex="1" GridPane.rowIndex="2"/>
        </children>
     </GridPane>
  </center>
</BorderPane>

因此,理想情况下,当用户单击丢失的密码超链接时,窗口将调整为首选尺寸(300x300)。现在,它的默认值为800 x 800(在主类中设置)。谢谢你的帮助。

0 个答案:

没有答案