You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
304 lines
11 KiB
304 lines
11 KiB
import javax.swing.*; |
|
import java.awt.*; |
|
|
|
|
|
public class TableLayoutHelper { |
|
public static final int FILL_NONE = 0; |
|
public static final int FILL_LASTCOLUMN = 1; |
|
public static final int FILL_LASTROW = 2; |
|
public static final int FILL_LASTCOL_AND_ROW = 3; |
|
private static final int FIVE = 5; |
|
private static final int TEN = 10; |
|
|
|
private TableLayoutHelper() { |
|
} |
|
|
|
public static JPanel createTableLayoutPane(Component[][] components, int fillType) { |
|
return createGapTableLayoutPane(components, fillType, LayoutConstants.VGAP_MEDIUM, LayoutConstants.VGAP_MEDIUM); |
|
} |
|
|
|
|
|
public static JPanel createTableLayoutPane(Component[][] components, double[] rowSize, double[] columnSize) { |
|
return createCommonTableLayoutPane(components, rowSize, columnSize, LayoutConstants.VGAP_MEDIUM); |
|
} |
|
|
|
public static JPanel createTableLayoutPaneWithTitle(String title, Component component) { |
|
return createTitlePane(title, component, LayoutConstants.CHART_ATTR_TOMARGIN); |
|
} |
|
|
|
public static JPanel createTitlePane(String title, Component component, int gap) { |
|
return createTitlePaneWithUILabel(new JLabel(title), component, gap); |
|
} |
|
|
|
/** |
|
* 标题布局(指定gap) |
|
* |
|
* @param label 标题label |
|
* @param component 组件 |
|
* @param gap 距左侧距离 |
|
* @return 布局好的组件 |
|
*/ |
|
public static JPanel createTitlePaneWithUILabel(JLabel label, Component component, int gap) { |
|
double p = TableLayout.PREFERRED; |
|
double f = TableLayout.FILL; |
|
double[] columnSize = {gap, f}; |
|
double[] rowSize = {p, p}; |
|
Component[][] components = new Component[][]{ |
|
new Component[]{label, null}, |
|
new Component[]{null, component}, |
|
}; |
|
return TableLayoutHelper.createTableLayoutPane(components, rowSize, columnSize); |
|
} |
|
|
|
|
|
public static JPanel createCommonTableLayoutPane(Component[][] components, double[] rowSize, double[] columnSize, double gap) { |
|
|
|
return createGapTableLayoutPane(components, rowSize, columnSize, gap, gap); |
|
} |
|
|
|
public static JPanel createGapTableLayoutPane(Component[][] components, int fillType, double horizontalGap, double verticalGap) { |
|
double p = TableLayout.PREFERRED; |
|
double f = TableLayout.FILL; |
|
int maxColCount = 0; |
|
for (int i = 0; i < components.length; i++) { |
|
if (components[i].length > maxColCount) { |
|
maxColCount = components[i].length; |
|
} |
|
} |
|
double[] rowSize = new double[components.length]; |
|
for (int i = 0; i < components.length; i++) { |
|
rowSize[i] = p; |
|
} |
|
double[] columnSize = new double[maxColCount]; |
|
for (int i = 0; i < maxColCount; i++) { |
|
columnSize[i] = p; |
|
} |
|
if (fillType == FILL_LASTCOLUMN && columnSize.length > 0) { |
|
columnSize[columnSize.length - 1] = f; |
|
} |
|
if (fillType == FILL_LASTROW && rowSize.length > 0) { |
|
rowSize[rowSize.length - 1] = f; |
|
} |
|
if (fillType == FILL_LASTCOL_AND_ROW) { |
|
if (columnSize.length > 0) { |
|
columnSize[columnSize.length - 1] = f; |
|
} |
|
if (rowSize.length > 0) { |
|
rowSize[rowSize.length - 1] = f; |
|
} |
|
} |
|
return createGapTableLayoutPane(components, rowSize, columnSize, horizontalGap, verticalGap); |
|
} |
|
|
|
public static JPanel createGapTableLayoutPane(Component[][] components, |
|
double[] rowSize, |
|
double[] columnSize, |
|
double horizontalGap, |
|
double verticalGap) { |
|
|
|
JPanel resultPane = setPanelLayout(rowSize, columnSize, horizontalGap, verticalGap); |
|
|
|
addComponent2ResultPane(components, rowSize, columnSize, resultPane); |
|
|
|
return resultPane; |
|
} |
|
|
|
|
|
public static JPanel createDiffVGapTableLayoutPane(Component[][] components, |
|
double[] rowSize, |
|
double[] columnSize, |
|
double horizontalGap, |
|
double[] verticalGap) { |
|
JPanel resultPane = setPanelLayout(rowSize, columnSize, horizontalGap, verticalGap); |
|
addComponent2ResultPane(components, rowSize, columnSize, resultPane); |
|
return resultPane; |
|
} |
|
|
|
|
|
public static JPanel createGapTableLayoutPane(Component[][] components, |
|
double[] rowSize, double[] columnSize, int rowCount[][], double horizontalGap, double verticalGap) { |
|
|
|
JPanel resultPane = setPanelLayout(rowSize, columnSize, horizontalGap, verticalGap); |
|
|
|
int k = components.length; |
|
int[] row = new int[k]; //存放每组控件在第几行开始 |
|
int sumRow = 1; //存放一次递增的行的数目 |
|
|
|
|
|
for (int i = 0; i < components.length; i++) { |
|
row[i] = sumRow; |
|
int maxRowCount = 1; |
|
|
|
if (i >= rowSize.length) { |
|
break; |
|
} |
|
|
|
Component[] rowComponents = components[i]; |
|
for (int j = 0; j < rowComponents.length && j < columnSize.length; j++) { |
|
if (rowComponents[j] == null) { |
|
continue; |
|
} |
|
if (isNextAllNull(rowComponents, j + 1)) { |
|
if (rowCount[i][j] != 1) { |
|
resultPane.add(rowComponents[j], (2 * j + 1) + "," + row[i] + "," + (2 * rowComponents.length - 1) + "," + (row[i] + rowCount[i][j] - 1)); |
|
if (rowCount[i][j] > maxRowCount) { |
|
maxRowCount = rowCount[i][j]; |
|
} |
|
} else { |
|
|
|
resultPane.add(rowComponents[j], (2 * j + 1) + "," + row[i] + "," + (2 * rowComponents.length - 1) + "," + row[i]); |
|
} |
|
} else { |
|
|
|
if (rowCount[i][j] != 1) { |
|
resultPane.add(rowComponents[j], (2 * j + 1) + "," + row[i] + "," + (2 * j + 1) + "," + (row[i] + rowCount[i][j] - 1)); |
|
if (rowCount[i][j] > maxRowCount) { |
|
maxRowCount = rowCount[i][j]; |
|
} |
|
} else { |
|
resultPane.add(rowComponents[j], (2 * j + 1) + "," + row[i]); |
|
} |
|
} |
|
} |
|
sumRow = row[i] + maxRowCount + 1; |
|
} |
|
|
|
return resultPane; |
|
} |
|
|
|
/** |
|
* 修改TableLayout布局的容器指定位置垂直间距的大小。 |
|
* |
|
* @param container 容器 |
|
* @param index vgap的索引,Container{vGapA[ComponentA]vGapB[ComponentB]vGapC},vGapB index=2, |
|
* 详情见{@link TableLayout#setRow(int, double)} |
|
* @param vgap vgap的新值 |
|
*/ |
|
public static void modifyTableLayoutIndexVGap(Container container, int index, double vgap) { |
|
TableLayout layout = (TableLayout) container.getLayout(); |
|
layout.setRow(index, vgap); |
|
layout.layoutContainer(container); |
|
container.revalidate(); |
|
container.repaint(); |
|
} |
|
|
|
/** |
|
* 修改TableLayout布局的容器指定位置水平间距的大小。 |
|
* |
|
* @param container 容器 |
|
* @param index hgap的索引,Container{hGapA[ComponentA]hGapB[ComponentB]},hGapB index=2, |
|
* 详情见{@link TableLayout#setColumn(int, double)} |
|
* @param hgap hgap的新值 |
|
*/ |
|
public static void modifyTableLayoutIndexHGap(Container container, int index, double hgap) { |
|
TableLayout layout = (TableLayout) container.getLayout(); |
|
layout.setColumn(index, hgap); |
|
layout.layoutContainer(container); |
|
container.revalidate(); |
|
container.repaint(); |
|
} |
|
|
|
private static void addComponent2ResultPane(Component[][] components, double[] rowSize, double[] columnSize, JPanel resultPane) { |
|
|
|
for (int i = 0; i < components.length; i++) { |
|
if (i >= rowSize.length) { |
|
break; |
|
} |
|
Component[] rowComponents = components[i]; |
|
for (int j = 0; j < rowComponents.length && j < columnSize.length; j++) { |
|
if (rowComponents[j] == null) { |
|
continue; |
|
} |
|
|
|
if (isNextAllNull(rowComponents, j + 1)) { |
|
resultPane.add(rowComponents[j], (2 * j + 1) + "," + (2 * i + 1) + "," + (2 * rowComponents.length - 1) + ",0"); |
|
} else { |
|
resultPane.add(rowComponents[j], (2 * j + 1) + "," + (2 * i + 1)); |
|
} |
|
} |
|
} |
|
|
|
} |
|
|
|
private static boolean isNextAllNull(Component[] rowComponents, int currentIndex) { |
|
for (int i = currentIndex; i < rowComponents.length; i++) { |
|
if (rowComponents[i] != null) { |
|
return false; |
|
} |
|
} |
|
return true; |
|
} |
|
|
|
private static JPanel setPanelLayout(double[] rowSize, double[] columnSize, double horizontalGap, double verticalGap) { |
|
double layoutSize[][] = new double[2][]; |
|
double layoutColumnSize[] = new double[2 * columnSize.length]; |
|
double layoutRowSize[] = new double[2 * rowSize.length]; |
|
|
|
for (int i = 0; i < columnSize.length; i++) { |
|
if (i == 0) { |
|
layoutColumnSize[i * 2] = 0; |
|
} else { |
|
layoutColumnSize[i * 2] = horizontalGap; |
|
} |
|
layoutColumnSize[i * 2 + 1] = columnSize[i]; |
|
} |
|
|
|
for (int i = 0; i < rowSize.length; i++) { |
|
if (i == 0) { |
|
layoutRowSize[i * 2] = 0; |
|
} else { |
|
layoutRowSize[i * 2] = verticalGap; |
|
} |
|
layoutRowSize[i * 2 + 1] = rowSize[i]; |
|
} |
|
|
|
layoutSize[0] = layoutColumnSize; |
|
layoutSize[1] = layoutRowSize; |
|
|
|
JPanel resultPane = new JPanel(); |
|
resultPane.setLayout(new TableLayout(layoutSize)); |
|
|
|
return resultPane; |
|
} |
|
|
|
/** |
|
* 创建具有不同垂直间距的TableLayout面板 |
|
* |
|
* @param rowSize 行 |
|
* @param columnSize 列 |
|
* @param horizontalGap 水平间距 |
|
* @param verticalGap 不同的垂直间距 |
|
* @return JPanel 具有不同垂直间距的TableLayout面板 |
|
*/ |
|
private static JPanel setPanelLayout(double[] rowSize, double[] columnSize, double horizontalGap, double[] verticalGap) { |
|
double layoutSize[][] = new double[2][]; |
|
double layoutColumnSize[] = new double[2 * columnSize.length]; |
|
double layoutRowSize[] = new double[2 * rowSize.length]; |
|
|
|
for (int i = 0; i < columnSize.length; i++) { |
|
if (i == 0) { |
|
layoutColumnSize[i * 2] = 0; |
|
} else { |
|
layoutColumnSize[i * 2] = horizontalGap; |
|
} |
|
layoutColumnSize[i * 2 + 1] = columnSize[i]; |
|
} |
|
|
|
for (int i = 0; i < rowSize.length; i++) { |
|
if (i == 0) { |
|
layoutRowSize[i * 2] = 0; |
|
} else { |
|
layoutRowSize[i * 2] = verticalGap[i - 1]; |
|
} |
|
layoutRowSize[i * 2 + 1] = rowSize[i]; |
|
} |
|
|
|
layoutSize[0] = layoutColumnSize; |
|
layoutSize[1] = layoutRowSize; |
|
|
|
JPanel resultPane = new JPanel(); |
|
resultPane.setLayout(new TableLayout(layoutSize)); |
|
|
|
return resultPane; |
|
} |
|
} |