使用 JPanel 列表制作 Table

Making Table With List Of JPanels

我需要 table 在 Java 应用程序中。首先,我使用 class JTable 的对象,但我的 table 有很多功能,现在我尝试使用 JPanel 组件列表而不是 table.

如何使用面板列表制作 table?

如果您需要创建一个由包含 JTextAreaJPanel 组成的 table,请从以下内容开始:

JPanel table = new JPanel();
table.setLayout(new BoxLayout(table, BoxLayout.X_AXIS));
for (int rowIndex = 0; rowIndex < numberOfRows; rowIndex++) {
    table.add(getRow(numberOfColumns));
} 

其中 getRow

定义
private Component getRow(int numberOfColumns) {

    JPanel row = new JPanel();
    //use GridLayout if you want equally spaced columns 
    row.setLayout(new BoxLayout(row, BoxLayout.Y_AXIS));
    for (int colIndex = 0; colIndex < numberOfColumns; colIndex++) {
        row.add(getCell());
    }
    return row;
}

getCell

private Component getCell() {
    JTextArea ta = new JTextArea("Add text");
    ta.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    return ta;
}

但是,推荐的方法是使用 JTable 并尝试解决您在之前 post 中描述的问题。