我目前正在进行一项任务,我必须在主舞台的中央打印一个圆圈,在主舞台的底部中央区域打印4个按钮,在点击时向上,向下,向左和向右移动圆圈。当我运行我的代码时,我的圆圈用黑色填充。我已将圆的笔划设置为黑色,但我没有将圆圈设置为黑色。我知道我可以设置我的圆圈填充白色并稍微解决问题,但我想知道是否有人知道为什么会发生这种情况。此外,我无法将圆形和按钮打印到同一窗口中。我可以通过将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);
}
}
答案 0 :(得分:0)
789-0000002
因为默认颜色是黑色。请参阅"Why is my circle filled Black even though i haven't set it to be filled?"
方法的文档:
使用设置定义用于填充Shape内部的参数 Paint上下文。所有形状的默认值均为 Color.BLACK 除了线,折线和路径。对于那些人,默认值为null 形状。
Shape.setFill()
将Hbox放入父BorderPane,例如放入底部:
"... Also, i cannot get the Circle and the buttons to print into the same window."