StackPane 设置背景
StackPane setBackground
这是我的代码:
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);
}
}
如您所见,我有一个 class,其中有一个根 StackPane 和根 StackPane 内的另外两个 StackPanes(leftPane 和 rightPane)。我只想将leftPane的背景颜色设置为黄色,但结果是整个window都是黄色背景,我做错了什么?
提前致谢。
如果您没有特别的理由将 StackPane 用作 root,您可以使用 AnchorPane。您可以为左右 StackPanes 设置 3 个侧锚并将 Stackpanes 的宽度与根宽度绑定,因此如果您调整 window 的大小,您的 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);
}
}
这是我的代码:
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);
}
}
如您所见,我有一个 class,其中有一个根 StackPane 和根 StackPane 内的另外两个 StackPanes(leftPane 和 rightPane)。我只想将leftPane的背景颜色设置为黄色,但结果是整个window都是黄色背景,我做错了什么?
提前致谢。
如果您没有特别的理由将 StackPane 用作 root,您可以使用 AnchorPane。您可以为左右 StackPanes 设置 3 个侧锚并将 Stackpanes 的宽度与根宽度绑定,因此如果您调整 window 的大小,您的 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);
}
}