JAVA 添加滚动条

来源:百度知道 编辑:UC知道 时间:2024/07/02 14:43:26
JAVA菜鸟写的一个图片缩略图..希望帮忙添加一个滚动条..

public class demotest extends JFrame{
public demotest(String s){
Container container = getContentPane();
container.setLayout(new FlowLayout(FlowLayout.LEFT));
// container.add(new JScrollPane());

File file = new File(s);
String[] picture = file.list();

int j,k;
int[] flag = new int[picture.length];

for(k = 0,j = 0;j < picture.length;j++) {
if(picture[j].toLowerCase().endsWith(".jpg") || picture[j].toLowerCase().endsWith(".jpeg") || picture[j].toLowerCase().endsWith(".gif")
|| picture[j].toLowerCase().endsWith(".png")){

k++;
//System.out.println(picture[j]);
}
}
String[] pic = new String[k];
for(k = 0,j = 0;j < picture.length;j++) {
if(picture[j].toLowerCase().endsWith(".jpg") || picture[j].toLowerCase().endsWith(&

我调试了,下面修改可以。 不过FlowLayout布局效果不好,再多的图片,它也
是一子排开。 图片数量达到上百时,机器像蜗牛一样……
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;

public class demotest extends JFrame{
JScrollPane scroll;
JPanel pan; //用此pan来添加图片,然后把它放入主窗口。
public demotest(String s){
pan = new JPanel();
pan.setLayout(new FlowLayout());
scroll = new JScrollPane(pan);
scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);//设置何时显示滚动条
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); //设置何时显示滚动条
Container container = getContentPane();
container.add(scroll);

File file = new File(s);
String[] picture = file.list();

int j,k;
int[] flag = new int[picture.length];

for(k = 0,j = 0;j < picture.length;j++) {
if(picture[j].toLowerCase().endsWith(".jpg") || picture[j].toLowerCas