问答题
本程序的功能是用表格的形式列出当前目录所有的文件以及属性,包括是否是文件夹、文件名称、是否可读、是否可写、文件大小以及最后修改时间,其中是否可读和是否可写采用复选框的形式表达。
import java.awt.*;
import javax.swing.*;
import java.util.Date;
import java.io. File;
import javax.swing.table.*;
public class exam_96 extends JFrame {
public exam_96() {
super("exam_96");
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[])
exam_96 ft = new exam_96();
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 Object getValueAt(int r, int c) { return data[r] [c];
public void setFileStats(______)
{
String files[] = dir.list();
data = new Object [files.length] [titles.length];
for (______) {
File tmp= new File (files[i]);
data[i] [0] = new Boolean(tmp.isDirectory());
data[i] [1] = tmp.getName();
data[i] [2] = new Boolean(tmp.canRead());
data[i] [3] = new Boolean(tmp.canWrite());
data[i] [4] = new Long(tmp.length());
data[i] [5] = new Date(tmp.lastModified());
}
fireTableDataChanged ( );
}
}
【参考答案】
第1处:File dir
第2处:int i=0; i < files.length; i++