请问怎么修改我的java程序,才能把list的信息显示在面板上

package sam;

import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class c extends JFrame{
JTable table;

public c(){


    JTable jtable=geTable();
    getContentPane().add(jtable);
    this.setVisible(true);


}
private JTable geTable(){
    if(table==null){
        table=new JTable();
        table.setRowHeight(23);
        String[]  columns={"姓名","性别","出生日期"};
        DefaultTableModel model=new DefaultTableModel(columns,0);
        table.setModel(model);
        List<String> students=getstudents();
    }
    return table;
}
private List<String>  getstudents(){
    List<String> list=new ArrayList<String>();
    list.add("张三丰,男,9207");
    return list;
    }
public static void main(String[] args) {
 new c();
}

}

2个回答

这里有一个简单的例子,跟你的代码类似:

 import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class SimpleTable {
    public SimpleTable() {
        JFrame f = new JFrame();
        Object[][] playerInfo = { { "阿呆", new Integer(66), new Integer(32), new Integer(98), new Boolean(false) },
                { "阿呆", new Integer(82), new Integer(69), new Integer(128), new Boolean(true) }, };
        String[] Names = { "姓名", "语文", "数学", "总分", "及格" };
        JTable table = new JTable(playerInfo, Names);
        table.setPreferredScrollableViewportSize(new Dimension(550, 30));
        JScrollPane scrollPane = new JScrollPane(table);
        f.getContentPane().add(scrollPane, BorderLayout.CENTER);
        f.setTitle("Simple Table");
        f.pack();
        f.show();

        f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }

    public static void main(String[] args) {
        SimpleTable b = new SimpleTable();
    }
}

原文链接:http://www.cnblogs.com/mailingfeng/archive/2011/12/28/2304289.html

直接对list遍历,,取出值放入table中就行了

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!