为什么我的圆圈是黑色的,即使我没有设置它被填充
why is my circle filled Black even though i haven't set it to be filled
我目前正在做一项作业,我必须在初级阶段的中心打印一个圆圈,并在初级阶段的底部中心区域打印 4 个按钮,这些按钮可以向上、向下、向左和向右移动圆圈单击时。当我 运行 我的代码时,我的圆圈被黑色填充。我已经将圆圈的笔划设置为黑色,但我没有将圆圈设置为黑色。我知道我可以将我的圆圈设置为白色并在某种程度上解决问题,但我想知道是否有人知道为什么会这样。另外,我无法将 Circle 和按钮打印到同一个 window。我可以通过将 primaryStage 设置为场景来打印圆圈,或者通过将场景设置为 hBox 然后将 primaryStage 设置为场景来打印按钮。我应该如何最好地更改我的代码,以便同时显示按钮和圆圈?
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.geometry.Pos;
import javafx.event.EventHandler;
import javafx.event.ActionEvent;
public class Btest extends Application {
@Override // Override the start method in the Application class
public void start(Stage primaryStage) {
// Create a border pane
BorderPane pane = new BorderPane();
// create Hbox, set to bottom center
HBox hBox = new HBox();
hBox.setSpacing(10);
hBox.setAlignment(Pos.BOTTOM_CENTER);
Button btLeft = new Button("Left");
Button btDown = new Button("Down");
Button btUp = new Button("Up");
Button btRight = new Button("Right");
hBox.getChildren().addAll(btLeft, btDown, btUp, btRight);
// Lambda's
btLeft.setOnAction((e) -> {
System.out.println("Process Left");
});
btDown.setOnAction((e) -> {
System.out.println("Process Down");
});
btUp.setOnAction(e -> {
System.out.println("Process Up");
});
btRight.setOnAction((e) -> {
System.out.println("Process Right");
});
pane.setCenter(new CenteredCircle("Center"));
// Create a scene and place it in the stage
Scene scene = new Scene(pane, 300, 300);
//set stage and display
primaryStage.setTitle("ShowBorderPane"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
}
public static void main(String[] args) {
Application.launch(args);
}
}
// create custom class for circle
class CenteredCircle extends StackPane {
public CenteredCircle(String title) {
setPadding(new Insets(11.5, 12.5, 13.5, 14.5));
Circle circle = new Circle();
circle.setStroke(Color.BLACK);
circle.setCenterX(50);
circle.setCenterY(50);
circle.setRadius(50);
getChildren().add(circle);
}
}
"Why is my circle filled Black even though i haven't set it to be filled?"
因为默认颜色是黑色。请参阅 Shape.setFill()
方法的文档:
Defines parameters to fill the interior of an Shape using the settings
of the Paint context. The default value is Color.BLACK for all shapes
except Line, Polyline, and Path. The default value is null for those
shapes.
"... Also, i cannot get the Circle and the buttons to print into the same window."
将Hbox放到父BorderPane中,比如放到底部:
pane.setBottom( hBox );
我目前正在做一项作业,我必须在初级阶段的中心打印一个圆圈,并在初级阶段的底部中心区域打印 4 个按钮,这些按钮可以向上、向下、向左和向右移动圆圈单击时。当我 运行 我的代码时,我的圆圈被黑色填充。我已经将圆圈的笔划设置为黑色,但我没有将圆圈设置为黑色。我知道我可以将我的圆圈设置为白色并在某种程度上解决问题,但我想知道是否有人知道为什么会这样。另外,我无法将 Circle 和按钮打印到同一个 window。我可以通过将 primaryStage 设置为场景来打印圆圈,或者通过将场景设置为 hBox 然后将 primaryStage 设置为场景来打印按钮。我应该如何最好地更改我的代码,以便同时显示按钮和圆圈?
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.geometry.Pos;
import javafx.event.EventHandler;
import javafx.event.ActionEvent;
public class Btest extends Application {
@Override // Override the start method in the Application class
public void start(Stage primaryStage) {
// Create a border pane
BorderPane pane = new BorderPane();
// create Hbox, set to bottom center
HBox hBox = new HBox();
hBox.setSpacing(10);
hBox.setAlignment(Pos.BOTTOM_CENTER);
Button btLeft = new Button("Left");
Button btDown = new Button("Down");
Button btUp = new Button("Up");
Button btRight = new Button("Right");
hBox.getChildren().addAll(btLeft, btDown, btUp, btRight);
// Lambda's
btLeft.setOnAction((e) -> {
System.out.println("Process Left");
});
btDown.setOnAction((e) -> {
System.out.println("Process Down");
});
btUp.setOnAction(e -> {
System.out.println("Process Up");
});
btRight.setOnAction((e) -> {
System.out.println("Process Right");
});
pane.setCenter(new CenteredCircle("Center"));
// Create a scene and place it in the stage
Scene scene = new Scene(pane, 300, 300);
//set stage and display
primaryStage.setTitle("ShowBorderPane"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
}
public static void main(String[] args) {
Application.launch(args);
}
}
// create custom class for circle
class CenteredCircle extends StackPane {
public CenteredCircle(String title) {
setPadding(new Insets(11.5, 12.5, 13.5, 14.5));
Circle circle = new Circle();
circle.setStroke(Color.BLACK);
circle.setCenterX(50);
circle.setCenterY(50);
circle.setRadius(50);
getChildren().add(circle);
}
}
"Why is my circle filled Black even though i haven't set it to be filled?"
因为默认颜色是黑色。请参阅 Shape.setFill()
方法的文档:
Defines parameters to fill the interior of an Shape using the settings of the Paint context. The default value is Color.BLACK for all shapes except Line, Polyline, and Path. The default value is null for those shapes.
"... Also, i cannot get the Circle and the buttons to print into the same window."
将Hbox放到父BorderPane中,比如放到底部:
pane.setBottom( hBox );