日期:2014-05-20  浏览次数:20694 次

Jlist加入JScrollPane的问题
我是新手,请问怎么向Jlist加入JScrollPane,为什么我加了没有反应呢
import java.awt.Color;

import javax.swing.BorderFactory;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollBar;

import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.border.Border;


public class AddressBook extends JApplet{
String[] ButtonName={"Search","Add","Amend","Delete"};
JButton[] FunctionButtons=new JButton[4];
String[] data=new String[]{"sf","fasf","sdfasfasfsa","fas","fsa","fsa","rwq","sdf","sdf"};
JList list=new JList(data);
JScrollPane scrollpane=new JScrollPane(list);

public AddressBook(){
setLayout(null);
  for(int i:range(0,3,1)){
  FunctionButtons[i]=new JButton(ButtonName[i]);
  add(FunctionButtons[i]);
  FunctionButtons[i].setSize(100,40);
  FunctionButtons[i].setLocation(370,20+40*i);
  FunctionButtons[i].setBackground(Color.pink);
  }
   
  Border brd=BorderFactory.createMatteBorder(1, 1, 2, 2, Color.BLACK);
list.setBorder(brd);
  add(list);
  list.setSize(350, 160);
  list.setLocation(20,20);

   
}
public void init(){
resize(480,200);
 
}


public int[] range(int first,int last,int step){
int[] src=new int[(last-first)/step+1];
for(int i=0;i<src.length;i++){
src[i]=first+step*i;
System.out.println(src[i]);
}
return src;

}

------解决方案--------------------
add(list); // 这里应该调用add(scrollPane),因为加入frame的是scrollPane,而不是list,list已经加在了scrollPanel里
list.setSize(350, 160);
list.setLocation(20,20);