0

我目前正在处理一项任务,我必须在初级阶段的中心打印一个圆圈,并在初级阶段的底部中心区域打印 4 个按钮,这些按钮在单击时向上、向下、向左和向右移动。当我运行我的代码时,我的圆圈被黑色填充。我已将圆圈的笔划设置为黑色,但我没有将圆圈设置为黑色。我知道我可以将我的圈子设置为白色并在某种程度上解决问题,但我想知道是否有人知道为什么会这样。此外,我无法将 Circle 和按钮打印到同一个窗口中。我可以通过将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); 
 }
}
4

1 回答 1

0
"Why is my circle filled Black even though i haven't set it to be filled?"

因为默认颜色是黑色。请参阅方法文档Shape.setFill()

使用 Paint 上下文的设置定义参数以填充 Shape 的内部。除 Line、Polyline 和 Path 之外的所有形状的默认值为Color.BLACK 。这些形状的默认值为 null。

"... Also, i cannot get the Circle and the buttons to print into the same window."

将 Hbox 放到父 BorderPane 中,例如放到底部:

pane.setBottom( hBox );
于 2015-11-28T08:23:24.427 回答