1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | import java.awt.event.WindowAdapter ; import java.awt.event.WindowEvent ; import javax.swing.JTable ; import javax.swing.JScrollPane ; import javax.swing.JFrame ; public class JTableDemo01{ public static void main(String args[]){ JFrame frame = new JFrame( "Welcome To MLDN" ) ; String[] titles = { "姓名" , "年龄" , "性别" , "数学成绩" , "英语成绩" , "总分" , "是否及格" } ; Object [][] userInfo = { { "李兴华" , 30 , "男" , 89 , 97 , 186 , true } , { "李康" , 23 , "女" , 90 , 93 , 183 , false } } ; // 定义数据 JTable table = new JTable(userInfo,titles) ; // 建立表格 JScrollPane scr = new JScrollPane(table) ; frame.add(scr) ; frame.setSize( 370 , 90 ) ; frame.setVisible( true ) ; frame.addWindowListener( new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.exit( 1 ) ; } }) } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | import java.awt.BorderLayout ; import java.awt.event.WindowAdapter ; import java.awt.event.WindowEvent ; import javax.swing.JTable ; import javax.swing.JScrollPane ; import javax.swing.JFrame ; import javax.swing.JPanel ; import javax.swing.DefaultCellEditor ; import javax.swing.JComboBox ; import javax.swing.table.AbstractTableModel ; class DefaultTable extends AbstractTableModel{ private String[] titles = { "姓名" , "年龄" , "性别" , "数学成绩" , "英语成绩" , "总分" , "是否及格" } ; private Object [][] userInfo = { { "李兴华" , 30 , "男" , 89 , 97 , 186 , true } , { "李康" , 23 , "女" , 90 , 93 , 183 , false } } ; // 定义数据 public int getColumnCount(){ // 取得列的数量 return this .titles.length ; } public int getRowCount(){ // 取得行的数量 return this .userInfo.length ; } public Object getValueAt( int rowIndex, int columnIndex){ return this .userInfo[rowIndex][columnIndex] ; } public String getColumnName( int columnIndex){ return this .titles[columnIndex] ; } public Class<?> getColumnClass( int columnIndex) { // 得到指定列的类型 return this .getValueAt( 0 ,columnIndex).getClass() ; } public boolean isCellEditable( int rowIndex, int columnIndex){ // 所有内容都可以编辑 return true ; } public void setValueAt(Object aValue, int rowIndex, int columnIndex){ this .userInfo[rowIndex][columnIndex] = aValue ; } } class TableColumnModelDemo{ private JFrame frame = new JFrame( "Welcome To MLDN" ) ; private JTable table = null ; private DefaultTable defaultTable = new DefaultTable() ; // TableModel private JComboBox sexList = new JComboBox() ; public TableColumnModelDemo(){ this .table = new JTable( this .defaultTable) ; this .sexList.addItem( "男" ) ; this .sexList.addItem( "女" ) ; // 以下拉列表框的形式显示 this .table.getColumnModel().getColumn( 2 ).setCellEditor( new DefaultCellEditor( this .sexList)) ; JScrollPane scr = new JScrollPane( this .table) ; JPanel toolBar = new JPanel() ; frame.add(toolBar,BorderLayout.NORTH) ; // 加入组件 frame.add(scr,BorderLayout.CENTER) ; // 加入组件 frame.setSize( 370 , 90 ) ; frame.setVisible( true ) ; frame.addWindowListener( new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.exit( 1 ) ; } }) ; } } public class JTableDemo02{ public static void main(String args[]){ new TableColumnModelDemo() ; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | import java.awt.BorderLayout ; import java.awt.event.WindowAdapter ; import java.awt.event.ActionListener ; import java.awt.event.WindowEvent ; import java.awt.event.ActionEvent ; import javax.swing.JTable ; import javax.swing.JButton ; import javax.swing.JScrollPane ; import javax.swing.JFrame ; import javax.swing.JPanel ; import javax.swing.DefaultCellEditor ; import javax.swing.JComboBox ; import javax.swing.table.DefaultTableModel ; import javax.swing.table.TableColumnModel ; import javax.swing.table.TableColumn ; class ChangeTable implements ActionListener{ // 通过按钮实现动态表格 private JFrame frame = new JFrame( "Welcome To MLDN" ) ; private JTable table = null ; private DefaultTableModel tableModel ; // TableModel private String[] titles = { "姓名" , "年龄" , "性别" , "数学成绩" , "英语成绩" , "总分" , "是否及格" } ; private Object [][] userInfo = { { "李兴华" , 30 , "男" , 89 , 97 , 186 , true } , { "李康" , 23 , "女" , 90 , 93 , 183 , false } } ; // 定义数据 private JButton addRowBtn = new JButton( "增加行" ) ; // 定义按钮 private JButton removeRowBtn = new JButton( "删除行" ) ; // 定义按钮 private JButton addColBtn = new JButton( "增加列" ) ; // 定义按钮 private JButton removeColBtn = new JButton( "删除列" ) ; // 定义按钮 public ChangeTable(){ this .tableModel = new DefaultTableModel( this .userInfo, this .titles) ; this .table = new JTable( this .tableModel) ; JScrollPane scr = new JScrollPane( this .table) ; JPanel toolBar = new JPanel() ; toolBar.add( this .addRowBtn) ; toolBar.add( this .removeRowBtn) ; toolBar.add( this .addColBtn) ; toolBar.add( this .removeColBtn) ; frame.add(toolBar,BorderLayout.NORTH) ; // 加入组件 frame.add(scr,BorderLayout.CENTER) ; // 加入组件 frame.setSize( 370 , 160 ) ; frame.setVisible( true ) ; frame.addWindowListener( new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.exit( 1 ) ; } }) ; this .addRowBtn.addActionListener( this ) ; this .removeRowBtn.addActionListener( this ) ; this .addColBtn.addActionListener( this ) ; this .removeColBtn.addActionListener( this ) ; } public void actionPerformed(ActionEvent e){ if (e.getSource() == this .addColBtn){ // 增加列 this .tableModel.addColumn( "新增列" ) ; } if (e.getSource() == this .addRowBtn){ this .tableModel.addRow( new Object[]{}) ; } if (e.getSource()== this .removeColBtn){ int colCount = this .tableModel.getColumnCount() - 1 ; // 取得列的数量 if (colCount>= 0 ){ // 如果要想删除,则必须找到TableColumnModel TableColumnModel columMode = this .table.getColumnModel() ; TableColumn taleColumn = columMode.getColumn(colCount) ; columMode.removeColumn(taleColumn) ; // 删除指定的列 this .tableModel.setColumnCount(colCount) ; } } if (e.getSource()== this .removeRowBtn){ int rowCount = this .tableModel.getRowCount() - 1 ; if (rowCount>= 0 ){ // 判断是否还有行可以删除 this .tableModel.removeRow(rowCount) ; this .tableModel.setRowCount(rowCount) ; // 设置新的行数 } } } } public class JTableDemo03{ public static void main(String args[]){ new ChangeTable() ; } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通