📄 tableofmail.java~42~
字号:
package mailofjava;/*以表格形式显示mail*//* 曾海 2003-7*//*使用mvc 模式的jtable和jdatamodel*/import java.awt.*;import java.awt.event.*;import javax.swing.JFrame;import javax.swing.table.*;import javax.swing.*;import javax.swing.event.*;public class TableOfMail extends JFrame { JPanel jp1; JButton jb1; JTable jtable; JScrollPane jsp; public TableOfMail() { this.getContentPane().setLayout(new FlowLayout()); jp1= new JPanel(); jtable=new JTable(new MyModel()); jtable.setForeground(java.awt.Color.blue); jsp=new JScrollPane(); // jsp.getViewport().add(jtable); jsp.add(jtable); jsp.setSize(new Dimension(400,300)); this.getContentPane() .add(jsp); } public static void main(String[] args) { TableOfMail tableOfMail = new TableOfMail(); tableOfMail.setSize(500,400); tableOfMail.setVisible(true) ; tableOfMail.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e){ System.exit(0) ;} } ); }}class MyModel extends DefaultTableModel { // AbstractTableModel takes care of // TableModelListener list management,不用AbstractTableModel了 String[][] tableContents={ { "Hello ZengHai!","2003-09-27","10544", "bytes","text/plain"}, { "Hello ZengHai!","2003-09-27","10544", "bytes","text/plain"}, { "Hello ZengHai!","2003-09-27","10544", "bytes","text/plain"}, { "Hello ZengHai!","2003-09-27","10544", "bytes","text/plain"}, { "Hello ZengHai!","2003-09-27","10544", "bytes","text/plain"}, { "Hello ZengHai!","2003-09-27","10544", "bytes","text/plain"}, { "Hello ZengHai!","2003-09-27","10544", "bytes","text/plain"}, { "Hello ZengHai!","2003-09-27","10544", "bytes","text/plain"}, { "Hello ZengHai!","2003-09-27","10544", "bytes","text/plain"}, { "Hello ZengHai!","2003-09-27","10544", "bytes","text/plain"}, { "Hello ZengHai!","2003-09-27","10544", "bytes","text/plain"}} ; String colname[]={"Subject ","date","bytes","label","type"};private int numColumns = colname.length;private int numRows = tableContents.length;public int getColumnCount() {//超越基类的方法 return numColumns;}public int getRowCount() { return numRows;}public Object getValueAt (int row, int column) {//取得值,好象是容器回调的 return tableContents[row][column];}public String getColumnName (int columnIndex) { return colname[columnIndex];}public void setValueAt (Object aValue, //设定值 int row, int column) { String cellValue; if (aValue instanceof String) cellValue = (String)aValue; else cellValue = aValue.toString(); tableContents[row][column] = cellValue; fireTableCellUpdated (row, column);//通知listenter}public boolean isCellEditable(int row, int column) {//所有值只读 return false;}/*getColumnpublic TableColumn getColumn(Object identifier)Returns the TableColumn object for the column in the table whose identifier is equal to identifier,when compared using equals.Parameters:identifier - the identifier objectReturns:the TableColumn object that matches the identifierThrows:IllegalArgumentException - if identifier is null or no TableColumn has this identifier*/ public MyModel(){ }}///default
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -