简体   繁体   中英

Why won't anything show up in my JavaFX application?

I am currently trying to make an 8x8 board and cannot seem to figure out why my rectangle objects are not showing. I am trying to figure out the logic behind one row before I do it multiple times to get the whole board. Below is my current code:

import javafx.application.*;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.layout.TilePane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class Board extends Application {

    public static void main(String[] args) {
        launch();
    }

    public void start(Stage ps) {

        TilePane tp = new TilePane();
        Pane p = new Pane();

        for (int column = 0; column > 8; column++) {
            // This loop is used to determine the center of the rectangle
            for (int x = 10; x < 160; x += 20) {        
                Rectangle r = new Rectangle();
                r.setWidth(20);
                r.setHeight(20);
                r.setX(x);
                r.setY(10);
                if (column == 0 || column % 2 == 0) {
                    r.setFill(Color.BLACK);
                }
                else {
                    r.setFill(Color.GREY);
                }
                tp.getChildren().add(r);
            }
        }

        p.getChildren().add(tp);
        Scene s = new Scene(p, 160, 160);

        ps.setScene(s);
        ps.setTitle("PP2 Halma Project");
        ps.show();
    }
}
  1. for (int column = 0; column > 8; column++) - This will never happen because 0 can never be more than 8.
  2. r.setX(x) - I don't think you would need this. You should let TilePane layout the rectangles for you; you just need to define a size for it.
  3. Pane p = new Pane() - Personally, I think this is redundant. Using just TilePane without it will work just fine. This will not cause your program to bug, though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM