package Interface; import javax.swing.*; import javax.swing.table.DefaultTableCellRenderer; import javax.swing.table.DefaultTableModel; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.Vector; public class chazhao { JTable table; DefaultTableModel model; public chazhao(){ JFrame jf = new JFrame("个人资料"); JPanel panel = new JPanel (); Vector name = createName(); Vector data = createData(); model = new DefaultTableModel(data,name); table = new JTable(model); table.setRowHeight(20); table.setPreferredScrollableViewportSize(new Dimension(1000,400)); table.getColumnModel().getColumn(6).setPreferredWidth(100); table.getColumnModel().getColumn(0).setPreferredWidth(20); table.getColumnModel().getColumn(1).setPreferredWidth(10); table.getColumnModel().getColumn(5).setPreferredWidth(20); table.getTableHeader().setReorderingAllowed(true); table.setForeground(new Color(120,9,240)); table.setShowGrid(true); JScrollPane scrollPane = new JScrollPane(table); JButton fanhui = new JButton("返回"); fanhui.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try { new Introduction().init(); } catch (IOException ioException) { ioException.printStackTrace(); } jf.dispose(); } }); panel.add(scrollPane); jf.setContentPane(panel); jf.pack(); jf.setSize(1000,600); jf.add(scrollPane,BorderLayout.CENTER); jf.add(fanhui); DefaultTableCellRenderer d = new DefaultTableCellRenderer(); d.setHorizontalAlignment(JLabel.CENTER); table.setDefaultRenderer(Object.class,d); jf.setLocationRelativeTo(null); jf.setVisible(true); } private Vector createName(){ Object[] titles = {"昵称","性别","qq","微信","生日","职业","爱好","签名","个人说明"}; Vector name= new Vector(); for(int i = 0;i<titles.length;i++){ name.add(titles[i]); } return name; } private Vector createData(){ Vector data = new Vector(); String str= null; try { FileReader f = new FileReader("src/file.txt"); BufferedReader br = new BufferedReader(f); int i = 0; while((str = br.readLine())!=null){ String[] s = str.split("\\s+"); Vector rowdata = new Vector(); for(int j = 0;j<s.length;j++){ rowdata.add(s[j]); } data.add(rowdata); i++; } f.close(); br.close(); } catch (IOException e) { e.printStackTrace(); } return data; } }
标签:总结,23.12,java,每日,Vector,new,import,jf,table From: https://www.cnblogs.com/laobing6214/p/17878147.html