importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjavax.swing.JButton;importjavax.swing.JFrame;importjavax.swing.JScrollPane;importjavax.swing.JT...
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class Test{
public Test(){
JFrame f = new JFrame("表格测试");
Object data[][] = new Object[3][5];
String name[] = {"A","B","C","D","E"};
DefaultTableModel md = new DefaultTableModel(data,name);
final JTable tb = new JTable(md);
JButton b = new JButton("测试");
f.add(new JScrollPane(tb),"Center");
f.add(b,"South");
f.setVisible(true);
f.pack();
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
for(int i=0;i
int sum = 0;
for(int j=0;j
sum+=(Integer)tb.getValueAt(i, j);
}
System.out.println(sum);
}
}
});
}
public static void main(String [] agrs){
new Test();
}
}
一个测试代码如上,自己录入值后点测试,出错如下:
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
at test.Test$1.actionPerformed(Test.java:33)
说String cannot be cast to java.lang.Integer,错在sum+=(Integer)tb.getValueAt(i, j);
应该是表数据类型的问题,怎么设置表的数据类型啊,让输入进去的东西不为String型
展开