给你发个代码你参考下:
package Test2;
import java.util.Vector;
import javax.swing.AbstractListModel;
import javax.swing.JFrame;
import javax.swing.JList;
public class TestJPanel extends JFrame {
public TestJPanel(){
super("名称为空啊");
this.setSize(300, 400);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String[][] data = {{"one", "two", "three", "four"},{"one", "two", "three", "four"}};
JList jl = new JList(){ //自定义JList类,覆盖setListData方法
@Override
public void setListData(Object[] listData) {
// TODO Auto-generated method stub
if(listData!=null){
final Object[] objList = new Object[listData.length];
for(int i=0 ;i listData) {
// TODO Auto-generated method stub
super.setListData(listData);
}
};
jl.setListData(data);
this.getContentPane().add(jl);
this.setVisible(true);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new TestJPanel();
}
}