这是一个网格的例子,它使用简单的绑定自动调整大小。
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Line;
public class Grid {
private final Pane view = new Pane();
private final int numColumns ;
private final int numRows ;
// arbitrary defaults of 20:
private final DoubleProperty prefColumnWidth = new SimpleDoubleProperty(20);
private final DoubleProperty prefRowHeight = new SimpleDoubleProperty(20);
public Grid(int numColumns, int numRows) {
this.numColumns = numColumns ;
this.numRows = numRows ;
for (int x = 0 ; x <= numColumns ; x++) {
Line line = new Line();
line.startXProperty().bind(view.widthProperty().multiply(x).divide(numColumns));
line.endXProperty().bind(line.startXProperty());
line.setStartY(0);
line.endYProperty().bind(view.heightProperty());
view.getChildren().add(line);
}
for (int y = 0 ; y <= numRows ; y++) {
Line line = new Line();
line.startYProperty().bind(view.heightProperty().multiply(y).divide(numRows));
line.endYProperty().bind(line.startYProperty());
line.setStartX(0);
line.endXProperty().bind(view.widthProperty());
view.getChildren().add(line);
}
view.prefWidthProperty().bind(prefColumnWidth.multiply(numColumns));
view.prefHeightProperty().bind(prefRowHeight.multiply(numRows));
}
public final DoubleProperty prefColumnWidthProperty() {
return this.prefColumnWidth;
}
public final double getPrefColumnWidth() {
return this.prefColumnWidthProperty().get();
}
public final void setPrefColumnWidth(final double prefColumnWidth) {
this.prefColumnWidthProperty().set(prefColumnWidth);
}
public final DoubleProperty prefRowHeightProperty() {
return this.prefRowHeight;
}
public final double getPrefRowHeight() {
return this.prefRowHeightProperty().get();
}
public final void setPrefRowHeight(final double prefRowHeight) {
this.prefRowHeightProperty().set(prefRowHeight);
}
public Pane getView() {
return view;
}
public int getNumColumns() {
return numColumns;
}
public int getNumRows() {
return numRows;
}
}
这里有一个简单的测试:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class GridTest extends Application {
@Override
public void start(Stage primaryStage) {
Grid grid = new Grid(10,10);
Scene scene = new Scene(grid.getView());
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
有一堆,你可以使用此不同的方法。需要注意的是上面的方法并不真正需要的子类在所有的,所以你可以只写创建窗格的方法:
private Pane createGrid(int numColumns, int numRows) {
Pane view = new Pane();
for (int x = 0 ; x <= numColumns ; x++) {
Line line = new Line();
line.startXProperty().bind(view.widthProperty().multiply(x).divide(numColumns));
line.endXProperty().bind(line.startXProperty());
line.setStartY(0);
line.endYProperty().bind(view.heightProperty());
view.getChildren().add(line);
}
for (int y = 0 ; y <= numRows ; y++) {
Line line = new Line();
line.startYProperty().bind(view.heightProperty().multiply(y).divide(numRows));
line.endYProperty().bind(line.startYProperty());
line.setStartX(0);
line.endXProperty().bind(view.widthProperty());
view.getChildren().add(line);
}
view.setPrefSize(20*numColumns, 20*numRows);
return view ;
}
或者,如果你想要的东西更接近做事的AWT /春路,你可以继承Region
,使用Canvas
,并覆盖Region.layoutChildren()
。 layoutChildren()
方法被称为布局过程的一部分(如果区域更改大小,将会触发该方法)。在这其中我添加填充支持:
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.layout.Region;
public class Grid extends Region {
private Canvas canvas ;
private final int numColumns ;
private final int numRows ;
public Grid(int numColumns, int numRows) {
this.numColumns = numColumns ;
this.numRows = numRows ;
canvas = new Canvas();
getChildren().add(canvas);
}
@Override
protected void layoutChildren() {
double w = getWidth() - getPadding().getLeft() - getPadding().getRight() ;
double h = getHeight() - getPadding().getTop() - getPadding().getBottom() ;
canvas.setWidth(w+1);
canvas.setHeight(h+1);
canvas.setLayoutX(getPadding().getLeft());
canvas.setLayoutY(getPadding().getRight());
GraphicsContext gc = canvas.getGraphicsContext2D();
gc.clearRect(0, 0, w, h);
for (int i = 0 ; i <= numColumns ; i++) {
// adding 0.5 here centers the line in the physical pixel,
// making it appear crisper:
double x = w*i/numColumns + 0.5;
gc.strokeLine(x, 0, x, h);
}
for (int j = 0 ; j <= numRows ; j++) {
double y = h*j/numRows + 0.5 ;
gc.strokeLine(0, y, w, y);
}
}
@Override
protected double computePrefWidth(double height) {
return 20 * numColumns;
}
@Override
protected double computePrefHeight(double width) {
return 20 * numRows ;
}
}
这是一个测试此:
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class GridTest extends Application {
@Override
public void start(Stage primaryStage) {
Grid grid = new Grid(10,10);
grid.setPadding(new Insets(20));
Scene scene = new Scene(grid, 400, 400);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
我猜你必须使用JavaFx画布。 – Sedrick
JavaFX中的图形原语通过Prism运行,Prism在很多情况下都会遵循本地图形工具包。因此没有'paintComponent'的直接等价物(因为没有通用的Graphics对象来绘制)。您可以按照@SedrickJefferson的建议,使用['Canvas'](http://docs.oracle.com/javase/8/javafx/api/javafx/scene/canvas/Canvas.html),或者可能使用子类'根据需要使用“Shape”和“Text”元素作为子节点,并覆盖'layoutChildren()'(以及确定维度的方法)。 –
如果您可以更具体地了解您正在尝试做什么,那么您可能会收到更多有用的建议。 –