Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 是否有类似Windows资源管理器的布局?_Java_Swing_Layout_Windows Explorer - Fatal编程技术网

Java 是否有类似Windows资源管理器的布局?

Java 是否有类似Windows资源管理器的布局?,java,swing,layout,windows-explorer,Java,Swing,Layout,Windows Explorer,我想有一个布局管理器,它在文件夹视图(而不是任何列表视图)中的工作方式类似于Windows资源管理器。我需要的是这样的秋千。 有没有办法自定义标准布局或了解任何外部布局?下面的屏幕截图链接和关于代码的文章 import javax.swing.*; import javax.swing.border.EmptyBorder; import java.awt.*; public class ColumnsFlowLayout implements LayoutManager { int

我想有一个布局管理器,它在文件夹视图(而不是任何列表视图)中的工作方式类似于Windows资源管理器。我需要的是这样的秋千。 有没有办法自定义标准布局或了解任何外部布局?

下面的屏幕截图链接和关于代码的文章

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);
    }

}
import javax.swing.*;
导入javax.swing.border.EmptyBorder;
导入java.awt.*;
公共类列FlowLayout实现LayoutManager{
int-hGap;
int-vGap;
公共专栏布局(){
这(2,2);
}
公共专栏布局(int hGap、int vGap){
这个.hGap=hGap;
这个.vGap=vGap;
}
public void addLayoutComponent(字符串名称,组件组成){
}
公共void removeLayoutComponent(组件组件组件){
}
公共维度preferredLayoutSize(容器目标){
已同步(target.getTreeLock()){
尺寸标注=新尺寸标注(0,0);
int count=target.getComponentCount();
int visibleCount=0;
对于(inti=0;i1?(maxWidth colCount*(cellSize.width+hGap))/(colCount-1):0;
for(int i=0;i=maxWidth){
行++;
x=插图左;
}
}
}
}
}
公共静态void main(字符串[]args){
JFrame=newjframe(“多列流布局(类似Windows资源管理器)”;
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel容器=新JPanel(新列布局(3,3));
集装箱订单(新的空订单(2,2,2,2));

对于(int i=0;iyes是可能的,也许类似代码的作者就在附近