当前页面: 开发资料首页 → J2SE 专题 → 滚动条问题```急``` 
滚动条问题```急``` 
摘要: 滚动条问题```急```  
import java.awt.*;
import java.awt.event.*;
import java.io.File;
public class Ex17_3 extends Frame{
   TextField tf=new TextField();
   Label lb=new Label("Path :");
   List ls=new List(300);
   Panel p=new Panel();
   ScrollPane sp=new ScrollPane(0);
   File f;
public Ex17_3(String title){
super(title);
lb.setBounds(10,25,50,30);
tf.setBounds(50,30,440,20);
sp.setBounds(10,60,480,400);
p.setLayout(new GridLayout(100,1));
p.add(ls);
sp.add(p);
add(sp);
add(tf);
add(lb);
  WindowListener clo=new WindowAdapter(){
   public void windowClosing(WindowEvent e){
      dispose();
   }
  };
  addWindowListener(clo);
  tf.addTextListener(new Ofield());
}
   class Ofield implements TextListener{
     public void textValueChanged(TextEvent e){
     try{
        String p_text=tf.getText();
        f=new File(p_text);
        File[] fs=f.listFiles();
        ls.setBackground(Color.YELLOW);
        for(int i=0;i
          if(fs[i].isDirectory()){
          ls.add("目录:"+fs[i]);
          }
          else{
          ls.add("文件:"+fs[i]);
          }
        }
        Thread.sleep(1000);
     }
     catch(Exception ttt){}
     }
   }
  public static void main(String[] args){
    Ex17_3 ob=new Ex17_3("简单文件浏览器");
    ob.setBounds(300,230,500,470);
    ob.setLayout(null);
    ob.setVisible(true);
  }
}
这个文件浏览器的滚动条很不好使用,不知道怎么改,请各位帮帮忙``谢谢
修改意见如下:
程序有小错误,再第二次输入时应清除第一次的.ok
import java.awt.*;
import java.awt.event.*;
import java.io.File;
public class Ex17_3 extends Frame{
   TextField tf=new TextField();
   Label lb=new Label("Path :");
   List ls=new List();
   Panel p=new Panel();
   ScrollPane sp=new ScrollPane();
   File f;
   boolean first=false;
public Ex17_3(String title){
super(title);
lb.setBounds(10,25,50,30);
tf.setBounds(50,30,440,20);
sp.setBounds(10,60,480,400);
//p.setLayout(new GridLayout(100,1));
//p.add(ls);
//sp.add(p);
sp.add(ls);
add(sp);
add(tf);
add(lb);
  WindowListener clo=new WindowAdapter(){
   public void windowClosing(WindowEvent e){
      dispose();
   }
  };
  addWindowListener(clo);
  tf.addTextListener(new Ofield());
}
   class Ofield implements TextListener{
     public void textValueChanged(TextEvent e){
     try{     
        String p_text=tf.getText();
        f=new File(p_text);
        File[] fs=f.listFiles();
        ls.setBackground(Color.YELLOW);
        if(first==true) ls.clear();//clear the listitem which is the priority dir contain;
        for(int i=0;i
          if(fs[i].isDirectory()){
          ls.add("目录:"+fs[i]);
          }
          else{
          ls.add("文件:"+fs[i]);
          }
         first=true;
        }
        Thread.sleep(1000);
     }
     catch(Exception ttt){}
     }
   }
  public static void main(String[] args){
    Ex17_3 ob=new Ex17_3("简单文件浏览器");
    ob.setBounds(300,230,500,470);
    ob.setLayout(null);
    ob.setVisible(true);
  }
}