展开全部
按照你的要求编写的Java程序如下:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class B extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
DefaultTableModel tableModel=new DefaultTableModel();
JButton jb=new JButton("导入数据");
JPanel jp=new JPanel();
JTable table=new JTable(tableModel);
JScrollPane tableScrollPane=new JScrollPane(table);
BufferedReader br=null;
B(){
super("YY");
tableModel.addColumn("姓名");
tableModel.addColumn("年龄");
tableModel.addColumn("性别");
tableModel.addColumn("薪资");
jp.setLayout(null);
jb.addActionListener(this);
jb.setBounds(10,10,90,20);
tableScrollPane.setBounds(0,40,400,360);
jp.add(jb);
jp.add(tableScrollPane);
this.add(jp);
this.setSize(400,400);
this.setResizable(false);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==jb){
try {
br=new BufferedReader(new InputStreamReader(new FileInputStream("数据.txt")));
String str=null;
while((str=br.readLine())!=null){
String []s=str.split("----");
tableModel.addRow(s);
}
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
public static void main(String[] args) {
new B();
}
}
运行结果:
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询