import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
class Win extends JFrame implements ActionListener
{
JTable jtb;
JTextField jtf;
JButton addc;
JButton count;
Object name[]={"姓名","英语","数学","总成绩"};
Object a[][];
int row=1;
JPanel p;
Win()
{
jtf=new JTextField(10);
addc=new JButton("增加");
count=new JButton("总成绩");
addc.addActionListener(this);
count.addActionListener(this);
a=new Object[row][4];
jtb=new JTable(a,name);
p=new JPanel();
p.setSize(400,160);
p.add(new JLabel("输入表格的行数:"));
p.add(jtf);
p.add(addc);
p.add(count);
add(p,BorderLayout.SOUTH);
add(new JScrollPane(jtb),BorderLayout.CENTER);
setSize(550,220);
setVisible(true);
validate();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==addc)
{
row=Integer.parseInt(jtf.getText());
a=new Object[row][4];
jtb=new JTable(a,name);
getContentPane().removeAll();
add(new JScrollPane(jtb),BorderLayout.CENTER);
add(p,BorderLayout.SOUTH);
validate();
}
else if(e.getSource()==count)
{
System.out.println("您点了汇总按钮");
for(int i=0;i<a.length;i++)
{
//System.out.println("您点了汇总按钮"+i);
double sum=0;
boolean boo=true;
for(int j=1;j<a[i].length-1;j++)
{
try
{
sum=sum+Double.parseDouble(a[i][j].toString());//问题在这
}
catch(Exception ex)
{
boo=false;
repaint();
}
}
if(boo==true)
{
//System.out.println("您点了汇总按钮2");
a[i][3]=sum;
jtb.repaint();
}
}
}
}
}
public class Exam815
{
public static void main(String[] arg)
{
new Win();
}
}
在标签上输入成绩的时候,输入好后,一定把光标移开....我检测了错误
try
{
sum=sum+Double.parseDouble(a[i][j].toString());
}
catch(Exception ex)
{
boo=false;
repaint();
}
这里有错误的话就没有结果了....
[[it] 本帖最后由 sunkaidong 于 2008-3-7 09:07 编辑 [/it]]