问答题
本题程序的功能是用表格的形式列出当前目录所有的文件属性,包括是否是文件夹、文件名称、是否可读、是否可写、文件大小以及最后修改时间。程序中存在若干错误,请找出并改正(注意:不得改动程序的结构,不得增行或删行)。
import java.awt.*;
import javax.swing.*;
import java.util.Date;
import java.io.File;
import javax.swing.table.*;
public class advance extends Jframe
{
public advance()
{
super("advance");
setSize(500,400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
FileModel fm = new FileModel();
JTable jt = new JTable(fm);
jt.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
jt.setColumnSelectionAllowed(true);
jt.setDefaultRenderer(Number.class,new BigRenderer(1000));
JScrollPane jsp = new JScrollPane(jt);
getContentPane().add(jsp,BorderLayout.CENTER);
}
public static void main (String args[])
{
advance ft : new advance();
ft.setVisible(true);
}
}
class BigRenderer extends DefaultTableCellRenderer
{
double threshold;
public BigRenderer(double t)
{
threshold = t;
setHorizontalAlignment(JLabel.RIGHT);
setHorizontalTextPosition(SwingConstants.RIGHT);
}
public Component getTableCellRendererComponent(JTable table,Object value,boolean isSelected,boolean hasFocus,int row,int col)
{
return super.getTableCellRendererComponent(table,value,isSelected,hasFocus,row,col);
}
}
class FileModel extends AbstractTableModel
{
String titles[] = new String[]
{
"目录","文件名","可读?","可写?","大小","最后修改时间"
};
Class types = new Class[]
{
Boolean.class,String.class,Boolean.class,Boolean.class,
Number.class,Date.class
};
Object data[][];
public FileModel()
{
this(".");
}
public FileModel(String dir)
{
File pwd = new File(dir);
setFileStats (pwd);
}
public int getRowCount()
{
return data.length;
}
public int getColumnCount()
{
return titles.length;
}
public String getColumnName(int c)
{
return titles [c];
}
public Class getColumnClass(int c)
{
return types[c];
}
public void getValueAt(int r,int c)
{
return data[r] [c];
}
public void setFileStats(File dir)
{
String files[] = dir.list;
Data = new Object[files.length] [titles.length];
for (int i = 0; i<files.length; i++)
{
File tmp = new File(files[i]);
data[i] [0] = new Boolean(tmp.isDirectory());
data[i] [1] = tmp.getName();
data[i] [2] = new Boo lean(tmp.canRead());
data[i] [3] = new Boolean(tmp.canWrite());
data[i] [4] = new Long(tmp.length());
data[i] [5] = new Date(tmp.lastModified());
}
fireTableDataChanged ();
}
}