制作表与JPanels列表

我需要在Java应用程序中使用表。 首先我习惯了JTable类的Object,但是我的表有很多function,现在我尝试使用JPanel组件列表而不是表。

如何制作包含面板列表的表格?

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

 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并尝试解决您在上一篇文章中描述的问题。