有没有办法让页面上的 "autofit" 个元素占据整个 canvas
Is there a way to "autofit" elements on a page so they take up the entire canvas
在 JavaFX 中,有没有一种方法可以“自动调整”页面上的元素,使它们占据整个页面?
目前,我正在尝试让 window 有两个按钮一起占据整个 canvas,但我不确定该怎么做,因为可以拉伸 window,等等。我试过玩弄 Button.setPrefSize
,但按钮大小保持不变,它只显示带有两个超大按钮的 window,其中的文本不可见。
我目前拥有的
我想要的(但对于任何 window 尺寸)
这是一种方法(代码在这里,但也可以在 Scene Builder 和 FXML 中使用):
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.stage.Stage;
public class TestApplication extends Application {
@Override
public void start(Stage stage) throws Exception {
Button button1 = new Button("Button1");
HBox.setHgrow(button1, Priority.SOMETIMES);
button1.setMaxWidth(Double.MAX_VALUE);
button1.setMaxHeight(Double.MAX_VALUE);
Button button2 = new Button("Button2");
HBox.setHgrow(button2, Priority.SOMETIMES);
button2.setMaxWidth(Double.MAX_VALUE);
button2.setMaxHeight(Double.MAX_VALUE);
HBox hBox = new HBox(button1, button2);
AnchorPane.setLeftAnchor(hBox, 0.0);
AnchorPane.setRightAnchor(hBox, 0.0);
AnchorPane.setTopAnchor(hBox, 0.0);
AnchorPane.setBottomAnchor(hBox, 0.0);
AnchorPane rootContainer = new AnchorPane(hBox);
Scene scene = new Scene(rootContainer, 600, 600);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
}
在 JavaFX 中,有没有一种方法可以“自动调整”页面上的元素,使它们占据整个页面?
目前,我正在尝试让 window 有两个按钮一起占据整个 canvas,但我不确定该怎么做,因为可以拉伸 window,等等。我试过玩弄 Button.setPrefSize
,但按钮大小保持不变,它只显示带有两个超大按钮的 window,其中的文本不可见。
我目前拥有的
我想要的(但对于任何 window 尺寸)
这是一种方法(代码在这里,但也可以在 Scene Builder 和 FXML 中使用):
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.stage.Stage;
public class TestApplication extends Application {
@Override
public void start(Stage stage) throws Exception {
Button button1 = new Button("Button1");
HBox.setHgrow(button1, Priority.SOMETIMES);
button1.setMaxWidth(Double.MAX_VALUE);
button1.setMaxHeight(Double.MAX_VALUE);
Button button2 = new Button("Button2");
HBox.setHgrow(button2, Priority.SOMETIMES);
button2.setMaxWidth(Double.MAX_VALUE);
button2.setMaxHeight(Double.MAX_VALUE);
HBox hBox = new HBox(button1, button2);
AnchorPane.setLeftAnchor(hBox, 0.0);
AnchorPane.setRightAnchor(hBox, 0.0);
AnchorPane.setTopAnchor(hBox, 0.0);
AnchorPane.setBottomAnchor(hBox, 0.0);
AnchorPane rootContainer = new AnchorPane(hBox);
Scene scene = new Scene(rootContainer, 600, 600);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
}