那帮我看一下我写的这个程序,好吗?谢谢了
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import *;
public class Table{
public static void main(String args[]){
Win win=new Win();
}
}
/*class FileTable{
FileTable(){
File file=new File("table.txt");
char table[][];
try{
FileOutputStream out=new FileOutputStream(file);
out.write(table);
out.close();
}
catch(IOException e){
System.out.println(e);
}
}
}*/
//定义Win类
class Win extends JFrame implements ActionListener{
JTable table;
Object a[][];
Object name[]={"姓名","学号","专业","班级"};
JButton computerRows,computerColums;
JTextField inputRowsNumber;
int initRows=1;
JPanel pSouth,pNorth;
int count=0,rowsNumber=0;
//表格构造函数
Win(){
computerRows=new JButton("保存");//定义保存按钮,点击之后保存表格
computerColums=new JButton("退出");//定义退出按钮,点击之后退出
inputRowsNumber=new JTextField(10);
computerRows.addActionListener(this);
computerColums.addActionListener(this);
inputRowsNumber.addActionListener(this);
pSouth=new JPanel();
pNorth=new JPanel();
pNorth.add(new JLabel("输入表格的行数,回车确定"));
pNorth.add(inputRowsNumber);
pSouth.add(computerRows);
pSouth.add(computerColums);
add(pSouth,BorderLayout.SOUTH);
add(pNorth,BorderLayout.NORTH);
add(new JScrollPane(table),BorderLayout.CENTER);
setBounds(100,100,370,250);
setVisible(true);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
//实现ActionEvent虚拟方法
public void actionPerformed(ActionEvent e){
//初始化表格单元格赋空值
if(e.getSource()==inputRowsNumber){
count=0;
initRows=Integer.parseInt(inputRowsNumber.getText());
a=new Object[initRows][4];
for(int i=0;i<initRows;i++){
for(int j=0;j<4;j++)
a[i][j]="";
}
table=new JTable(a,name);
table.setRowHeight(20);
getContentPane().removeAll();
add(new JScrollPane(table),BorderLayout.CENTER);
add(pSouth,BorderLayout.SOUTH);
add(pNorth,BorderLayout.NORTH);
validate();
}
//实现保存按钮操作,鼠标点击则调用方法存入文件table.txt中
else if(e.getSource()==computerRows){
saveFile();
}
//for(int i=0;i<initRows;i++)
//for(int j=0;j<4;j++)
//char table[i][j]=a[i][j];
//}
//实现退出按钮,鼠标点击则退出
else if(e.getSource()==computerColums){
break;
}
double totalSum=0;
for(int j=0;j<rowsNumber;j++){
totalSum=totalSum+Double.parseDouble(a[j][3].toString());}
Object b[][]=new Object[rowsNumber+1][14];
for(int i=0;i<rowsNumber;i++){
for(int j=0;j<4;j++)
b[i][j]=a[i][j];
}
a=b;
table=new JTable(a,name);
getContentPane().removeAll();
add(new JScrollPane(table),BorderLayout.CENTER);
add(pSouth,BorderLayout.SOUTH);
add(pNorth,BorderLayout.NORTH);
validate();
public void saveFile(){
try{
FileOutputStream fos=new FileOutputStream("C:/data.bin");
DataOutputStream dos=new DataOutputStream(fos);
dos.writeUTF(tf.getText());
dos.flush();
dos.close();
fos.close();
}catch(Exception e){
}finally{
dos=null;
fos=null;
}
}
}
}