java - 有没有类似于 Windows 资源管理器的布局?

标签 java swing layout windows-explorer

我想要布局管理器,它在文件夹 View (不是任何 ListView )中像 Windows 资源管理器一样工作。我需要的是这样的 Marginal Layout panel like Windows 7 Explorer ListView在 Swing 中。 有什么方法可以自定义标准布局,或者您知道任何外部布局吗?

最佳答案

http://java-sl.com/tip_columns_flow_layout.html屏幕截图和有关以下代码的文章的链接

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;

public class ColumnsFlowLayout implements LayoutManager {
    int hGap;
    int vGap;
    public ColumnsFlowLayout() {
        this(2,2);
    }

    public ColumnsFlowLayout(int hGap, int vGap) {
        this.hGap=hGap;
        this.vGap=vGap;
    }
    public void addLayoutComponent(String name, Component comp) {
    }

    public void removeLayoutComponent(Component comp) {
    }

    public Dimension preferredLayoutSize(Container target) {
        synchronized (target.getTreeLock()) {
            Dimension dim = new Dimension(0, 0);
            int count = target.getComponentCount();
            int visibleCount = 0;
            for (int i=0; i<count; i++) {
                if (target.getComponent(i).isVisible()) {
                    visibleCount++;
                }
            }
            Insets insets = target.getInsets();

            Dimension maxPref=getCellSize(target);

            if (target.getWidth()!=0) {
                int maxWidth = target.getWidth() - (insets.left + insets.right);
                int colCount=maxWidth/(maxPref.width+hGap);
                int row=visibleCount/colCount;
                if (visibleCount % colCount!=0) {
                    row++;
                }
                dim.width += insets.left + insets.right + hGap*(visibleCount-1)+maxPref.width*visibleCount;
                dim.height += insets.top + insets.bottom + row*maxPref.height;
            }
            else {
                dim.width += insets.left + insets.right + hGap*(visibleCount-1)+maxPref.width*visibleCount;
                dim.height += insets.top + insets.bottom + maxPref.height;
            }
            return dim;
        }
    }

    private Dimension getCellSize(Container target) {
        Dimension maxPref=new Dimension();
        int count = target.getComponentCount();
        for (int i = 0 ; i < count ; i++) {
            Component m = target.getComponent(i);
            if (m.isVisible()) {
                Dimension d = m.getPreferredSize();
                maxPref.width = Math.max(maxPref.width, d.width);
                maxPref.height = Math.max(maxPref.height, d.height);
            }
        }

        return maxPref;
    }

    public Dimension minimumLayoutSize(Container target) {
        return preferredLayoutSize(target);
    }

    /**
     * Lays out the container.
     *
     * @param target the specified component being laid out
     * @see Container
     * @see       java.awt.Container#doLayout
     */
    public void layoutContainer(Container target) {
        synchronized (target.getTreeLock()) {
            Insets insets = target.getInsets();
            int maxWidth = target.getWidth() - (insets.left + insets.right);
            int count = target.getComponentCount();
            Dimension cellSize=getCellSize(target);
            int x=insets.left;
            int y=insets.top;
            int row=0;
            int colCount=maxWidth/(cellSize.width+hGap);
            int realHGap=colCount>1 ?(maxWidth-colCount*(cellSize.width+hGap))/(colCount-1) : 0;
            for (int i = 0 ; i < count ; i++) {
                Component m = target.getComponent(i);
                if (m.isVisible()) {
                    m.setSize(cellSize.width, cellSize.height);

                    m.setLocation(x, y+row*(cellSize.height+vGap));
                    x+=hGap+cellSize.width+realHGap;
                    if (x+cellSize.width>=maxWidth) {
                        row++;
                        x=insets.left;
                    }

                }
            }
        }
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("Multi column flow layout (Windows Explorer like)");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel container=new JPanel(new ColumnsFlowLayout(3,3));
        container.setBorder(new EmptyBorder(2,2,2,2));
        for (int i=0; i<27; i++) {
            container.add(new JButton("Test "+i));
        }
        frame.getContentPane().add(container);

        frame.setSize(500, 300);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

}

关于java - 有没有类似于 Windows 资源管理器的布局?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9385800/

相关文章:

java Restful Web 应用程序和 WADL

java - JPanel 内部的 JPanel 添加顶部填充

java - 从依赖包中导入 Spring 配置(*.xml)

java - 询问用户输入后无限循环

java - 图书类: How to print continually with no page separation

java - 如何在 Java Swing 应用程序中管理来自 Controller 的 View 更新

java - 如何将滚动条添加到 Vaadin 布局

html - 为什么在将内容插入 Div 容器时我的布局会中断 (HTML CSS)

java - 如何从另一个面板更改卡片布局面板?

java - 在 JGraphT 中合并图形