/** * 设置表格 * * * */ private JTable getTable() { if (table == null) { table = new JTable(); //定义列名数组 String columns[] = {"ID", "姓名", "性别", "出生日期", "居住地", "备注"}; //定义列宽 int columnWidth[] = {10, 20, 20, 40, 30, 20}; //创建表格数据模型 DefaultTableModel model = new DefaultTableModel(columns, 15); table.setModel(model); //获取列模型 TableColumnModel columnModel = table.getColumnModel(); //获取列数量 int columnCnt = columnModel.getColumnCount(); //便利列, 设置列宽 for (int i = 0; i < columnCnt; i++) { //获取列对象 TableColumn column = columnModel.getColumn(i); column.setPreferredWidth(columnWidth[i]); } } return table; }
时间: 2024-12-16 10:53:35