Java swing之JTable与TableModel

package PictureLink;
/**
 * Test测试类
 * @author lxw
 *
 */
public class Test {
    public static void main(String[] args) {
        new GameFrame();
    }
}
package PictureLink;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTable;
/**
 * GameFrame窗体类
 * @author lxw
 *
 */
public class GameFrame extends JFrame{

    public GameFrame(){
        initFrame();
    }

    public void initFrame(){
        //窗体属性
        //this.setTitle("连连看");
        this.setSize(600,500);
        this.setResizable(false);
        this.setDefaultCloseOperation(3);
        this.setLocationRelativeTo(null);
        this.setLayout(new FlowLayout());
        //进度条
        JProgressBar jpb = new JProgressBar();
        jpb.setValue(100);
        //JTabel,TableModel
        MyTabelModel mymodel = new MyTabelModel();
        JTable jtable = new JTable(mymodel);
        JScrollPane jsp = new JScrollPane(jtable);
        jsp.setPreferredSize(new Dimension(500,400));
        this.add(jpb);
        this.add(jsp);
        this.setVisible(true);
    }
}
package PictureLink;

import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
/**
 * TableModel 模型类
 * @author lxw
 *
 */
public class MyTabelModel implements TableModel{
    public MyTabelModel() {
        for(int i = 0;ifor(int j=0;j","+ j;
            }
        }
    }
    private String[] date = new String[]{"age","height","class"};
    private String [][] content = new String [12][12];
    public Class getColumnClass(int columnIndex) {
        //根据列中所有的单元格值,返回最具体的超类
        return String.class;
    }

    public int getColumnCount() {//返回模型列数
        return content[0].length;
    }

    public String getColumnName(int columnIndex) {//返回columnIndex位置的列的名称
        return null;
    }

    public int getRowCount() {//返回模型中的行数
        return content.length;
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
        //返回rowIndex 和 column 位置的单元格值
        return content[rowIndex][columnIndex];
    }

    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        //设置rowIndex 和 column 位置的单元格值

    }

    public boolean isCellEditable(int rowIndex, int columnIndex) {
        //单元格是否可编辑
        return false;
    }

    public void removeTableModelListener(TableModelListener l) {//移除监听器

    }
    public void addTableModelListener(TableModelListener l) {//添加监听器

    }

}

Java swing之JTable与TableModel_第1张图片
/**
*暂时未完成
*
**/

你可能感兴趣的:(JAVA学习笔记)