0

現在、プライマリ ステージの中央に円を印刷し、プライマリ ステージの下部中央領域に 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); 
 }
}
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 に答える