如果您需要创建一个由包含JTextArea的JPanels组成的表,请从以下内容开始:
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中描述的问题.