如何禁用 JavaFX 中的最大化选项?
How to disable the maximizing option in JavaFX?
我正在创建一个 JavaFX 程序。我已经使用 .fxml 代码来设计页面但是有一个问题,每当我 运行 程序它在默认高度和宽度下工作正常但是当我最大化那个结果时,按钮和其他内容保持在原来的状态默认高度和宽度的位置。他们没有调整高度和宽度。因为它是一个mp3播放器,可以执行一些其他的小功能,所以我希望它的大小不应该是可扩展的(不能最大化)。这也能解决我的问题。
.fxml代码
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.text.Font?>
<GridPane alignment="top_left" hgap="10" prefHeight="475.0" prefWidth="800.0" style="-fx-background-color: white;" vgap="10" xmlns="http://javafx.com/javafx/8.0.121" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<columnConstraints>
<ColumnConstraints />
</columnConstraints>
<rowConstraints>
<RowConstraints />
<RowConstraints />
</rowConstraints>
<children>
<Label prefHeight="49.0" prefWidth="800.0" text="Spotify Downloader Music Player">
<font>
<Font name="Comic Sans MS" size="24.0" />
</font>
<GridPane.margin>
<Insets left="10.0" />
</GridPane.margin></Label>
<GridPane GridPane.rowIndex="1">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="392.0" minWidth="10.0" prefWidth="253.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="551.0" minWidth="10.0" prefWidth="391.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="551.0" minWidth="10.0" prefWidth="217.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Button mnemonicParsing="false" prefHeight="30.0" prefWidth="275.0" text="Home">
<font>
<Font size="14.0" />
</font>
</Button>
<Button mnemonicParsing="false" prefHeight="45.0" prefWidth="257.0" text="Playlist" GridPane.rowIndex="1">
<font>
<Font size="14.0" />
</font>
</Button>
<Button mnemonicParsing="false" prefHeight="47.0" prefWidth="257.0" text="Settings" GridPane.rowIndex="2">
<font>
<Font size="14.0" />
</font>
</Button>
<Button mnemonicParsing="false" prefHeight="51.0" prefWidth="257.0" text="About" GridPane.rowIndex="3">
<font>
<Font size="14.0" />
</font>
</Button>
<Label prefHeight="33.0" prefWidth="253.0" text="Music name" GridPane.columnIndex="1">
<font>
<Font size="14.0" />
</font>
<GridPane.margin>
<Insets left="10.0" />
</GridPane.margin>
</Label>
<TextField prefHeight="34.0" prefWidth="543.0" GridPane.columnIndex="1" GridPane.rowIndex="1">
<font>
<Font size="14.0" />
</font>
<GridPane.margin>
<Insets left="10.0" />
</GridPane.margin>
</TextField>
<Button mnemonicParsing="false" text="Search" GridPane.columnIndex="2" GridPane.rowIndex="1">
<GridPane.margin>
<Insets left="10.0" />
</GridPane.margin>
</Button>
</children>
</GridPane>
</children>
</GridPane>
如何禁用 JavaFX 中的最大化选项?
如何改变大小扩展的内容位置?
您可以使用实用程序 window,它会从工具栏中删除最大化和最小化选项。
stage.initStyle(StageStyle.UTILITY);
或者您可以在舞台上设置最大高度和宽度。例如:
stage.setMaxHeight(400.0);
stage.setMaxWidth(600.0);
然而,如果用户可以选择最大化,他们可能会感到困惑,但 window 只会达到特定高度。
如果您将 Stage
的 resizable
property 设置为 false
,它会禁用最大化按钮,同时保留最小化按钮。
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
// Create Scene and add it to primaryStage
primaryStage.setResizable(false);
primaryStage.show();
}
}
这只会阻止用户调整 Stage
的大小,但不会阻止您(开发人员)以编程方式调整 Stage
的大小。
Defines whether the Stage is resizable or not by the user.
Programatically you may still change the size of the Stage. This is a
hint which allows the implementation to optionally make the Stage
resizable by the user.
Javadoc 指出这只是对实现的提示。然而,我只能在 Windows 10 机器上测试它,但是你在问题评论中提到它在 Linux 机器上工作。
我正在创建一个 JavaFX 程序。我已经使用 .fxml 代码来设计页面但是有一个问题,每当我 运行 程序它在默认高度和宽度下工作正常但是当我最大化那个结果时,按钮和其他内容保持在原来的状态默认高度和宽度的位置。他们没有调整高度和宽度。因为它是一个mp3播放器,可以执行一些其他的小功能,所以我希望它的大小不应该是可扩展的(不能最大化)。这也能解决我的问题。
.fxml代码
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.text.Font?>
<GridPane alignment="top_left" hgap="10" prefHeight="475.0" prefWidth="800.0" style="-fx-background-color: white;" vgap="10" xmlns="http://javafx.com/javafx/8.0.121" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<columnConstraints>
<ColumnConstraints />
</columnConstraints>
<rowConstraints>
<RowConstraints />
<RowConstraints />
</rowConstraints>
<children>
<Label prefHeight="49.0" prefWidth="800.0" text="Spotify Downloader Music Player">
<font>
<Font name="Comic Sans MS" size="24.0" />
</font>
<GridPane.margin>
<Insets left="10.0" />
</GridPane.margin></Label>
<GridPane GridPane.rowIndex="1">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="392.0" minWidth="10.0" prefWidth="253.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="551.0" minWidth="10.0" prefWidth="391.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="551.0" minWidth="10.0" prefWidth="217.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Button mnemonicParsing="false" prefHeight="30.0" prefWidth="275.0" text="Home">
<font>
<Font size="14.0" />
</font>
</Button>
<Button mnemonicParsing="false" prefHeight="45.0" prefWidth="257.0" text="Playlist" GridPane.rowIndex="1">
<font>
<Font size="14.0" />
</font>
</Button>
<Button mnemonicParsing="false" prefHeight="47.0" prefWidth="257.0" text="Settings" GridPane.rowIndex="2">
<font>
<Font size="14.0" />
</font>
</Button>
<Button mnemonicParsing="false" prefHeight="51.0" prefWidth="257.0" text="About" GridPane.rowIndex="3">
<font>
<Font size="14.0" />
</font>
</Button>
<Label prefHeight="33.0" prefWidth="253.0" text="Music name" GridPane.columnIndex="1">
<font>
<Font size="14.0" />
</font>
<GridPane.margin>
<Insets left="10.0" />
</GridPane.margin>
</Label>
<TextField prefHeight="34.0" prefWidth="543.0" GridPane.columnIndex="1" GridPane.rowIndex="1">
<font>
<Font size="14.0" />
</font>
<GridPane.margin>
<Insets left="10.0" />
</GridPane.margin>
</TextField>
<Button mnemonicParsing="false" text="Search" GridPane.columnIndex="2" GridPane.rowIndex="1">
<GridPane.margin>
<Insets left="10.0" />
</GridPane.margin>
</Button>
</children>
</GridPane>
</children>
</GridPane>
如何禁用 JavaFX 中的最大化选项?
如何改变大小扩展的内容位置?
您可以使用实用程序 window,它会从工具栏中删除最大化和最小化选项。
stage.initStyle(StageStyle.UTILITY);
或者您可以在舞台上设置最大高度和宽度。例如:
stage.setMaxHeight(400.0);
stage.setMaxWidth(600.0);
然而,如果用户可以选择最大化,他们可能会感到困惑,但 window 只会达到特定高度。
如果您将 Stage
的 resizable
property 设置为 false
,它会禁用最大化按钮,同时保留最小化按钮。
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
// Create Scene and add it to primaryStage
primaryStage.setResizable(false);
primaryStage.show();
}
}
这只会阻止用户调整 Stage
的大小,但不会阻止您(开发人员)以编程方式调整 Stage
的大小。
Defines whether the Stage is resizable or not by the user. Programatically you may still change the size of the Stage. This is a hint which allows the implementation to optionally make the Stage resizable by the user.
Javadoc 指出这只是对实现的提示。然而,我只能在 Windows 10 机器上测试它,但是你在问题评论中提到它在 Linux 机器上工作。