|
|
|
@ -4,11 +4,10 @@ package com.fr.design.layout;
|
|
|
|
|
import com.fr.design.constants.LayoutConstants; |
|
|
|
|
import com.fr.design.gui.ibutton.UIButton; |
|
|
|
|
import com.fr.design.gui.ilable.UILabel; |
|
|
|
|
import com.fr.general.Inter; |
|
|
|
|
import com.fr.design.utils.gui.GUICoreUtils; |
|
|
|
|
import com.fr.general.Inter; |
|
|
|
|
|
|
|
|
|
import javax.swing.*; |
|
|
|
|
|
|
|
|
|
import java.awt.*; |
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -19,6 +18,7 @@ public class TableLayoutHelper {
|
|
|
|
|
public static final int FILL_LASTCOL_AND_ROW = 3; |
|
|
|
|
private static final int FIVE = 5; |
|
|
|
|
private static final int TEN = 10; |
|
|
|
|
|
|
|
|
|
private TableLayoutHelper() { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -34,11 +34,12 @@ public class TableLayoutHelper {
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
*最常用的最后一行列fill或者都是prefer的tablelayout |
|
|
|
|
* 最常用的最后一行列fill或者都是prefer的tablelayout |
|
|
|
|
* |
|
|
|
|
* @param components 组件 |
|
|
|
|
* @return |
|
|
|
|
*/ |
|
|
|
|
public static JPanel createTableLayoutPane(Component[][] components, int fillType){ |
|
|
|
|
public static JPanel createTableLayoutPane(Component[][] components, int fillType) { |
|
|
|
|
return createGapTableLayoutPane(components, fillType, LayoutConstants.VGAP_MEDIUM, LayoutConstants.VGAP_MEDIUM); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -49,33 +50,35 @@ public class TableLayoutHelper {
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 标题布局(二级菜单距左边框46) |
|
|
|
|
* |
|
|
|
|
* @param title 标题 |
|
|
|
|
* @param component 组件 |
|
|
|
|
* @return 布局好的组件 |
|
|
|
|
*/ |
|
|
|
|
public static JPanel createTableLayoutPaneWithTitle(String title, Component component){ |
|
|
|
|
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){ |
|
|
|
|
public static JPanel createTitlePane(String title, Component component, int gap) { |
|
|
|
|
return createTitlePaneWithUILabel(new UILabel(title), component, gap); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 标题布局(指定gap) |
|
|
|
|
* |
|
|
|
|
* @param label 标题label |
|
|
|
|
* @param component 组件 |
|
|
|
|
* @param gap 距左侧距离 |
|
|
|
|
* @return 布局好的组件 |
|
|
|
|
*/ |
|
|
|
|
public static JPanel createTitlePaneWithUILabel(UILabel label, Component component, int gap){ |
|
|
|
|
public static JPanel createTitlePaneWithUILabel(UILabel 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}, |
|
|
|
|
new Component[]{label, null}, |
|
|
|
|
new Component[]{null, component}, |
|
|
|
|
}; |
|
|
|
|
return TableLayoutHelper.createTableLayoutPane(components, rowSize, columnSize); |
|
|
|
|
} |
|
|
|
@ -90,17 +93,17 @@ public class TableLayoutHelper {
|
|
|
|
|
double p = TableLayout.PREFERRED; |
|
|
|
|
double f = TableLayout.FILL; |
|
|
|
|
int maxColCount = 0; |
|
|
|
|
for (int i = 0 ; i < components.length; i ++) { |
|
|
|
|
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 ++) { |
|
|
|
|
for (int i = 0; i < components.length; i++) { |
|
|
|
|
rowSize[i] = p; |
|
|
|
|
} |
|
|
|
|
double[] columnSize = new double[maxColCount]; |
|
|
|
|
for (int i = 0; i < maxColCount; i ++) { |
|
|
|
|
for (int i = 0; i < maxColCount; i++) { |
|
|
|
|
columnSize[i] = p; |
|
|
|
|
} |
|
|
|
|
if (fillType == FILL_LASTCOLUMN && columnSize.length > 0) { |
|
|
|
@ -109,7 +112,7 @@ public class TableLayoutHelper {
|
|
|
|
|
if (fillType == FILL_LASTROW && rowSize.length > 0) { |
|
|
|
|
rowSize[rowSize.length - 1] = f; |
|
|
|
|
} |
|
|
|
|
if (fillType == FILL_LASTCOL_AND_ROW ) { |
|
|
|
|
if (fillType == FILL_LASTCOL_AND_ROW) { |
|
|
|
|
if (columnSize.length > 0) { |
|
|
|
|
columnSize[columnSize.length - 1] = f; |
|
|
|
|
} |
|
|
|
@ -121,28 +124,26 @@ public class TableLayoutHelper {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static JPanel createGapTableLayoutPane(Component[][] components, |
|
|
|
|
double[] rowSize, double[] columnSize, double horizontalGap, double verticalGap) { |
|
|
|
|
double[] rowSize, |
|
|
|
|
double[] columnSize, |
|
|
|
|
double horizontalGap, |
|
|
|
|
double verticalGap) { |
|
|
|
|
|
|
|
|
|
JPanel resultPane = setPanelLayout(rowSize, columnSize, horizontalGap, verticalGap); |
|
|
|
|
|
|
|
|
|
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; |
|
|
|
|
} |
|
|
|
|
addComponent2ResultPane(components, rowSize, columnSize, resultPane); |
|
|
|
|
|
|
|
|
|
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)); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
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; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -198,6 +199,57 @@ public class TableLayoutHelper {
|
|
|
|
|
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.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.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++) { |
|
|
|
@ -240,21 +292,63 @@ public class TableLayoutHelper {
|
|
|
|
|
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; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 图表属性表用的比较多的一种布局,第一行是title,下面的components相当于二级菜单 |
|
|
|
|
* 二级菜单和上层面板的间距在layoutconstants里定义 |
|
|
|
|
* |
|
|
|
|
* @param title |
|
|
|
|
* @param components 二级菜单的内容 |
|
|
|
|
* @param rowSize 二级菜单的行数 |
|
|
|
|
* @param columnSize 二级菜单的列数 |
|
|
|
|
* @return |
|
|
|
|
*/ |
|
|
|
|
public static JPanel createTableLayoutPane4Chart(String[] title, Component[][] components, double[] rowSize, double[] columnSize){ |
|
|
|
|
public static JPanel createTableLayoutPane4Chart(String[] title, Component[][] components, double[] rowSize, double[] columnSize) { |
|
|
|
|
JPanel secondMenu = createTableLayoutPane(components, rowSize, columnSize); |
|
|
|
|
double p = TableLayout.PREFERRED; |
|
|
|
|
double f = TableLayout.FILL; |
|
|
|
|
double[] column = {LayoutConstants.CHART_ATTR_TOMARGIN, f}; |
|
|
|
|
double[] row = { p,p}; |
|
|
|
|
double[] row = {p, p}; |
|
|
|
|
Component[][] comp = new Component[][]{ |
|
|
|
|
new Component[]{new UILabel(Inter.getLocText(title)), null}, |
|
|
|
|
new Component[]{null, secondMenu}, |
|
|
|
@ -262,35 +356,44 @@ public class TableLayoutHelper {
|
|
|
|
|
return createTableLayoutPane(comp, row, column); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static void main (String[] args) { |
|
|
|
|
public static void main(String[] args) { |
|
|
|
|
JFrame jf = new JFrame("test"); |
|
|
|
|
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
|
|
|
|
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); |
|
|
|
|
JPanel content = (JPanel) jf.getContentPane(); |
|
|
|
|
content.setLayout(new GridLayout(2,2)); |
|
|
|
|
content.setLayout(new GridLayout(2, 3)); |
|
|
|
|
double p = TableLayout.PREFERRED; |
|
|
|
|
double f = TableLayout.FILL; |
|
|
|
|
JPanel jp1 = TableLayoutHelper.createTableLayoutPane(createTestComponents("jp1"), TableLayoutHelper.FILL_NONE); |
|
|
|
|
JPanel jp2 = TableLayoutHelper.createGapTableLayoutPane(createTestComponents("jp2"), TableLayoutHelper.FILL_LASTCOL_AND_ROW, 2 * TEN, 2 * TEN); |
|
|
|
|
JPanel jp3 = TableLayoutHelper.createGapTableLayoutPane(createTestComponents("jp3"), |
|
|
|
|
new double[] {f,p,f,p}, new double[] {f,f}, 4,4); |
|
|
|
|
new double[]{f, p, f, p}, new double[]{f, f}, 4, 4); |
|
|
|
|
JPanel jp4 = TableLayoutHelper.createGapTableLayoutPane(createTestComponents("jp4"), |
|
|
|
|
new double[] {p,FIVE * TEN,p,p,p,p},new double[] {p, f}, new int[][] {{1,3},{1,1},{1,1},{1,1},{1,1}}, 1, FIVE); |
|
|
|
|
new double[]{p, FIVE * TEN, p, p, p, p}, new double[]{p, f}, new int[][]{{1, 3}, {1, 1}, {1, 1}, {1, 1}, {1, 1}}, 1, FIVE); |
|
|
|
|
|
|
|
|
|
JPanel jp5 = TableLayoutHelper.createDiffVGapTableLayoutPane(createTestComponents("jp5"), |
|
|
|
|
new double[]{p, p, p, p, p}, new double[]{p, f}, 4, new double[]{4, 8, 14, 4}); |
|
|
|
|
JPanel jp6 = new JPanel(); |
|
|
|
|
jp1.setBorder(BorderFactory.createLineBorder(Color.red)); |
|
|
|
|
jp2.setBorder(BorderFactory.createLineBorder(Color.red)); |
|
|
|
|
jp3.setBorder(BorderFactory.createLineBorder(Color.red)); |
|
|
|
|
jp4.setBorder(BorderFactory.createLineBorder(Color.red)); |
|
|
|
|
jp5.setBorder(BorderFactory.createLineBorder(Color.red)); |
|
|
|
|
jp6.setBorder(BorderFactory.createLineBorder(Color.red)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
content.add(jp1); |
|
|
|
|
content.add(jp2); |
|
|
|
|
content.add(jp3); |
|
|
|
|
content.add(jp4); |
|
|
|
|
content.add(jp5); |
|
|
|
|
content.add(jp6); |
|
|
|
|
|
|
|
|
|
GUICoreUtils.centerWindow(jf); |
|
|
|
|
jf.setSize(600, 600); |
|
|
|
|
jf.setSize(900, 600); |
|
|
|
|
jf.setVisible(true); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private static Component[][] createTestComponents (String name ) { |
|
|
|
|
private static Component[][] createTestComponents(String name) { |
|
|
|
|
UILabel label1 = new UILabel(name + "laaaable1"); |
|
|
|
|
UILabel label2 = new UILabel(name + "lable2"); |
|
|
|
|
UILabel label3 = new UILabel(name + "lable3"); |
|
|
|
@ -312,13 +415,12 @@ public class TableLayoutHelper {
|
|
|
|
|
button2.setSize(button2.getPreferredSize()); |
|
|
|
|
button1.setBackground(Color.darkGray); |
|
|
|
|
button2.setBackground(Color.darkGray); |
|
|
|
|
Component[][] components = new Component[][] { |
|
|
|
|
new Component[] {label1, button1}, |
|
|
|
|
new Component[] {label2, null}, |
|
|
|
|
new Component[] {label3}, |
|
|
|
|
new Component[] {null,label4}, |
|
|
|
|
new Component[] {button2, label5} |
|
|
|
|
} ; |
|
|
|
|
return components; |
|
|
|
|
return new Component[][]{ |
|
|
|
|
new Component[]{label1, button1}, |
|
|
|
|
new Component[]{label2, null}, |
|
|
|
|
new Component[]{label3}, |
|
|
|
|
new Component[]{null, label4}, |
|
|
|
|
new Component[]{button2, label5} |
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
|
} |