且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

制作表与JPanels列表

更新时间:2023-12-05 23:24:16

如果您需要创建一个由 JPanel 组成的表格 JTextArea ,从以下内容开始:

If you need to create a table composed of JPanels containing JTextArea , start with something like:

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 并尝试解决之前中描述的问题发布

However, the recommended way is to use a JTable and attempt to solve the issues you described in a previous post.