下面是我的代码:
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class NotesApplication extends Application {
@Override
public void start(Stage stage) {
StackPane root = new StackPane();
root.setPrefSize(700,700);
StackPane leftPane = new StackPane();
leftPane.setPrefSize(100, 700);
Button button = new Button("Button 1");
leftPane.setBackground(new Background(new BackgroundFill(Color.YELLOW, CornerRadii.EMPTY, Insets.EMPTY)));
leftPane.getChildren().add(button);
leftPane.setAlignment(Pos.CENTER_LEFT);
StackPane rightPane = new StackPane();
rightPane.setPrefSize(200,700);
Button button1 = new Button("Button 2");
rightPane.getChildren().add(button1);
rightPane.setAlignment(Pos.CENTER_RIGHT);
root.getChildren().addAll(leftPane, rightPane);
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}如您所见,我有一个带有根StackPane的类,在根StackPane中还有另外两个StackPanes(leftPane和rightPane)。我想只将leftPane的背景颜色设置为黄色,但结果是整个窗口的背景都是黄色的,我做错了什么?
提前谢谢。
发布于 2020-12-07 20:36:57
如果您没有特殊的理由使用StackPane作为根目录,那么可以使用AnchorPane。你可以为你的左侧和右侧StackPanes设置3个侧边锚点,并将你的stackpanes的宽度与你的根宽度绑定在一起,所以如果你调整你的窗口大小,你的stackpanes将会适应它
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class NotesApplication extends Application {
@Override
public void start(Stage stage) {
AnchorPane root = new AnchorPane(); // Changed stackpane to anchor pane
root.setPrefSize(700,700);
StackPane leftPane = new StackPane();
leftPane.minWidthProperty().bind(root.widthProperty().divide(2)); // Binding leftPane's width with the half of your root
Button button = new Button("Button 1");
leftPane.setBackground(new Background(new BackgroundFill(Color.YELLOW, CornerRadii.EMPTY, Insets.EMPTY)));
leftPane.getChildren().add(button);
leftPane.setAlignment(Pos.CENTER_LEFT);
StackPane rightPane = new StackPane();
rightPane.minWidthProperty().bind(root.widthProperty().divide(2)); // Binding rightPane's width with the half of your root
Button button1 = new Button("Button 2");
rightPane.getChildren().add(button1);
rightPane.setAlignment(Pos.CENTER_RIGHT);
rightPane.setBackground(new Background(new BackgroundFill(Color.RED, CornerRadii.EMPTY, Insets.EMPTY)));
root.getChildren().addAll(leftPane, rightPane);
// Setting the anchors
root.setLeftAnchor(leftPane, 0.0);
root.setTopAnchor(leftPane, 0.0);
root.setBottomAnchor(leftPane, 0.0);
root.setRightAnchor(rightPane, 0.0);
root.setTopAnchor(rightPane, 0.0);
root.setBottomAnchor(rightPane, 0.0);
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}https://stackoverflow.com/questions/65162164
复制相似问题