有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java FormLayout在一个单元格中显示所有内容

我试图使用^{}创建一个网格,但它似乎在其他元素之上显示每个新元素。我想问题在于CellContraintsFormLayout。注意CityPanel扩展了Panel

public class MapPanel extends JPanel {

    public MapPanel() {
        drawMap(5, 5);
    }

    private void drawMap(columns, rows) {
        ColumnSpec[] columnSpec = new ColumnSpec[columns];
        for(int i = 0; i < columns; i++)
            columnSpec[i] = FormSpecs.DEFAULT_COLSPEC;

        RowSpec[] rowSpec = new RowSpec[rows];
        for(int i = 0; i < rows; i++)
            rowSpec[i] = FormSpecs.DEFAULT_ROWSPEC;

        FormLayout layout = new FormLayout(columnSpec, rowSpec);
        this.setLayout(layout);
        CellConstraints cc = new CellConstraints(columns, rows);

        //draw the grid

        for (City city : gameMap.getCities().values()) {
            Dimension dimension = new Dimension(100, 100);
            CityPanel cityPanel = new CityPanel(city, dimension);

            //this code put the cityPanel in a cell. It's confirmed by the println()
            this.add(cityPanel, cc.xy(mapGrid.getColumn(city), mapGrid.getRow(city), CellConstraints.CENTER, CellConstraints.CENTER));
            System.out.println(city.getName() + " -> (" + mapGrid.getColumn(city) + "," + mapGrid.getRow(city) + ")" );
        }
    }
}

共 (0) 个答案