Home  >  Article  >  Java  >  Teach you how to use List to transfer student information

Teach you how to use List to transfer student information

巴扎黑
巴扎黑Original
2017-07-20 10:22:331891browse

Collections are often used in program development. For example, in business methods, student information, product information, etc. are stored in collections, and then returned to the caller as the return value of the method, thereby passing a large amount of ordered data.

This example will use the List collection to transfer student information between methods. The example effect is as follows:

Design process

1) Create a new form class ClassInfo in the project. Add JScrollPane to the form, and then place the table control JTable in the JScrollPane.

2) Write the getTable() method. In this method, create a table object and set the data model of the table, and then call the getStudents() method to obtain the collection object of student information.

While traversing the collection object, add each element to the table model rows and displayed in the table control.

 1 private JTable getTable() {  
 2       if (table == null) {  
 3           table = new JTable();// 创建表格控件   4           table.setRowHeight(23);// 设置行高度   5           String[] columns = { "姓名", "性别", "出生日期" };// 创建列名数组  
 6           // 创建表格模型   7           DefaultTableModel model = new DefaultTableModel(columns, 0);  
 8           table.setModel(model);// 设置表格模型   9           List<String> students = getStudents();// 调用方法传递list集合对象  10           for (String info : students) {// 遍历学生集合对象  11               String[] args = info.split(",");// 把学生信息拆分为数组  12               model.addRow(args);// 把学生信息添加到表格的行  13           }  
14       }  
15       return table;  
16   }

3) Write the getStudents() method, which will pass the List collection object to the caller and add multiple elements to the collection object. Each element value is a student information. , which includes name, gender, and date of birth.

 1 private List<String> getStudents(){  
 2         //创建List集合对象   3         List<String> list = new ArrayList<String>();  
 4         list.add("钱东强,男,1990-1-4");//字符串之间用英文的逗号隔开   5         list.add("孙西,女,1990-1-4");  
 6         list.add("周五燕,男,1990-1-4");  
 7         list.add("王东,女,1990-1-4");  
 8         list.add("冯晓,男,1990-1-4");  
 9         list.add("陈玉山,女,1990-1-4");  
10         list.add("蒋钦大,男,1990-1-4");  
11         return list;  
12           13     }

Full code

 1 package cn.str.opera;  
 2    3 import java.awt.BorderLayout;  
 4    5 public class ClassInfo extends JFrame {  
 6    7     private JPanel contentPane;  
 8     private JTable table;  
 9   10     /** 11      * Launch the application. 
12      */  13     public static void main(String[] args) {  
14           15            try {  
16                 UIManager  
17                         .setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");  
18             } catch (Throwable e) {  
19                 e.printStackTrace();  
20             }  
21         EventQueue.invokeLater(new Runnable() {  
22             public void run() {  
23                 try {  
24                     ClassInfo frame = new ClassInfo();  
25                     frame.setVisible(true);  
26                 } catch (Exception e) {  
27                     e.printStackTrace();  
28                 }  
29             }  
30         });  
31     }  
32   33     /** 34      * Create the frame. 
35      */  36     public ClassInfo() {  
37         getContentPane().setLayout(new BorderLayout(0, 0));  
38         setTitle("\u7528List\u96C6\u5408\u4F20\u9012\u5B66\u751F\u4FE1\u606F");  
39         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
40         setBounds(100, 100, 450, 300);  
41         contentPane = new JPanel();  
42         contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));  
43         contentPane.setLayout(new BorderLayout(0, 0));  
44         setContentPane(contentPane);  
45           46           47         JScrollPane scrollPane = new JScrollPane();  
48         contentPane.add(scrollPane,BorderLayout.CENTER);  
49         scrollPane.setViewportView(getTable());  
50           51     }  
52   53     private JTable getTable() {  
54         if(table == null){  
55             table = new JTable();//创建表格控件  56             table.setRowHeight(23);  
57             String[] columns = {"姓名","性别","出生日期"};//创建列名数组  
58             //创建表格模型  59             DefaultTableModel model = new DefaultTableModel(columns,0);  
60             table.setModel(model);//设置表格模型  61             List<String> students = getStudents();  
62             for(String info:students){// 遍历学生集合对象  63                 String[] args = info.split(",");// 按英文逗号,把学生信息拆分为数组  64                 model.addRow(args);  
65             }     
66               67         }         
68         return table;         
69     }  
70       71     private List<String> getStudents(){  
72         //创建List集合对象  73         List<String> list = new ArrayList<String>();  
74         list.add("钱东强,男,1990-1-4");//字符串之间用英文的逗号隔开  75         list.add("孙西,女,1990-1-4");  
76         list.add("周五燕,男,1990-1-4");  
77         list.add("王东,女,1990-1-4");  
78         list.add("冯晓,男,1990-1-4");  
79         list.add("陈玉山,女,1990-1-4");  
80         list.add("蒋钦大,男,1990-1-4");  
81         return list;  
82           83     }     
84   85 }

The above is the detailed content of Teach you how to use List to transfer student information. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn