📄 newlayout.java
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package com.deskManager.window.contr;import java.awt.Component;import java.awt.Container;import java.awt.Dimension;import java.awt.Insets;import java.awt.LayoutManager;/** * * @author Administrator */public class newLayout implements LayoutManager, java.io.Serializable { /** * */ private static final long serialVersionUID = 1L; int hgap; int vgap; public newLayout() { this(0, 0); } public newLayout(int hgap, int vgap) { this.hgap = hgap; this.vgap = vgap; } @Override public void addLayoutComponent(String name, Component comp) { throw new UnsupportedOperationException("Not supported yet."); } @Override public void removeLayoutComponent(Component comp) { throw new UnsupportedOperationException("Not supported yet."); } @Override public Dimension preferredLayoutSize(Container parent) { // 容器四条边框的尺寸 Insets s = parent.getInsets(); Dimension p = parent.getSize(); Dimension dp = new Dimension(s.left + s.right + p.width, s.bottom + s.top); Component c; int height = 0; int width = 0; int n = parent.getComponentCount(); if (n != 0) { // 计算各个组件的最大宽度和最大高度 for (int i = 0; i < n; i++) { c = parent.getComponent(i); int temph = c.getHeight(), tempw = c.getWidth(); if (temph == 0) { temph = c.getPreferredSize().height; } if (tempw == 0) { tempw = c.getPreferredSize().width; } if (height < temph) { height = temph; } if (width < tempw) { width = tempw; } } int ncols = p.width / (width + vgap); if (ncols == 0) { ncols = 1; } int nrows = 0; if(n % ncols!=0){ nrows= n / ncols + 1; }else { nrows= n / ncols; } dp.height += nrows * (height + hgap); return dp; } else { return p; } } @Override public Dimension minimumLayoutSize(Container parent) { throw new UnsupportedOperationException("Not supported yet."); } @Override public void layoutContainer(Container parent) { int n = parent.getComponentCount(); if (n <= 0) { return; } Dimension p = preferredLayoutSize(parent); // System.out.println(p.width); // System.out.println(p.height); Component c; int height = 0, width = 0; // 计算各个组件的最大宽度和最大高度 for (int i = 0; i < n; i++) { c = parent.getComponent(i); int temph = c.getHeight(), tempw = c.getWidth(); if (temph == 0) { temph = c.getPreferredSize().height; } if (tempw == 0) { tempw = c.getPreferredSize().width; } if (height < temph) { height = temph; } if (width < tempw) { width = tempw; } } int ncols = p.width / (width + vgap); if (ncols == 0) { ncols = 1; } int nrows = n / (ncols) + 1; for (int m = 0, x = vgap; m < ncols; m++, x += vgap + width) { for (int r = 0, y = hgap; r < nrows; r++, y += hgap + height) { int i = r * ncols + m; if (i < n) { parent.getComponent(i).setBounds(x, y, width, height); } } } } @Override public String toString() { return "hgap=[" + hgap + "],vgap=[" + vgap + "]"; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -