abstractTableModel必须要用滚动条么?
代码如下:---------------------------------------------------------------------
import java.awt.BorderLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
class Table extends AbstractTableModel {
public String[] titles = { "姓名", "年龄" };
public Object[][] userInfo = { { "李", 10 }, { "", 3 } };
public int getColumnCount() {
return this.titles.length;
}
public int getRowCount() {
return this.userInfo.length;
}
public Object getValueAt(int rowIndex, int columnIndex) {
return this.userInfo[rowIndex][columnIndex];
}
public String getColumnName(int column) {
return this.titles[column];
}
}
public class Test20 {
public static JFrame frame = new JFrame("Welcome");
public static JTable table = null;
public static void main(String[] args) {
table = new JTable(new Table());
//JScrollPane scr = new JScrollPane(table);
//frame.add(scr, BorderLayout.CENTER);
frame.add(table, BorderLayout.CENTER); //为什么直接加入table显示不出列名,而改成上面被注释的代码,用滚动条才能显示列名?
frame.setSize(300, 100);
frame.setLocation(370, 160);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(1);
}
});
}
}