下面程序的目的是在屏幕上显示当前目录下的文件信息。文件信息通过表格JTable的实例显示。请改正程序中的错误(有下划线的语句),使程序能输出正确的结果。 注意:不改动程序的结构,不得增行或删行。 import java.awt.*; import javax.swing.*; import java.util. Date; import javax.swing.table.*; import java.applet.*; import java.io.*; public class Example3_10 extends JApplet, JFrame public void init()
FileModel fm = new FileModel(); JTable jt = new JTable(); jt.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); jt.setColumnSelectionAllowed(true); JScrollPane jsp = new JScrollPane(jt); getContentPane().add(jsp, BorderLayout.CENTER);
public static void main(String args[])
Example3_10 ft = new Example3_10(); ft.init(); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT ON CLOSE); f.getContentPane() .add(ft.getContentPane()); f.setSize (300, 400); f.show();
class FileModel extends AbstractTableModel String[] columnName = new String[] "文件名", "大小", "最后修改时间"
Object[] [] data; public FileModel() this("."); public FileModel(String dir)
File file = new File(dir); String files[] = file.list(); data = new Object [files.length] [columnName.length]; for (int i=0; i < files.length; i++)
File tmp = new File (files[i]); data[i] [0] = tmp.getName(); data[i] [1] = new Long(tmp.length()); data[i] [2] = new Date(tmp.lastModified() );
public int getColumnNumber() return columnName.length; public int getRowCount() return data. length; public String getColumnName(int col) return columnName[col]; public Object getValueAt(int row, int col) return data[row] [col]; public Class getColumnClass(int c) return getValueAt (0, c) .getClass();