package txtTojtable;
import java.awt.BorderLayout;
import java.awt.Font;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class txtTojtable extends JFrame{
public static void main(String[] args) throws Exception {
	new txtTojtable();
}
public txtTojtable() throws Exception{
	this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	//表头
	Vector<String> column=new Vector<String>();
	column.addElement("红球1");
	column.addElement("红球2");
	column.addElement("红球3");
	column.addElement("红球4");
	column.addElement("红球5");
	column.addElement("红球6");
	column.addElement("蓝球");
	//声明所在行
	Vector<Vector> row = new Vector<Vector>();
	//开始读取文本文件
	FileReader fileReader = new FileReader("files\\ball.txt");
	BufferedReader br=new BufferedReader(fileReader);
	//定义每一行
    String eachRow=null;
    try {
		while((eachRow=br.readLine())!=null) {
			// split(String)根据正则表达式的匹配拆分此字符串
			String[] temp= eachRow.split(" ");
			//声明每一行必须在这里,外部的不行
			Vector<String> rowDate =new Vector<String>();
			for(int i=0;i<temp.length;i++) {//遍历每一行
				rowDate.add(temp[i]);//将每一行家入到rowDate
			}
			row.add(rowDate);//再把每一行的数据给row
		}
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
    JTable tb=new JTable(row,column);
    JScrollPane jsp=new JScrollPane(tb);
    JLabel jl=new JLabel("txt文件内容导入JTable练习");
    jl.setSize(50,50);
    jl.setFont(new Font("楷书", Font.BOLD, 24));
    this.add(jl, BorderLayout.NORTH);
    this.add(jsp, BorderLayout.CENTER);
    this.setSize(400,500);
    this.setTitle("txt文件导入JTable练习");
    this.setLocation(300,300);
    this.setVisible(true);
    }
}