“如何”将归结为“什么”。
一般来说,如果你自己动手绘制东西,你会发现将东西绘制到GridPane上更容易,而不是依赖布局管理器之类的东西。
例如。这创建了一个简单的interface,使用单个方法将事物绘制到网格上。
public interface GridShape {
public void draw(Graphics2D g2d, JComponent parent);
}
然后,这是由什么都想要画到网格
public class WaveShape implements GridShape {
@Override
public void draw(Graphics2D g2d, JComponent parent) {
g2d.setColor(Color.RED);
int xDiff = parent.getWidth()/4;
int height = parent.getHeight() - 1;
int xPos = 0;
GeneralPath path = new GeneralPath();
path.moveTo(0, 0);
path.curveTo(xPos + xDiff, 0, xPos, height, xPos + xDiff, height);
xPos += xDiff;
path.curveTo(xPos + xDiff, height, xPos, 0, xPos + xDiff, 0);
xPos += xDiff;
path.curveTo(xPos + xDiff