JAVA 添加滚动条
JAVA菜鸟写的一个图片缩略图..希望帮忙添加一个滚动条..publicclassdemotestextendsJFrame{publicdemotest(Strings...
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(".jpeg") || picture[j].toLowerCase().endsWith(".gif")
|| picture[j].toLowerCase().endsWith(".png")){
pic[k] = s + "\\" + picture[j];
flag[k] = j;
k++;
}
}
for(int i = 0;i < pic.length;i++){
ImageIcon a = new ImageIcon(pic[i]);
Image b = a.getImage();
JLabel label = new JLabel(new ImageIcon(b.getScaledInstance(250,170,Image.SCALE_DEFAULT)));
JLabel label2 = new JLabel(picture[flag[i]],JLabel.CENTER);
JPanel jpanel = new JPanel();
jpanel.setLayout(new BorderLayout());
jpanel.add(label,BorderLayout.CENTER);
jpanel.add(label2,BorderLayout.SOUTH);
JScrollPane c = new JScrollPane();
c.setViewportView(jpanel);
container.add(c);
}
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(800,660);
this.setVisible(true);
}
public static void main(String[] args){
demotest demo = new demotest("D:\\22");(自己添加路径试试程序)
}
}
这是一个类似于ACDSEE左边显示的图片缩略图,不过粗糙很多就是了,只是想在图片多的时候,显示不了那么多图片,要添加滚动条.. 加分不是问题...先谢谢大家... 展开
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(".jpeg") || picture[j].toLowerCase().endsWith(".gif")
|| picture[j].toLowerCase().endsWith(".png")){
pic[k] = s + "\\" + picture[j];
flag[k] = j;
k++;
}
}
for(int i = 0;i < pic.length;i++){
ImageIcon a = new ImageIcon(pic[i]);
Image b = a.getImage();
JLabel label = new JLabel(new ImageIcon(b.getScaledInstance(250,170,Image.SCALE_DEFAULT)));
JLabel label2 = new JLabel(picture[flag[i]],JLabel.CENTER);
JPanel jpanel = new JPanel();
jpanel.setLayout(new BorderLayout());
jpanel.add(label,BorderLayout.CENTER);
jpanel.add(label2,BorderLayout.SOUTH);
JScrollPane c = new JScrollPane();
c.setViewportView(jpanel);
container.add(c);
}
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(800,660);
this.setVisible(true);
}
public static void main(String[] args){
demotest demo = new demotest("D:\\22");(自己添加路径试试程序)
}
}
这是一个类似于ACDSEE左边显示的图片缩略图,不过粗糙很多就是了,只是想在图片多的时候,显示不了那么多图片,要添加滚动条.. 加分不是问题...先谢谢大家... 展开
2个回答
展开全部
我调试了,下面修改可以。 不过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].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(".jpeg") || picture[j].toLowerCase().endsWith(".gif")
|| picture[j].toLowerCase().endsWith(".png")){
pic[k] = s + "\\" + picture[j];
flag[k] = j;
k++;
}
}
for(int i = 0;i < pic.length;i++){
ImageIcon a = new ImageIcon(pic[i]);
Image b = a.getImage();
JLabel label = new JLabel(new ImageIcon(b.getScaledInstance(250,170,Image.SCALE_DEFAULT)));
JLabel label2 = new JLabel(picture[flag[i]],JLabel.CENTER);
JPanel jpanel = new JPanel();
jpanel.setLayout(new BorderLayout());
jpanel.add(label,BorderLayout.CENTER);
jpanel.add(label2,BorderLayout.SOUTH);
JScrollPane c = new JScrollPane();
c.setViewportView(jpanel);
pan.add(c);
}
container.add(scroll);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(800,660);
this.setVisible(true);
}
public static void main(String[] args){
demotest demo = new demotest("D:\\media\\photos");//(自己添加路径试试程序)
}
}
是一子排开。 图片数量达到上百时,机器像蜗牛一样……
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].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(".jpeg") || picture[j].toLowerCase().endsWith(".gif")
|| picture[j].toLowerCase().endsWith(".png")){
pic[k] = s + "\\" + picture[j];
flag[k] = j;
k++;
}
}
for(int i = 0;i < pic.length;i++){
ImageIcon a = new ImageIcon(pic[i]);
Image b = a.getImage();
JLabel label = new JLabel(new ImageIcon(b.getScaledInstance(250,170,Image.SCALE_DEFAULT)));
JLabel label2 = new JLabel(picture[flag[i]],JLabel.CENTER);
JPanel jpanel = new JPanel();
jpanel.setLayout(new BorderLayout());
jpanel.add(label,BorderLayout.CENTER);
jpanel.add(label2,BorderLayout.SOUTH);
JScrollPane c = new JScrollPane();
c.setViewportView(jpanel);
pan.add(c);
}
container.add(scroll);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(800,660);
this.setVisible(true);
}
public static void main(String[] args){
demotest demo = new demotest("D:\\media\\photos");//(自己添加路径试试程序)
}
}
展开全部
//这个可以实现垂直显示缩略图,不过你可以计算它的实际显示的大小,在此
//就不算了。可以直接调试通过!
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Image;
import java.io.File;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
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(".jpeg")
|| picture[j].toLowerCase().endsWith(".gif")
|| picture[j].toLowerCase().endsWith(".png")) {
pic[k] = s + "\\" + picture[j];
flag[k] = j;
k++;
}
}
for (int i = 0; i < pic.length; i++) {
ImageIcon a = new ImageIcon(pic[i]);
Image b = a.getImage();
JLabel label = new JLabel(new ImageIcon(b.getScaledInstance(250,
170, Image.SCALE_DEFAULT)));
JLabel label2 = new JLabel(picture[flag[i]], JLabel.CENTER);
JPanel jpanel = new JPanel();
jpanel.setLayout(new BorderLayout());
jpanel.add(label, BorderLayout.CENTER);
jpanel.add(label2, BorderLayout.SOUTH);
JScrollPane c = new JScrollPane();
c.setViewportView(jpanel);
container.add(c);
}
/////////////////////////////////////////////////添加的代码
JScrollPane jsp = new JScrollPane(container,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
jsp.getViewport().add(container);
jsp.setWheelScrollingEnabled(true);
setContentPane(jsp);
container.setPreferredSize(new Dimension(600,10000));///可以计算显示的大小,在此就不算了
/////////////////////////////////////////////////添加的代码
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(800, 660);
this.setVisible(true);
}
public static void main(String[] args) {
demotest demo = new demotest("D:\\22");//(自己添加路径试试程序)
}
}
//就不算了。可以直接调试通过!
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Image;
import java.io.File;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
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(".jpeg")
|| picture[j].toLowerCase().endsWith(".gif")
|| picture[j].toLowerCase().endsWith(".png")) {
pic[k] = s + "\\" + picture[j];
flag[k] = j;
k++;
}
}
for (int i = 0; i < pic.length; i++) {
ImageIcon a = new ImageIcon(pic[i]);
Image b = a.getImage();
JLabel label = new JLabel(new ImageIcon(b.getScaledInstance(250,
170, Image.SCALE_DEFAULT)));
JLabel label2 = new JLabel(picture[flag[i]], JLabel.CENTER);
JPanel jpanel = new JPanel();
jpanel.setLayout(new BorderLayout());
jpanel.add(label, BorderLayout.CENTER);
jpanel.add(label2, BorderLayout.SOUTH);
JScrollPane c = new JScrollPane();
c.setViewportView(jpanel);
container.add(c);
}
/////////////////////////////////////////////////添加的代码
JScrollPane jsp = new JScrollPane(container,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
jsp.getViewport().add(container);
jsp.setWheelScrollingEnabled(true);
setContentPane(jsp);
container.setPreferredSize(new Dimension(600,10000));///可以计算显示的大小,在此就不算了
/////////////////////////////////////////////////添加的代码
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(800, 660);
this.setVisible(true);
}
public static void main(String[] args) {
demotest demo = new demotest("D:\\22");//(自己添加路径试试程序)
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询