How do I make a simple solid border around a FlowP

2020-05-25 07:25发布

问题:

I'm building a simple app in javafx, and I want to be able to add a border to a FlowPane.

I have a bit more experience in java than javafx so I tried to find the equivalent of .setBorder(BorderFactory.createEmptyBorder(0,0,0,0)) but to no avail.

Unfortunately, everything I have found seems to be more complicated than I need. I don't need styles or dashes and that's all I'm finding.

Thanks !

回答1:

There's a setBorder() method, so you can add a border to your pane:

FlowPane pane = new FlowPane(10, 10);
pane.setBorder(new Border(new BorderStroke(Color.BLACK, 
            BorderStrokeStyle.SOLID, CornerRadii.EMPTY, BorderWidths.DEFAULT)));

Though this is really more simple with inline CSS:

pane.setStyle("-fx-border-color: black");

Or you could apply it with a CSS file:

FlowPane pane = new FlowPane(10, 10);
pane.getStyleClass().add("pane");

Scene scene = new Scene(pane, 300, 250);
scene.getStylesheets().add(getClass().getResource("root.css").toExternalForm());

where 'root.css' is in the same package and contains:

.pane {
    -fx-border-color: black;
}