十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
程序如下:
创新互联主营建德网站建设的网络公司,主营网站建设方案,重庆APP软件开发,建德h5小程序开发搭建,建德网站营销推广欢迎建德等地区企业咨询
import java.awt.GridLayout;
import java.awt.Toolkit;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class JscrollDemo extends JFrame
{
private JScrollPane btnPanel;
private JPanel panel;
private JButton button;
int btnNum = 10; //按钮数,可以是任意数
int closNum = 4;//每行按钮数,可自定义
public JscrollDemo()
{
panel = new JPanel();
setTitle("滚动条测试");
setBounds((Toolkit.getDefaultToolkit().getScreenSize().width - 500)/2,
(Toolkit.getDefaultToolkit().getScreenSize().height - 500)/2, 500, 400);
panel.setLayout(new GridLayout(0,closNum));
for(int i=0;ibtnNum;i++)
{
button = new JButton("按钮" + (i+1));
button.setSize(100, 80);
panel.add(button);
}
if((btnNum*1.0/closNum) 3)
{
btnPanel = new JScrollPane(panel,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
}
else
{
btnPanel = new JScrollPane(panel,
JScrollPane.VERTICAL_SCROLLBAR_NEVER,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
}
add(btnPanel);
setVisible(true);
setResizable(false);
}
public static void main(String[] args)
{
new JscrollDemo();
}
}
有问题欢迎提问,满意请采纳,谢谢!
我调试了,下面修改可以。 不过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");//(自己添加路径试试程序)
}
}
其实JScrollPane滚动条是自动的,当你的内容大于容器大小时滚动条就会出现。
如果你非要滚动条显示,就加上这两句:
jScrollpane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
jScrollpane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);