这个程序是实现一个表格的.但是有错误.好像是接口类事件有问题.在eclipse中能够显示出来.
麻烦大家看看.以前留下来的问题到现在都没解决的.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
public class TableDemo extends JFrame {
TableModel model;
JTable table;
String[] header = {"学号","姓名","性别","出身日期","地址"};
Object data[][]={
{new Integer(1),"张军","男","1989-9-9","北京"},
{new Integer(2),"李红","女","1987-10-12","大连"},
{new Integer(3),"张大智","男","1988-12-9","北京"},
{new Integer(4),"杨立军","女","1987-8-2","天津"},
{new Integer(5),"张小惠","女","1988-10-2","山东"},
{new Integer(6),"韩宏志","男","1987-9-10","大连"}
};
public TableDemo(){
table = new JTable(data,header);
table.setColumnSelectionAllowed(true);
table.setGridColor(Color.blue);
table.setRowHeight(20);
ListSelectionModel rowSM = table.getSelectionModel();
ListSelectionModel colSm = table.getColumnModel().getSelectionModel();
rowSM.addListSelectionListener(new ListSelectionListener(){
public void valueChanged(ListSelectionEvent e){
if(e.getValueIsAdjusting()) return;
ListSelectionModel lsm =(ListSelectionModel)e.getSource();
int selectCol = lsm.getMinSelectionIndex();
System.out.println("列"+selectCol);
}
});
model = table.getModel();
model.addTableModelListener(new TableModelListener(){
public void tableChanged(TableModelEvnet e){
int row = e.getFirstRow();
int column = e.getColumn();
Object data = model.getValueAt(row, column);
try{
System.out.println(data);
table.requestDefaultFocus();
}catch(Exception ee){ }
}
});
JScrollPane jsp = new JScrollPane(table);
getContentPane().add(jsp);
setTitle("表格演示");
setSize(300,300);
setVisible(true);
}
public static void main(String args[]){
new TableDemo();
}
}